mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-18 08:26:38 +02:00
f23638c036
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
48 lines
798 B
Go
48 lines
798 B
Go
package service
|
|
|
|
import (
|
|
"github.com/micro/go-micro/v2/registry"
|
|
pb "github.com/micro/go-micro/v2/registry/service/proto"
|
|
)
|
|
|
|
type serviceWatcher struct {
|
|
stream pb.Registry_WatchService
|
|
closed chan bool
|
|
}
|
|
|
|
func (s *serviceWatcher) Next() (*registry.Result, error) {
|
|
// 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,
|
|
Service: ToService(r.Service),
|
|
}, 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),
|
|
}
|
|
}
|