diff options
author | obscuren <geffobscura@gmail.com> | 2014-03-03 07:55:10 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-03-03 07:55:10 +0800 |
commit | d2bc57cd34fe4da3ecec3ff95bc4ef9e74589e5d (patch) | |
tree | c7d976bb1f3db543c025329fc48eb4a84cb5eb65 /ethutil/reactor.go | |
parent | d65b4cd0dd49975410374801fae3ece7d7e087b3 (diff) | |
download | dexon-d2bc57cd34fe4da3ecec3ff95bc4ef9e74589e5d.tar dexon-d2bc57cd34fe4da3ecec3ff95bc4ef9e74589e5d.tar.gz dexon-d2bc57cd34fe4da3ecec3ff95bc4ef9e74589e5d.tar.bz2 dexon-d2bc57cd34fe4da3ecec3ff95bc4ef9e74589e5d.tar.lz dexon-d2bc57cd34fe4da3ecec3ff95bc4ef9e74589e5d.tar.xz dexon-d2bc57cd34fe4da3ecec3ff95bc4ef9e74589e5d.tar.zst dexon-d2bc57cd34fe4da3ecec3ff95bc4ef9e74589e5d.zip |
PoC reactor pattern
Diffstat (limited to 'ethutil/reactor.go')
-rw-r--r-- | ethutil/reactor.go | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/ethutil/reactor.go b/ethutil/reactor.go index b3f8b9b5b..f8084986c 100644 --- a/ethutil/reactor.go +++ b/ethutil/reactor.go @@ -13,6 +13,9 @@ type ReactorEvent struct { // Post the specified reactor resource on the channels // currently subscribed func (e *ReactorEvent) Post(react React) { + e.mut.Lock() + defer e.mut.Unlock() + for _, ch := range e.chans { go func(ch chan React) { ch <- react @@ -22,11 +25,17 @@ func (e *ReactorEvent) Post(react React) { // Add a subscriber to this event func (e *ReactorEvent) Add(ch chan React) { + e.mut.Lock() + defer e.mut.Unlock() + e.chans = append(e.chans, ch) } // Remove a subscriber func (e *ReactorEvent) Remove(ch chan React) { + e.mut.Lock() + defer e.mut.Unlock() + for i, c := range e.chans { if c == ch { e.chans = append(e.chans[:i], e.chans[i+1:]...) |