2019-10-03 11:23:56 +03:00
|
|
|
package gossip
|
|
|
|
|
|
|
|
import (
|
2021-01-30 00:30:51 +03:00
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2019-10-03 11:23:56 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type gossipWatcher struct {
|
2021-01-30 00:30:51 +03:00
|
|
|
wo register.WatchOptions
|
|
|
|
next chan *register.Result
|
2019-10-03 11:23:56 +03:00
|
|
|
stop chan bool
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
func newGossipWatcher(ch chan *register.Result, stop chan bool, opts ...register.WatchOption) (register.Watcher, error) {
|
|
|
|
var wo register.WatchOptions
|
2019-10-03 11:23:56 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&wo)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &gossipWatcher{
|
|
|
|
wo: wo,
|
|
|
|
next: ch,
|
|
|
|
stop: stop,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
func (m *gossipWatcher) Next() (*register.Result, error) {
|
2019-10-03 11:23:56 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case r, ok := <-m.next:
|
|
|
|
if !ok {
|
2021-01-30 00:30:51 +03:00
|
|
|
return nil, register.ErrWatcherStopped
|
2019-10-03 11:23:56 +03:00
|
|
|
}
|
|
|
|
// check watch options
|
|
|
|
if len(m.wo.Service) > 0 && r.Service.Name != m.wo.Service {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-30 00:30:51 +03:00
|
|
|
nr := ®ister.Result{}
|
2019-10-03 11:23:56 +03:00
|
|
|
*nr = *r
|
|
|
|
return nr, nil
|
|
|
|
case <-m.stop:
|
2021-01-30 00:30:51 +03:00
|
|
|
return nil, register.ErrWatcherStopped
|
2019-10-03 11:23:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *gossipWatcher) Stop() {
|
|
|
|
select {
|
|
|
|
case <-m.stop:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
close(m.stop)
|
|
|
|
}
|
|
|
|
}
|