mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-22 19:50:33 +03:00
set: Fix race condition
This commit is contained in:
parent
3572c4674c
commit
bdd9274aaa
10
set/set.go
10
set/set.go
@ -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
|
||||
// element.
|
||||
func (s *Set) Each(fn IterFunc) error {
|
||||
var err error
|
||||
s.RLock()
|
||||
defer s.RUnlock()
|
||||
for key, item := range s.lookup {
|
||||
if item.Value() == nil {
|
||||
// Expired
|
||||
defer s.cleanup(key)
|
||||
continue
|
||||
}
|
||||
if err := fn(key, item); err != nil {
|
||||
if err = fn(key, item); err != nil {
|
||||
// Abort early
|
||||
return err
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
s.RUnlock()
|
||||
return err
|
||||
}
|
||||
|
||||
// ListPrefix returns a list of items with a prefix, normalized.
|
||||
|
Loading…
x
Reference in New Issue
Block a user