watch branch

This commit is contained in:
Asim Aslam 2018-02-19 20:27:36 +00:00 committed by Vasiliy Tolstov
parent 42ab4ca7b5
commit 1d69bd4a16
2 changed files with 23 additions and 10 deletions

View File

@ -418,9 +418,9 @@ func (m *gossipRegistry) ListServices() ([]*registry.Service, error) {
return services, nil return services, nil
} }
func (m *gossipRegistry) Watch() (registry.Watcher, error) { func (m *gossipRegistry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
n, e := m.subscribe() n, e := m.subscribe()
return newGossipWatcher(n, e) return newGossipWatcher(n, e, opts...)
} }
func (m *gossipRegistry) String() string { func (m *gossipRegistry) String() string {

View File

@ -7,11 +7,17 @@ import (
) )
type gossipWatcher struct { type gossipWatcher struct {
wo registry.WatchOptions
next chan *registry.Result next chan *registry.Result
stop chan bool stop chan bool
} }
func newGossipWatcher(ch chan *registry.Result, exit chan bool) (registry.Watcher, error) { 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)
}
stop := make(chan bool) stop := make(chan bool)
go func() { go func() {
@ -20,21 +26,28 @@ func newGossipWatcher(ch chan *registry.Result, exit chan bool) (registry.Watche
}() }()
return &gossipWatcher{ return &gossipWatcher{
wo: wo,
next: ch, next: ch,
stop: stop, stop: stop,
}, nil }, nil
} }
func (m *gossipWatcher) Next() (*registry.Result, error) { func (m *gossipWatcher) Next() (*registry.Result, error) {
for {
select { select {
case r, ok := <-m.next: case r, ok := <-m.next:
if !ok { if !ok {
return nil, errors.New("result chan closed") 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 return r, nil
case <-m.stop: case <-m.stop:
return nil, errors.New("watcher stopped") return nil, errors.New("watcher stopped")
} }
}
} }
func (m *gossipWatcher) Stop() { func (m *gossipWatcher) Stop() {