Skip to content

Commit

Permalink
add limit to SetBatch goroutine number
Browse files Browse the repository at this point in the history
  • Loading branch information
ykadowak committed Sep 15, 2023
1 parent a611e88 commit e21441c
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions internal/cache/bbolt/bbolt.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package bbolt

import (
"context"
"fmt"
"os"

"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/sync"
bolt "go.etcd.io/bbolt"
"golang.org/x/sync/errgroup"
)

type Bbolt struct {
Expand Down Expand Up @@ -48,20 +49,21 @@ func (b *Bbolt) Set(key string, val []byte) error {
}

func (b *Bbolt) SetBatch(kv map[string]struct{}) error {
var wg sync.WaitGroup
eg, _ := errgroup.WithContext(context.Background())
eg.SetLimit(200)
for k := range kv {
wg.Add(1)
go func(key string) {
defer wg.Done()
key := k
eg.Go(func() error {
b.db.Batch(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
// FIXME: for index correction, value doesn't matter, but for more general use, it should be considered
err := b.Put([]byte(key), nil)
return err
})
}(k)
return nil
})
}
wg.Wait()
eg.Wait()

return nil
}
Expand Down

0 comments on commit e21441c

Please sign in to comment.