2019-09-09 17:57:57 +02:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2020-01-30 13:39:00 +02:00
|
|
|
"github.com/micro/go-micro/v2/registry"
|
|
|
|
pb "github.com/micro/go-micro/v2/registry/service/proto"
|
2019-09-09 17:57:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type serviceWatcher struct {
|
|
|
|
stream pb.Registry_WatchService
|
|
|
|
closed chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serviceWatcher) Next() (*registry.Result, error) {
|
2019-12-03 09:25:58 +02:00
|
|
|
// check if closed
|
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
return nil, registry.ErrWatcherStopped
|
|
|
|
default:
|
|
|
|
}
|
2019-09-09 17:57:57 +02:00
|
|
|
|
2019-12-03 09:25:58 +02:00
|
|
|
r, err := s.stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-09-09 17:57:57 +02:00
|
|
|
}
|
2019-12-03 09:25:58 +02:00
|
|
|
|
|
|
|
return ®istry.Result{
|
|
|
|
Action: r.Action,
|
|
|
|
Service: ToService(r.Service),
|
|
|
|
}, nil
|
2019-09-09 17:57:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|