etcd/wait/wait.go

42 lines
605 B
Go
Raw Normal View History

2014-08-20 02:54:59 +04:00
package wait
2014-08-29 03:41:42 +04:00
import (
"sync"
)
2014-08-20 02:54:59 +04:00
type Wait interface {
Register(id int64) <-chan interface{}
Trigger(id int64, x interface{})
}
2014-08-26 05:39:02 +04:00
type List struct {
2014-08-20 02:54:59 +04:00
l sync.Mutex
m map[int64]chan interface{}
}
2014-08-29 03:41:42 +04:00
func New() *List {
return &List{m: make(map[int64]chan interface{})}
2014-08-20 02:54:59 +04:00
}
2014-08-29 03:41:42 +04:00
func (w *List) Register(id int64) <-chan interface{} {
2014-08-20 02:54:59 +04:00
w.l.Lock()
defer w.l.Unlock()
ch := w.m[id]
if ch == nil {
ch = make(chan interface{}, 1)
w.m[id] = ch
}
return ch
}
2014-08-29 03:41:42 +04:00
func (w *List) Trigger(id int64, x interface{}) {
2014-08-20 02:54:59 +04:00
w.l.Lock()
ch := w.m[id]
delete(w.m, id)
w.l.Unlock()
if ch != nil {
ch <- x
close(ch)
}
}