2019-07-08 17:51:55 +02:00
|
|
|
package table
|
2019-06-12 00:59:25 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-07-05 20:15:32 +02:00
|
|
|
"fmt"
|
2019-07-03 20:50:07 +02:00
|
|
|
"time"
|
2019-06-12 00:59:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrWatcherStopped is returned when routing table watcher has been stopped
|
2019-07-01 16:43:50 +02:00
|
|
|
ErrWatcherStopped = errors.New("watcher stopped")
|
2019-06-12 00:59:25 +02:00
|
|
|
)
|
|
|
|
|
2019-06-18 11:57:43 +02:00
|
|
|
// EventType defines routing table event
|
|
|
|
type EventType int
|
|
|
|
|
|
|
|
const (
|
2019-07-08 22:03:54 +02:00
|
|
|
// Insert is emitted when a new route has been inserted
|
|
|
|
Insert EventType = iota
|
2019-07-08 17:51:55 +02: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 11:57:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// String returns string representation of the event
|
|
|
|
func (et EventType) String() string {
|
|
|
|
switch et {
|
2019-07-08 22:03:54 +02:00
|
|
|
case Insert:
|
|
|
|
return "INSERT"
|
2019-07-08 17:51:55 +02:00
|
|
|
case Delete:
|
2019-06-18 11:57:43 +02:00
|
|
|
return "DELETE"
|
2019-07-08 17:51:55 +02:00
|
|
|
case Update:
|
2019-06-18 11:57:43 +02:00
|
|
|
return "UPDATE"
|
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event is returned by a call to Next on the watcher.
|
|
|
|
type Event struct {
|
|
|
|
// Type defines type of event
|
|
|
|
Type EventType
|
2019-07-03 20:50:07 +02:00
|
|
|
// Timestamp is event timestamp
|
|
|
|
Timestamp time.Time
|
2019-07-05 20:15:32 +02:00
|
|
|
// Route is table route
|
2019-06-18 11:57:43 +02:00
|
|
|
Route Route
|
|
|
|
}
|
|
|
|
|
2019-07-05 20:15:32 +02:00
|
|
|
// String prints human readable Event
|
|
|
|
func (e Event) String() string {
|
2019-07-09 16:45:42 +02:00
|
|
|
return fmt.Sprintf("[EVENT] time: %s type: %s", e.Timestamp, e.Type)
|
2019-07-05 20:15:32 +02:00
|
|
|
}
|
|
|
|
|
2019-06-13 13:09:49 +02:00
|
|
|
// WatchOption is used to define what routes to watch in the table
|
|
|
|
type WatchOption func(*WatchOptions)
|
|
|
|
|
|
|
|
// Watcher defines routing table watcher interface
|
|
|
|
// Watcher returns updates to the routing table
|
2019-06-12 00:59:25 +02:00
|
|
|
type Watcher interface {
|
|
|
|
// Next is a blocking call that returns watch result
|
2019-06-18 11:57:43 +02:00
|
|
|
Next() (*Event, error)
|
2019-06-19 22:22:14 +02:00
|
|
|
// Chan returns event channel
|
|
|
|
Chan() (<-chan *Event, error)
|
2019-06-12 00:59:25 +02:00
|
|
|
// Stop stops watcher
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
// WatchOptions are table watcher options
|
2019-06-12 00:59:25 +02:00
|
|
|
type WatchOptions struct {
|
2019-07-09 16:45:42 +02:00
|
|
|
// Service allows to watch specific service routes
|
|
|
|
Service string
|
2019-06-12 00:59:25 +02:00
|
|
|
}
|
|
|
|
|
2019-07-09 16:45:42 +02:00
|
|
|
// WatchService sets what service routes to watch
|
|
|
|
// Service is the microservice name
|
|
|
|
func WatchService(s string) WatchOption {
|
2019-06-12 00:59:25 +02:00
|
|
|
return func(o *WatchOptions) {
|
2019-07-09 16:45:42 +02:00
|
|
|
o.Service = s
|
2019-06-12 00:59:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type tableWatcher struct {
|
|
|
|
opts WatchOptions
|
2019-06-18 11:57:43 +02:00
|
|
|
resChan chan *Event
|
2019-06-12 00:59:25 +02:00
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next noticed action taken on table
|
2019-07-03 20:50:07 +02:00
|
|
|
// TODO: this needs to be thought through properly;
|
|
|
|
// right now we only allow to watch destination
|
2019-06-18 11:57:43 +02:00
|
|
|
func (w *tableWatcher) Next() (*Event, error) {
|
2019-06-12 00:59:25 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case res := <-w.resChan:
|
2019-07-09 16:45:42 +02:00
|
|
|
switch w.opts.Service {
|
|
|
|
case res.Route.Service, "*":
|
2019-06-26 17:03:19 +02:00
|
|
|
return res, nil
|
|
|
|
default:
|
2019-06-27 23:52:51 +02:00
|
|
|
continue
|
2019-06-12 00:59:25 +02:00
|
|
|
}
|
|
|
|
case <-w.done:
|
|
|
|
return nil, ErrWatcherStopped
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 22:22:14 +02:00
|
|
|
// Chan returns watcher events channel
|
|
|
|
func (w *tableWatcher) Chan() (<-chan *Event, error) {
|
|
|
|
return w.resChan, nil
|
|
|
|
}
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
// Stop stops routing table watcher
|
|
|
|
func (w *tableWatcher) Stop() {
|
|
|
|
select {
|
|
|
|
case <-w.done:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
close(w.done)
|
|
|
|
}
|
|
|
|
}
|
2019-06-19 19:01:48 +02:00
|
|
|
|
|
|
|
// String prints debug information
|
2019-07-08 22:03:54 +02:00
|
|
|
func (w tableWatcher) String() string {
|
2019-07-09 17:17:18 +02:00
|
|
|
return "watcher"
|
2019-06-19 19:01:48 +02:00
|
|
|
}
|