2019-07-26 01:19:05 +03:00
|
|
|
package router
|
2019-06-12 01:59:25 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-07-03 21:50:07 +03:00
|
|
|
"time"
|
2019-06-12 01:59:25 +03:00
|
|
|
)
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
// ErrWatcherStopped is returned when routing table watcher has been stopped
|
|
|
|
var ErrWatcherStopped = errors.New("watcher stopped")
|
2019-08-28 01:08:35 +03:00
|
|
|
|
2019-06-18 12:57:43 +03:00
|
|
|
// EventType defines routing table event
|
|
|
|
type EventType int
|
|
|
|
|
|
|
|
const (
|
2019-07-10 19:46:22 +03:00
|
|
|
// Create is emitted when a new route has been created
|
|
|
|
Create EventType = iota
|
2019-07-08 18:51:55 +03: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 12:57:43 +03:00
|
|
|
)
|
|
|
|
|
2019-07-26 01:19:05 +03:00
|
|
|
// String returns human readable event type
|
|
|
|
func (t EventType) String() string {
|
|
|
|
switch t {
|
2019-07-11 14:36:39 +03:00
|
|
|
case Create:
|
|
|
|
return "create"
|
|
|
|
case Delete:
|
|
|
|
return "delete"
|
|
|
|
case Update:
|
|
|
|
return "update"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 12:57:43 +03:00
|
|
|
// Event is returned by a call to Next on the watcher.
|
|
|
|
type Event struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Timestamp is event timestamp
|
|
|
|
Timestamp time.Time
|
2021-09-30 21:00:02 +03:00
|
|
|
// ID of the event
|
|
|
|
ID string
|
2021-04-25 11:58:24 +03:00
|
|
|
// Route is table route
|
|
|
|
Route Route
|
2019-06-18 12:57:43 +03:00
|
|
|
// Type defines type of event
|
|
|
|
Type EventType
|
|
|
|
}
|
|
|
|
|
2019-06-13 14:09:49 +03:00
|
|
|
// Watcher defines routing table watcher interface
|
|
|
|
// Watcher returns updates to the routing table
|
2019-06-12 01:59:25 +03:00
|
|
|
type Watcher interface {
|
|
|
|
// Next is a blocking call that returns watch result
|
2019-06-18 12:57:43 +03:00
|
|
|
Next() (*Event, error)
|
2019-06-19 23:22:14 +03:00
|
|
|
// Chan returns event channel
|
|
|
|
Chan() (<-chan *Event, error)
|
2019-06-12 01:59:25 +03:00
|
|
|
// Stop stops watcher
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
// WatchOption is used to define what routes to watch in the table
|
|
|
|
type WatchOption func(*WatchOptions)
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
// WatchOptions are table watcher options
|
2019-08-28 01:08:35 +03:00
|
|
|
// TODO: expand the options to watch based on other criteria
|
2019-06-12 01:59:25 +03:00
|
|
|
type WatchOptions struct {
|
2019-07-09 17:45:42 +03:00
|
|
|
// Service allows to watch specific service routes
|
|
|
|
Service string
|
2019-06-12 01:59:25 +03:00
|
|
|
}
|
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
// WatchService sets what service routes to watch
|
|
|
|
// Service is the microservice name
|
|
|
|
func WatchService(s string) WatchOption {
|
2019-06-12 01:59:25 +03:00
|
|
|
return func(o *WatchOptions) {
|
2019-07-09 17:45:42 +03:00
|
|
|
o.Service = s
|
2019-06-12 01:59:25 +03:00
|
|
|
}
|
|
|
|
}
|