micro/registry/gossip/watcher.go

54 lines
930 B
Go
Raw Normal View History

2018-12-04 19:41:40 +03:00
package gossip
import (
"github.com/micro/go-micro/registry"
)
2018-12-06 21:19:05 +03:00
type gossipWatcher struct {
wo registry.WatchOptions
next chan *registry.Result
stop chan bool
2018-12-04 19:41:40 +03:00
}
2018-12-06 21:19:05 +03:00
func newGossipWatcher(ch chan *registry.Result, stop chan bool, opts ...registry.WatchOption) (registry.Watcher, error) {
var wo registry.WatchOptions
for _, o := range opts {
o(&wo)
}
return &gossipWatcher{
wo: wo,
next: ch,
stop: stop,
}, nil
}
func (m *gossipWatcher) Next() (*registry.Result, error) {
2018-12-04 19:41:40 +03:00
for {
select {
2018-12-06 21:19:05 +03:00
case r, ok := <-m.next:
if !ok {
return nil, registry.ErrWatcherStopped
2018-12-04 19:41:40 +03:00
}
2018-12-06 21:19:05 +03:00
// check watch options
if len(m.wo.Service) > 0 && r.Service.Name != m.wo.Service {
2018-12-04 19:41:40 +03:00
continue
}
nr := &registry.Result{}
*nr = *r
return nr, nil
2018-12-06 21:19:05 +03:00
case <-m.stop:
2018-12-04 19:41:40 +03:00
return nil, registry.ErrWatcherStopped
}
}
}
2018-12-06 21:19:05 +03:00
func (m *gossipWatcher) Stop() {
2018-12-04 19:41:40 +03:00
select {
2018-12-06 21:19:05 +03:00
case <-m.stop:
2018-12-04 19:41:40 +03:00
return
default:
2018-12-06 21:19:05 +03:00
close(m.stop)
2018-12-04 19:41:40 +03:00
}
}