set: Fix race condition

This commit is contained in:
Andrey Petrov 2018-12-15 20:37:46 -05:00
parent 3572c4674c
commit bdd9274aaa

View File

@ -156,19 +156,21 @@ func (s *Set) Replace(oldKey string, item Item) error {
// Each loops over every item while holding a read lock and applies fn to each // Each loops over every item while holding a read lock and applies fn to each
// element. // element.
func (s *Set) Each(fn IterFunc) error { func (s *Set) Each(fn IterFunc) error {
var err error
s.RLock() s.RLock()
defer s.RUnlock()
for key, item := range s.lookup { for key, item := range s.lookup {
if item.Value() == nil { if item.Value() == nil {
// Expired
defer s.cleanup(key) defer s.cleanup(key)
continue continue
} }
if err := fn(key, item); err != nil { if err = fn(key, item); err != nil {
// Abort early // Abort early
return err break
} }
} }
return nil s.RUnlock()
return err
} }
// ListPrefix returns a list of items with a prefix, normalized. // ListPrefix returns a list of items with a prefix, normalized.