2019-09-09 08:57:57 -07:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/micro/go-micro/registry"
|
2019-10-13 12:23:13 +01:00
|
|
|
pb "github.com/micro/go-micro/registry/service/proto"
|
2019-09-09 08:57:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type serviceWatcher struct {
|
|
|
|
stream pb.Registry_WatchService
|
|
|
|
closed chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serviceWatcher) Next() (*registry.Result, error) {
|
|
|
|
for {
|
|
|
|
// check if closed
|
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
return nil, registry.ErrWatcherStopped
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := s.stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ®istry.Result{
|
|
|
|
Action: r.Action,
|
2019-09-09 09:20:17 -07:00
|
|
|
Service: ToService(r.Service),
|
2019-09-09 08:57:57 -07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serviceWatcher) Stop() {
|
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
close(s.closed)
|
|
|
|
s.stream.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWatcher(stream pb.Registry_WatchService) registry.Watcher {
|
|
|
|
return &serviceWatcher{
|
|
|
|
stream: stream,
|
|
|
|
closed: make(chan bool),
|
|
|
|
}
|
|
|
|
}
|