gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/registry/service/watcher.go (about)

     1  package service
     2  
     3  import (
     4  	"gitee.com/liuxuezhan/go-micro-v1.18.0/registry"
     5  	pb "gitee.com/liuxuezhan/go-micro-v1.18.0/registry/service/proto"
     6  )
     7  
     8  type serviceWatcher struct {
     9  	stream pb.Registry_WatchService
    10  	closed chan bool
    11  }
    12  
    13  func (s *serviceWatcher) Next() (*registry.Result, error) {
    14  	// check if closed
    15  	select {
    16  	case <-s.closed:
    17  		return nil, registry.ErrWatcherStopped
    18  	default:
    19  	}
    20  
    21  	r, err := s.stream.Recv()
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	return &registry.Result{
    27  		Action:  r.Action,
    28  		Service: ToService(r.Service),
    29  	}, nil
    30  }
    31  
    32  func (s *serviceWatcher) Stop() {
    33  	select {
    34  	case <-s.closed:
    35  		return
    36  	default:
    37  		close(s.closed)
    38  		s.stream.Close()
    39  	}
    40  }
    41  
    42  func newWatcher(stream pb.Registry_WatchService) registry.Watcher {
    43  	return &serviceWatcher{
    44  		stream: stream,
    45  		closed: make(chan bool),
    46  	}
    47  }