Skip to content

Commit

Permalink
refactor: use waitgroup and context
Browse files Browse the repository at this point in the history
  • Loading branch information
lvrach committed Sep 9, 2024
1 parent d45ee22 commit 14e8452
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions services/dedup/badger/badger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package badger

import (
"context"
"fmt"
"strconv"
"sync"
Expand All @@ -23,11 +24,12 @@ type BadgerDB struct {
logger loggerForBadger
badgerDB *badger.DB
window config.ValueLoader[time.Duration]
close chan struct{}
gcDone chan struct{}
path string
opts badger.Options
once sync.Once
wg sync.WaitGroup
bgCtx context.Context
cancel context.CancelFunc
}

// DefaultPath returns the default path for the deduplication service's badger DB
Expand Down Expand Up @@ -57,14 +59,16 @@ func NewBadgerDB(conf *config.Config, stats stats.Stats, path string) *Dedup {
WithSyncWrites(conf.GetBool("BadgerDB.syncWrites", false)).
WithDetectConflicts(conf.GetBool("BadgerDB.detectConflicts", false))

bgCtx, cancel := context.WithCancel(context.Background())
db := &BadgerDB{
stats: stats,
logger: loggerForBadger{log},
path: path,
gcDone: make(chan struct{}),
close: make(chan struct{}),
window: dedupWindow,
opts: badgerOpts,
wg: sync.WaitGroup{},
bgCtx: bgCtx,
cancel: cancel,
}
return &Dedup{
badgerDB: db,
Expand Down Expand Up @@ -114,23 +118,27 @@ func (d *BadgerDB) Set(kvs []types.KeyValue) error {
}

func (d *BadgerDB) Close() {
d.init()
close(d.close)
<-d.gcDone
_ = d.badgerDB.Close()
d.cancel()
d.wg.Wait()
if d.badgerDB != nil {
_ = d.badgerDB.Close()
}
}

func (d *BadgerDB) init() error {
var err error

d.once.Do(func() {
d.wg = sync.WaitGroup{}

d.badgerDB, err = badger.Open(d.opts)
if err != nil {
return
}
d.wg.Add(1)
rruntime.Go(func() {
defer d.wg.Done()
d.gcLoop()
close(d.gcDone)
})
})
return err
Expand All @@ -139,12 +147,15 @@ func (d *BadgerDB) init() error {
func (d *BadgerDB) gcLoop() {
for {
select {
case <-d.close:
case <-d.bgCtx.Done():
_ = d.badgerDB.RunValueLogGC(0.5)
return
case <-time.After(5 * time.Minute):
}
again:
if d.bgCtx.Err() != nil {
return

Check warning on line 157 in services/dedup/badger/badger.go

View check run for this annotation

Codecov / codecov/patch

services/dedup/badger/badger.go#L156-L157

Added lines #L156 - L157 were not covered by tests
}
// One call would only result in removal of at max one log file.
// As an optimization, you could also immediately re-run it whenever it returns nil error
// (this is why `goto again` is used).
Expand Down

0 comments on commit 14e8452

Please sign in to comment.