micro/registry/gossip/watcher.go
2018-12-04 16:41:40 +00:00

41 lines
566 B
Go

package gossip
import (
"github.com/micro/go-micro/registry"
)
type watcher struct {
id string
srv string
ch chan *registry.Result
exit chan bool
fn func()
}
func (w *watcher) Next() (*registry.Result, error) {
for {
select {
case r := <-w.ch:
if r.Service == nil {
continue
}
if len(w.srv) > 0 && (r.Service.Name != w.srv) {
continue
}
return r, nil
case <-w.exit:
return nil, registry.ErrWatcherStopped
}
}
}
func (w *watcher) Stop() {
select {
case <-w.exit:
return
default:
close(w.exit)
w.fn()
}
}