Skip to content

Commit

Permalink
fix race in badger batch callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
SaveTheRbtz committed Apr 12, 2022
1 parent ca0b3c4 commit 35babdb
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion storage/badger/batch.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package badger

import (
"sync"

"github.com/dgraph-io/badger/v2"
)

type Batch struct {
writer *badger.WriteBatch
writer *badger.WriteBatch

m sync.RWMutex
callbacks []func()
}

Expand All @@ -26,6 +30,8 @@ func (b *Batch) GetWriter() *badger.WriteBatch {
// useful for implementing the cache where we will only cache
// after the batch has been successfully flushed
func (b *Batch) OnSucceed(callback func()) {
b.m.Lock()
defer b.m.Unlock()
b.callbacks = append(b.callbacks, callback)
}

Expand All @@ -38,6 +44,8 @@ func (b *Batch) Flush() error {
return err
}

b.m.RLock()
defer b.m.RUnlock()
for _, callback := range b.callbacks {
callback()
}
Expand Down

0 comments on commit 35babdb

Please sign in to comment.