2019-06-21 17:17:12 +02:00
|
|
|
// Package router provides a network routing control plane
|
2019-06-06 17:37:40 +02:00
|
|
|
package router
|
|
|
|
|
2019-07-08 17:51:55 +02:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/network/router/table"
|
|
|
|
)
|
2019-06-28 12:53:55 +02:00
|
|
|
|
2019-07-10 08:45:27 +02:00
|
|
|
const (
|
2019-07-10 08:51:24 +02:00
|
|
|
// Status codes
|
2019-07-10 08:45:27 +02:00
|
|
|
// Running means the router is up and running
|
|
|
|
Running StatusCode = iota
|
|
|
|
// Stopped means the router has been stopped
|
|
|
|
Stopped
|
|
|
|
// Error means the router has encountered error
|
|
|
|
Error
|
2019-07-10 08:51:24 +02:00
|
|
|
|
|
|
|
// Advert types
|
|
|
|
// Announce is advertised when the router announces itself
|
|
|
|
Announce AdvertType = iota
|
|
|
|
// Update advertises route updates
|
|
|
|
Update
|
2019-06-27 23:52:51 +02:00
|
|
|
)
|
|
|
|
|
2019-06-21 17:17:12 +02:00
|
|
|
// Router is an interface for a routing control plane
|
2019-06-06 17:37:40 +02:00
|
|
|
type Router interface {
|
2019-07-10 08:56:18 +02:00
|
|
|
// Router provides a routing table
|
|
|
|
table.Table
|
2019-06-12 23:30:42 +02:00
|
|
|
// Init initializes the router with options
|
2019-06-06 17:37:40 +02:00
|
|
|
Init(...Option) error
|
2019-06-12 23:30:42 +02:00
|
|
|
// Options returns the router options
|
2019-06-06 17:37:40 +02:00
|
|
|
Options() Options
|
2019-07-08 17:16:50 +02:00
|
|
|
// Advertise advertises routes to the network
|
2019-07-04 03:06:59 +02:00
|
|
|
Advertise() (<-chan *Advert, error)
|
2019-07-10 08:45:27 +02:00
|
|
|
// Process processes incoming adverts
|
|
|
|
Process(*Advert) error
|
2019-06-28 19:35:53 +02:00
|
|
|
// Status returns router status
|
|
|
|
Status() Status
|
2019-06-12 23:30:42 +02:00
|
|
|
// Stop stops the router
|
2019-06-12 00:59:25 +02:00
|
|
|
Stop() error
|
2019-07-10 08:56:52 +02:00
|
|
|
// Returns the router implementation
|
2019-06-07 14:29:09 +02:00
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2019-07-03 20:50:07 +02:00
|
|
|
// Option used by the router
|
|
|
|
type Option func(*Options)
|
|
|
|
|
2019-07-09 13:46:15 +02:00
|
|
|
// AdvertType is route advertisement type
|
|
|
|
type AdvertType int
|
2019-07-03 20:50:07 +02:00
|
|
|
|
2019-07-08 17:16:50 +02:00
|
|
|
// Advert contains a list of events advertised by the router to the network
|
2019-07-04 03:06:59 +02:00
|
|
|
type Advert struct {
|
2019-07-10 08:45:27 +02:00
|
|
|
// Id is the router Id
|
|
|
|
Id string
|
2019-07-09 13:46:15 +02:00
|
|
|
// Type is type of advert
|
|
|
|
Type AdvertType
|
2019-07-03 20:50:07 +02:00
|
|
|
// Timestamp marks the time when the update is sent
|
2019-06-28 12:53:55 +02:00
|
|
|
Timestamp time.Time
|
2019-07-08 22:03:54 +02:00
|
|
|
// TTL is Advert TTL
|
2019-07-11 13:36:39 +02:00
|
|
|
TTL time.Duration
|
2019-07-08 17:51:55 +02:00
|
|
|
// Events is a list of routing table events to advertise
|
|
|
|
Events []*table.Event
|
2019-06-27 23:52:51 +02:00
|
|
|
}
|
|
|
|
|
2019-06-28 19:35:53 +02:00
|
|
|
// StatusCode defines router status
|
|
|
|
type StatusCode int
|
|
|
|
|
|
|
|
// Status is router status
|
|
|
|
type Status struct {
|
|
|
|
// Error is router error
|
|
|
|
Error error
|
|
|
|
// Code defines router status
|
|
|
|
Code StatusCode
|
|
|
|
}
|
|
|
|
|
2019-07-10 08:45:27 +02:00
|
|
|
var (
|
|
|
|
// DefaultRouter is default network router
|
|
|
|
DefaultRouter = NewRouter()
|
2019-06-28 19:35:53 +02:00
|
|
|
)
|
|
|
|
|
2019-06-06 17:37:40 +02:00
|
|
|
// NewRouter creates new Router and returns it
|
|
|
|
func NewRouter(opts ...Option) Router {
|
|
|
|
return newRouter(opts...)
|
|
|
|
}
|