2019-06-21 16:17:12 +01:00
|
|
|
// Package router provides a network routing control plane
|
2019-06-06 16:37:40 +01:00
|
|
|
package router
|
|
|
|
|
2019-07-08 16:51:55 +01:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
2019-06-28 11:53:55 +01:00
|
|
|
|
2019-08-20 12:48:51 +01:00
|
|
|
var (
|
|
|
|
// DefaultAddress is default router address
|
|
|
|
DefaultAddress = ":9093"
|
|
|
|
// DefaultName is default router service name
|
|
|
|
DefaultName = "go.micro.router"
|
|
|
|
// DefaultNetwork is default micro network
|
|
|
|
DefaultNetwork = "go.micro"
|
|
|
|
// DefaultRouter is default network router
|
|
|
|
DefaultRouter = NewRouter()
|
|
|
|
)
|
|
|
|
|
2019-06-21 16:17:12 +01:00
|
|
|
// Router is an interface for a routing control plane
|
2019-06-06 16:37:40 +01:00
|
|
|
type Router interface {
|
2019-06-12 22:30:42 +01:00
|
|
|
// Init initializes the router with options
|
2019-06-06 16:37:40 +01:00
|
|
|
Init(...Option) error
|
2019-06-12 22:30:42 +01:00
|
|
|
// Options returns the router options
|
2019-06-06 16:37:40 +01:00
|
|
|
Options() Options
|
2019-07-29 18:57:40 +01:00
|
|
|
// The routing table
|
|
|
|
Table() Table
|
2019-12-17 08:28:45 +00:00
|
|
|
// Advertise advertises routes
|
2019-07-04 02:06:59 +01:00
|
|
|
Advertise() (<-chan *Advert, error)
|
2019-07-10 07:45:27 +01:00
|
|
|
// Process processes incoming adverts
|
|
|
|
Process(*Advert) error
|
2019-07-25 23:52:54 +01:00
|
|
|
// Lookup queries routes in the routing table
|
2019-10-09 17:13:52 +01:00
|
|
|
Lookup(...QueryOption) ([]Route, error)
|
2019-07-25 23:52:54 +01:00
|
|
|
// Watch returns a watcher which tracks updates to the routing table
|
2019-07-25 23:19:05 +01:00
|
|
|
Watch(opts ...WatchOption) (Watcher, error)
|
2019-08-12 18:18:17 +01:00
|
|
|
// Start starts the router
|
|
|
|
Start() error
|
2019-06-28 18:35:53 +01:00
|
|
|
// Status returns router status
|
|
|
|
Status() Status
|
2019-06-12 22:30:42 +01:00
|
|
|
// Stop stops the router
|
2019-06-11 23:59:25 +01:00
|
|
|
Stop() error
|
2019-07-10 07:56:52 +01:00
|
|
|
// Returns the router implementation
|
2019-06-07 13:29:09 +01:00
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2019-08-20 21:11:27 +01:00
|
|
|
// Table is an interface for routing table
|
2019-07-29 18:57:40 +01:00
|
|
|
type Table interface {
|
|
|
|
// Create new route in the routing table
|
|
|
|
Create(Route) error
|
2019-08-20 21:11:27 +01:00
|
|
|
// Delete existing route from the routing table
|
2019-07-29 18:57:40 +01:00
|
|
|
Delete(Route) error
|
2019-08-20 21:11:27 +01:00
|
|
|
// Update route in the routing table
|
2019-07-29 18:57:40 +01:00
|
|
|
Update(Route) error
|
2019-08-20 21:11:27 +01:00
|
|
|
// List all routes in the table
|
2019-07-29 18:57:40 +01:00
|
|
|
List() ([]Route, error)
|
2019-08-20 21:11:27 +01:00
|
|
|
// Query routes in the routing table
|
2019-10-09 17:13:52 +01:00
|
|
|
Query(...QueryOption) ([]Route, error)
|
2019-07-29 18:57:40 +01:00
|
|
|
}
|
|
|
|
|
2019-07-03 19:50:07 +01:00
|
|
|
// Option used by the router
|
|
|
|
type Option func(*Options)
|
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
// StatusCode defines router status
|
|
|
|
type StatusCode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Running means the router is up and running
|
|
|
|
Running StatusCode = iota
|
|
|
|
// Advertising means the router is advertising
|
|
|
|
Advertising
|
|
|
|
// Stopped means the router has been stopped
|
|
|
|
Stopped
|
|
|
|
// Error means the router has encountered error
|
|
|
|
Error
|
|
|
|
)
|
|
|
|
|
2019-07-27 16:01:30 +01:00
|
|
|
func (s StatusCode) String() string {
|
|
|
|
switch s {
|
|
|
|
case Running:
|
|
|
|
return "running"
|
|
|
|
case Advertising:
|
|
|
|
return "advertising"
|
|
|
|
case Stopped:
|
|
|
|
return "stopped"
|
|
|
|
case Error:
|
|
|
|
return "error"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
// Status is router status
|
|
|
|
type Status struct {
|
|
|
|
// Code defines router status
|
|
|
|
Code StatusCode
|
2019-08-12 18:18:17 +01:00
|
|
|
// Error contains error description
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns human readable status
|
|
|
|
func (s Status) String() string {
|
|
|
|
return s.Code.String()
|
2019-07-24 17:16:52 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 12:46:15 +01:00
|
|
|
// AdvertType is route advertisement type
|
|
|
|
type AdvertType int
|
2019-07-03 19:50:07 +01:00
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
const (
|
|
|
|
// Announce is advertised when the router announces itself
|
|
|
|
Announce AdvertType = iota
|
2019-07-25 23:19:05 +01:00
|
|
|
// RouteUpdate advertises route updates
|
|
|
|
RouteUpdate
|
2019-07-24 17:16:52 +01:00
|
|
|
)
|
|
|
|
|
2019-07-25 23:19:05 +01:00
|
|
|
// String returns human readable advertisement type
|
|
|
|
func (t AdvertType) String() string {
|
|
|
|
switch t {
|
|
|
|
case Announce:
|
|
|
|
return "announce"
|
|
|
|
case RouteUpdate:
|
|
|
|
return "update"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 16:16:50 +01:00
|
|
|
// Advert contains a list of events advertised by the router to the network
|
2019-07-04 02:06:59 +01:00
|
|
|
type Advert struct {
|
2019-07-10 07:45:27 +01:00
|
|
|
// Id is the router Id
|
|
|
|
Id string
|
2019-07-09 12:46:15 +01:00
|
|
|
// Type is type of advert
|
|
|
|
Type AdvertType
|
2019-07-03 19:50:07 +01:00
|
|
|
// Timestamp marks the time when the update is sent
|
2019-06-28 11:53:55 +01:00
|
|
|
Timestamp time.Time
|
2019-07-08 21:03:54 +01:00
|
|
|
// TTL is Advert TTL
|
2019-07-11 12:36:39 +01:00
|
|
|
TTL time.Duration
|
2019-07-08 16:51:55 +01:00
|
|
|
// Events is a list of routing table events to advertise
|
2019-07-25 23:19:05 +01:00
|
|
|
Events []*Event
|
2019-06-27 22:52:51 +01:00
|
|
|
}
|
|
|
|
|
2019-10-09 16:03:06 +01:00
|
|
|
// Strategy is route advertisement strategy
|
|
|
|
type Strategy int
|
|
|
|
|
2020-01-16 12:48:36 +00:00
|
|
|
// TODO: remove the "Advertise" prefix from these
|
2019-10-09 16:03:06 +01:00
|
|
|
const (
|
2019-10-09 19:08:24 +01:00
|
|
|
// AdvertiseAll advertises all routes to the network
|
|
|
|
AdvertiseAll Strategy = iota
|
|
|
|
// AdvertiseBest advertises optimal routes to the network
|
|
|
|
AdvertiseBest
|
2019-11-11 15:28:37 +00:00
|
|
|
// AdvertiseLocal will only advertise the local routes
|
|
|
|
AdvertiseLocal
|
|
|
|
// AdvertiseNone will not advertise any routes
|
|
|
|
AdvertiseNone
|
2019-10-09 16:03:06 +01:00
|
|
|
)
|
|
|
|
|
2019-10-09 17:23:02 +01:00
|
|
|
// String returns human readable Strategy
|
|
|
|
func (s Strategy) String() string {
|
|
|
|
switch s {
|
2019-10-09 19:08:24 +01:00
|
|
|
case AdvertiseAll:
|
2019-10-09 17:23:02 +01:00
|
|
|
return "all"
|
2019-10-09 19:08:24 +01:00
|
|
|
case AdvertiseBest:
|
|
|
|
return "best"
|
2019-11-11 15:28:37 +00:00
|
|
|
case AdvertiseLocal:
|
|
|
|
return "local"
|
|
|
|
case AdvertiseNone:
|
|
|
|
return "none"
|
2019-10-09 17:23:02 +01:00
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 16:37:40 +01:00
|
|
|
// NewRouter creates new Router and returns it
|
|
|
|
func NewRouter(opts ...Option) Router {
|
|
|
|
return newRouter(opts...)
|
|
|
|
}
|