2019-07-25 23:19:05 +01:00
|
|
|
package router
|
2019-06-11 23:59:25 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-07-26 14:05:03 +01:00
|
|
|
"sync"
|
2019-07-03 19:50:07 +01:00
|
|
|
"time"
|
2019-06-11 23:59:25 +01:00
|
|
|
)
|
|
|
|
|
2019-08-27 23:08:35 +01:00
|
|
|
var (
|
|
|
|
// ErrWatcherStopped is returned when routing table watcher has been stopped
|
|
|
|
ErrWatcherStopped = errors.New("watcher stopped")
|
|
|
|
)
|
|
|
|
|
2019-06-18 10:57:43 +01:00
|
|
|
// EventType defines routing table event
|
|
|
|
type EventType int
|
|
|
|
|
|
|
|
const (
|
2019-07-10 17:46:22 +01:00
|
|
|
// Create is emitted when a new route has been created
|
|
|
|
Create EventType = iota
|
2019-07-08 16:51:55 +01:00
|
|
|
// Delete is emitted when an existing route has been deleted
|
|
|
|
Delete
|
|
|
|
// Update is emitted when an existing route has been updated
|
|
|
|
Update
|
2019-06-18 10:57:43 +01:00
|
|
|
)
|
|
|
|
|
2019-07-25 23:19:05 +01:00
|
|
|
// String returns human readable event type
|
|
|
|
func (t EventType) String() string {
|
|
|
|
switch t {
|
2019-07-11 12:36:39 +01:00
|
|
|
case Create:
|
|
|
|
return "create"
|
|
|
|
case Delete:
|
|
|
|
return "delete"
|
|
|
|
case Update:
|
|
|
|
return "update"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 10:57:43 +01:00
|
|
|
// Event is returned by a call to Next on the watcher.
|
|
|
|
type Event struct {
|
|
|
|
// Type defines type of event
|
|
|
|
Type EventType
|
2019-07-03 19:50:07 +01:00
|
|
|
// Timestamp is event timestamp
|
|
|
|
Timestamp time.Time
|
2019-07-05 19:15:32 +01:00
|
|
|
// Route is table route
|
2019-06-18 10:57:43 +01:00
|
|
|
Route Route
|
|
|
|
}
|
|
|
|
|
2019-06-13 12:09:49 +01:00
|
|
|
// Watcher defines routing table watcher interface
|
|
|
|
// Watcher returns updates to the routing table
|
2019-06-11 23:59:25 +01:00
|
|
|
type Watcher interface {
|
|
|
|
// Next is a blocking call that returns watch result
|
2019-06-18 10:57:43 +01:00
|
|
|
Next() (*Event, error)
|
2019-06-19 21:22:14 +01:00
|
|
|
// Chan returns event channel
|
|
|
|
Chan() (<-chan *Event, error)
|
2019-06-11 23:59:25 +01:00
|
|
|
// Stop stops watcher
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
2019-08-27 23:08:35 +01:00
|
|
|
// WatchOption is used to define what routes to watch in the table
|
|
|
|
type WatchOption func(*WatchOptions)
|
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
// WatchOptions are table watcher options
|
2019-08-27 23:08:35 +01:00
|
|
|
// TODO: expand the options to watch based on other criteria
|
2019-06-11 23:59:25 +01:00
|
|
|
type WatchOptions struct {
|
2019-07-09 15:45:42 +01:00
|
|
|
// Service allows to watch specific service routes
|
|
|
|
Service string
|
2019-06-11 23:59:25 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 15:45:42 +01:00
|
|
|
// WatchService sets what service routes to watch
|
|
|
|
// Service is the microservice name
|
|
|
|
func WatchService(s string) WatchOption {
|
2019-06-11 23:59:25 +01:00
|
|
|
return func(o *WatchOptions) {
|
2019-07-09 15:45:42 +01:00
|
|
|
o.Service = s
|
2019-06-11 23:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 23:08:35 +01:00
|
|
|
// tableWatcher implements routing table Watcher
|
2019-06-11 23:59:25 +01:00
|
|
|
type tableWatcher struct {
|
2019-07-29 12:44:28 +01:00
|
|
|
sync.RWMutex
|
2019-08-02 14:59:08 +01:00
|
|
|
id string
|
2019-06-11 23:59:25 +01:00
|
|
|
opts WatchOptions
|
2019-06-18 10:57:43 +01:00
|
|
|
resChan chan *Event
|
2019-06-11 23:59:25 +01:00
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next noticed action taken on table
|
2019-07-26 14:05:03 +01:00
|
|
|
// TODO: right now we only allow to watch particular service
|
2019-06-18 10:57:43 +01:00
|
|
|
func (w *tableWatcher) Next() (*Event, error) {
|
2019-06-11 23:59:25 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case res := <-w.resChan:
|
2019-07-09 15:45:42 +01:00
|
|
|
switch w.opts.Service {
|
|
|
|
case res.Route.Service, "*":
|
2019-06-26 16:03:19 +01:00
|
|
|
return res, nil
|
|
|
|
default:
|
2019-06-27 22:52:51 +01:00
|
|
|
continue
|
2019-06-11 23:59:25 +01:00
|
|
|
}
|
|
|
|
case <-w.done:
|
|
|
|
return nil, ErrWatcherStopped
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:22:14 +01:00
|
|
|
// Chan returns watcher events channel
|
|
|
|
func (w *tableWatcher) Chan() (<-chan *Event, error) {
|
|
|
|
return w.resChan, nil
|
|
|
|
}
|
|
|
|
|
2019-06-11 23:59:25 +01:00
|
|
|
// Stop stops routing table watcher
|
|
|
|
func (w *tableWatcher) Stop() {
|
2019-07-26 14:05:03 +01:00
|
|
|
w.Lock()
|
|
|
|
defer w.Unlock()
|
|
|
|
|
2019-06-11 23:59:25 +01:00
|
|
|
select {
|
|
|
|
case <-w.done:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
close(w.done)
|
|
|
|
}
|
|
|
|
}
|