[eventsHandler] Use Lock/Unlock to sync a write access

Delete keys from internal maps in a lazy way.
The heavy Lock is acquired only when we have errors.

Signed-off-by: Anton Tiurin <noxiouz@yandex.ru>
This commit is contained in:
Anton Tiurin 2015-04-01 23:23:43 +03:00
parent 1f2bd67555
commit 78f21a8e88
1 changed files with 21 additions and 3 deletions

View File

@ -49,11 +49,12 @@ func (eh *eventsHandler) Handle(e *cluster.Event) error {
"time", e.Time,
"node", cluster.SerializeNode(e.Node))
var failed []string
for key, w := range eh.ws {
if _, err := fmt.Fprintf(w, str); err != nil {
close(eh.cs[key])
delete(eh.ws, key)
delete(eh.cs, key)
// collect them to handle later under Lock
failed = append(failed, key)
continue
}
@ -62,7 +63,24 @@ func (eh *eventsHandler) Handle(e *cluster.Event) error {
}
}
eh.RUnlock()
if len(failed) > 0 {
eh.Lock()
for _, key := range failed {
if ch, ok := eh.cs[key]; ok {
close(ch)
// the maps are expected to have the same keys
delete(eh.cs, key)
delete(eh.ws, key)
}
}
eh.Unlock()
}
return nil
}