forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.go
47 lines (39 loc) · 711 Bytes
/
watch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package gossip
import (
"errors"
"github.com/micro/go-micro/registry"
)
type gossipWatcher struct {
next chan *registry.Result
stop chan bool
}
func newGossipWatcher(ch chan *registry.Result, exit chan bool) (registry.Watcher, error) {
stop := make(chan bool)
go func() {
<-stop
close(exit)
}()
return &gossipWatcher{
next: ch,
stop: stop,
}, nil
}
func (m *gossipWatcher) Next() (*registry.Result, error) {
select {
case r, ok := <-m.next:
if !ok {
return nil, errors.New("result chan closed")
}
return r, nil
case <-m.stop:
return nil, errors.New("watcher stopped")
}
}
func (m *gossipWatcher) Stop() {
select {
case <-m.stop:
return
default:
close(m.stop)
}
}