2019-07-26 14:05:03 +01:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2019-07-29 18:57:40 +01:00
|
|
|
"io"
|
2019-07-26 14:05:03 +01:00
|
|
|
"sync"
|
2019-07-29 18:57:40 +01:00
|
|
|
"time"
|
2019-07-26 14:05:03 +01:00
|
|
|
|
2019-08-05 17:44:33 +01:00
|
|
|
"github.com/micro/go-micro/router"
|
2019-12-10 11:46:13 +00:00
|
|
|
pb "github.com/micro/go-micro/router/service/proto"
|
2019-07-26 14:05:03 +01:00
|
|
|
)
|
|
|
|
|
2019-07-29 18:57:40 +01:00
|
|
|
type watcher struct {
|
2019-07-29 12:52:52 +01:00
|
|
|
sync.RWMutex
|
2019-07-26 14:05:03 +01:00
|
|
|
opts router.WatchOptions
|
|
|
|
resChan chan *router.Event
|
|
|
|
done chan struct{}
|
2020-01-19 23:22:41 +00:00
|
|
|
stream pb.Router_WatchService
|
2019-07-26 14:05:03 +01:00
|
|
|
}
|
|
|
|
|
2019-07-29 18:57:40 +01:00
|
|
|
func newWatcher(rsp pb.Router_WatchService, opts router.WatchOptions) (*watcher, error) {
|
|
|
|
w := &watcher{
|
|
|
|
opts: opts,
|
|
|
|
resChan: make(chan *router.Event),
|
|
|
|
done: make(chan struct{}),
|
2020-01-19 23:22:41 +00:00
|
|
|
stream: rsp,
|
2019-07-29 18:57:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-w.done:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
if err := w.watch(rsp); err != nil {
|
|
|
|
w.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// watchRouter watches router and send events to all registered watchers
|
|
|
|
func (w *watcher) watch(stream pb.Router_WatchService) error {
|
|
|
|
var watchErr error
|
|
|
|
|
|
|
|
for {
|
|
|
|
resp, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
watchErr = err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
route := router.Route{
|
|
|
|
Service: resp.Route.Service,
|
|
|
|
Address: resp.Route.Address,
|
|
|
|
Gateway: resp.Route.Gateway,
|
|
|
|
Network: resp.Route.Network,
|
|
|
|
Link: resp.Route.Link,
|
2019-10-23 12:53:36 +01:00
|
|
|
Metric: resp.Route.Metric,
|
2019-07-29 18:57:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
event := &router.Event{
|
2020-01-23 11:44:06 +00:00
|
|
|
Id: resp.Id,
|
2019-07-29 18:57:40 +01:00
|
|
|
Type: router.EventType(resp.Type),
|
|
|
|
Timestamp: time.Unix(0, resp.Timestamp),
|
|
|
|
Route: route,
|
|
|
|
}
|
|
|
|
|
2019-09-20 16:25:29 +01:00
|
|
|
select {
|
|
|
|
case w.resChan <- event:
|
|
|
|
case <-w.done:
|
2019-07-29 18:57:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return watchErr
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:05:03 +01:00
|
|
|
// Next is a blocking call that returns watch result
|
2019-07-29 18:57:40 +01:00
|
|
|
func (w *watcher) Next() (*router.Event, error) {
|
2019-07-26 14:05:03 +01:00
|
|
|
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 event channel
|
2019-07-29 18:57:40 +01:00
|
|
|
func (w *watcher) Chan() (<-chan *router.Event, error) {
|
2019-07-26 14:05:03 +01:00
|
|
|
return w.resChan, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops watcher
|
2019-07-29 18:57:40 +01:00
|
|
|
func (w *watcher) Stop() {
|
2019-07-26 14:05:03 +01:00
|
|
|
w.Lock()
|
|
|
|
defer w.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-w.done:
|
|
|
|
return
|
|
|
|
default:
|
2020-01-19 22:55:57 +00:00
|
|
|
w.stream.Close()
|
2019-07-26 14:05:03 +01:00
|
|
|
close(w.done)
|
|
|
|
}
|
|
|
|
}
|