reduce scope of exclusive lock in badger markset

This commit is contained in:
vyzo 2021-07-22 13:47:40 +03:00
parent 12c3432b8d
commit f2b7c3e6f2

View File

@ -82,24 +82,27 @@ func (e *BadgerMarkSetEnv) Close() error {
func (s *BadgerMarkSet) Mark(c cid.Cid) error { func (s *BadgerMarkSet) Mark(c cid.Cid) error {
s.mx.Lock() s.mx.Lock()
defer s.mx.Unlock()
if s.pend == nil { if s.pend == nil {
s.mx.Unlock()
return errMarkSetClosed return errMarkSetClosed
} }
s.pend[string(c.Hash())] = struct{}{} s.pend[string(c.Hash())] = struct{}{}
if len(s.pend) < badgerMarkSetBatchSize { if len(s.pend) < badgerMarkSetBatchSize {
s.mx.Unlock()
return nil return nil
} }
pend := s.pend pend := s.pend
s.pend = make(map[string]struct{}) s.pend = make(map[string]struct{})
db := s.db
s.mx.Unlock()
empty := []byte{} // not nil empty := []byte{} // not nil
batch := s.db.NewWriteBatch() batch := db.NewWriteBatch()
defer batch.Cancel() defer batch.Cancel()
for k := range pend { for k := range pend {