From e21441cecf8cc70ef9cdf0777db7147f14e339c4 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Fri, 8 Sep 2023 08:31:37 +0000 Subject: [PATCH] add limit to SetBatch goroutine number --- internal/cache/bbolt/bbolt.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/internal/cache/bbolt/bbolt.go b/internal/cache/bbolt/bbolt.go index ef2664d13e..43318c5efc 100644 --- a/internal/cache/bbolt/bbolt.go +++ b/internal/cache/bbolt/bbolt.go @@ -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 { @@ -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 }