mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-07-02 03:34:29 +02: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
|
// 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.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user