2016-05-06 21:56:02 +03:00
|
|
|
package gossip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
)
|
|
|
|
|
|
|
|
type gossipWatcher struct {
|
2018-02-19 23:27:36 +03:00
|
|
|
wo registry.WatchOptions
|
2016-05-06 21:56:02 +03:00
|
|
|
next chan *registry.Result
|
|
|
|
stop chan bool
|
|
|
|
}
|
|
|
|
|
2018-02-19 23:27:36 +03:00
|
|
|
func newGossipWatcher(ch chan *registry.Result, exit chan bool, opts ...registry.WatchOption) (registry.Watcher, error) {
|
|
|
|
var wo registry.WatchOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&wo)
|
|
|
|
}
|
|
|
|
|
2016-05-06 21:56:02 +03:00
|
|
|
stop := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-stop
|
|
|
|
close(exit)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return &gossipWatcher{
|
2018-02-19 23:27:36 +03:00
|
|
|
wo: wo,
|
2016-05-06 21:56:02 +03:00
|
|
|
next: ch,
|
|
|
|
stop: stop,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *gossipWatcher) Next() (*registry.Result, error) {
|
2018-02-19 23:27:36 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case r, ok := <-m.next:
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("result chan closed")
|
|
|
|
}
|
|
|
|
// check watch options
|
|
|
|
if len(m.wo.Service) > 0 && r.Service.Name != m.wo.Service {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
case <-m.stop:
|
|
|
|
return nil, errors.New("watcher stopped")
|
2016-05-06 21:56:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *gossipWatcher) Stop() {
|
|
|
|
select {
|
|
|
|
case <-m.stop:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
close(m.stop)
|
|
|
|
}
|
|
|
|
}
|