mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-12 08:23:58 +02:00
54 lines
932 B
Go
54 lines
932 B
Go
package gossip
|
|
|
|
import (
|
|
"github.com/asim/go-micro/v3/registry"
|
|
)
|
|
|
|
type gossipWatcher struct {
|
|
wo registry.WatchOptions
|
|
next chan *registry.Result
|
|
stop chan bool
|
|
}
|
|
|
|
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) {
|
|
for {
|
|
select {
|
|
case r, ok := <-m.next:
|
|
if !ok {
|
|
return nil, registry.ErrWatcherStopped
|
|
}
|
|
// check watch options
|
|
if len(m.wo.Service) > 0 && r.Service.Name != m.wo.Service {
|
|
continue
|
|
}
|
|
nr := ®istry.Result{}
|
|
*nr = *r
|
|
return nr, nil
|
|
case <-m.stop:
|
|
return nil, registry.ErrWatcherStopped
|
|
}
|
|
}
|
|
}
|
|
|
|
func (m *gossipWatcher) Stop() {
|
|
select {
|
|
case <-m.stop:
|
|
return
|
|
default:
|
|
close(m.stop)
|
|
}
|
|
}
|