2018-12-04 16:41:40 +00:00
|
|
|
package gossip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
)
|
|
|
|
|
2018-12-06 18:19:05 +00:00
|
|
|
type gossipWatcher struct {
|
|
|
|
wo registry.WatchOptions
|
|
|
|
next chan *registry.Result
|
|
|
|
stop chan bool
|
2018-12-04 16:41:40 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 18:19:05 +00: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 16:41:40 +00:00
|
|
|
for {
|
|
|
|
select {
|
2018-12-06 18:19:05 +00:00
|
|
|
case r, ok := <-m.next:
|
|
|
|
if !ok {
|
|
|
|
return nil, registry.ErrWatcherStopped
|
2018-12-04 16:41:40 +00:00
|
|
|
}
|
2018-12-06 18:19:05 +00:00
|
|
|
// check watch options
|
|
|
|
if len(m.wo.Service) > 0 && r.Service.Name != m.wo.Service {
|
2018-12-04 16:41:40 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return r, nil
|
2018-12-06 18:19:05 +00:00
|
|
|
case <-m.stop:
|
2018-12-04 16:41:40 +00:00
|
|
|
return nil, registry.ErrWatcherStopped
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 18:19:05 +00:00
|
|
|
func (m *gossipWatcher) Stop() {
|
2018-12-04 16:41:40 +00:00
|
|
|
select {
|
2018-12-06 18:19:05 +00:00
|
|
|
case <-m.stop:
|
2018-12-04 16:41:40 +00:00
|
|
|
return
|
|
|
|
default:
|
2018-12-06 18:19:05 +00:00
|
|
|
close(m.stop)
|
2018-12-04 16:41:40 +00:00
|
|
|
}
|
|
|
|
}
|