gossip registry
This commit is contained in:
		
							
								
								
									
										40
									
								
								registry/gossip/watcher.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								registry/gossip/watcher.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
| package gossip | ||||
|  | ||||
| import ( | ||||
| 	"github.com/micro/go-micro/registry" | ||||
| ) | ||||
|  | ||||
| type watcher struct { | ||||
| 	id   string | ||||
| 	srv  string | ||||
| 	ch   chan *registry.Result | ||||
| 	exit chan bool | ||||
| 	fn   func() | ||||
| } | ||||
|  | ||||
| func (w *watcher) Next() (*registry.Result, error) { | ||||
| 	for { | ||||
| 		select { | ||||
| 		case r := <-w.ch: | ||||
| 			if r.Service == nil { | ||||
| 				continue | ||||
| 			} | ||||
| 			if len(w.srv) > 0 && (r.Service.Name != w.srv) { | ||||
| 				continue | ||||
| 			} | ||||
| 			return r, nil | ||||
| 		case <-w.exit: | ||||
| 			return nil, registry.ErrWatcherStopped | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (w *watcher) Stop() { | ||||
| 	select { | ||||
| 	case <-w.exit: | ||||
| 		return | ||||
| 	default: | ||||
| 		close(w.exit) | ||||
| 		w.fn() | ||||
| 	} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user