2021-01-29 15:08:17 +03:00
|
|
|
package register
|
2020-07-27 15:22:00 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2021-10-27 01:09:40 +03:00
|
|
|
"go.unistack.org/micro/v3/router"
|
2020-07-27 15:22:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// tableWatcher implements routing table Watcher
|
|
|
|
type tableWatcher struct {
|
|
|
|
sync.RWMutex
|
|
|
|
id string
|
|
|
|
opts router.WatchOptions
|
|
|
|
resChan chan *router.Event
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next noticed action taken on table
|
|
|
|
// TODO: right now we only allow to watch particular service
|
|
|
|
func (w *tableWatcher) Next() (*router.Event, error) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case res := <-w.resChan:
|
|
|
|
switch w.opts.Service {
|
|
|
|
case res.Route.Service, "*":
|
|
|
|
return res, nil
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case <-w.done:
|
|
|
|
return nil, router.ErrWatcherStopped
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chan returns watcher events channel
|
|
|
|
func (w *tableWatcher) Chan() (<-chan *router.Event, error) {
|
|
|
|
return w.resChan, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops routing table watcher
|
|
|
|
func (w *tableWatcher) Stop() {
|
|
|
|
w.Lock()
|
|
|
|
defer w.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-w.done:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
close(w.done)
|
|
|
|
}
|
|
|
|
}
|