2019-06-21 18:17:12 +03:00
|
|
|
// Package router provides a network routing control plane
|
2019-06-06 18:37:40 +03:00
|
|
|
package router
|
|
|
|
|
2019-07-08 18:51:55 +03:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/network/router/table"
|
|
|
|
)
|
2019-06-28 13:53:55 +03:00
|
|
|
|
2019-06-28 00:52:51 +03:00
|
|
|
var (
|
|
|
|
// DefaultRouter is default network router
|
|
|
|
DefaultRouter = NewRouter()
|
|
|
|
)
|
|
|
|
|
2019-06-21 18:17:12 +03:00
|
|
|
// Router is an interface for a routing control plane
|
2019-06-06 18:37:40 +03:00
|
|
|
type Router interface {
|
2019-06-13 00:30:42 +03:00
|
|
|
// Init initializes the router with options
|
2019-06-06 18:37:40 +03:00
|
|
|
Init(...Option) error
|
2019-06-13 00:30:42 +03:00
|
|
|
// Options returns the router options
|
2019-06-06 18:37:40 +03:00
|
|
|
Options() Options
|
2019-07-08 18:16:50 +03:00
|
|
|
// ID returns the ID of the router
|
2019-06-17 01:09:59 +03:00
|
|
|
ID() string
|
2019-06-13 14:09:49 +03:00
|
|
|
// Address returns the router adddress
|
2019-06-07 01:29:24 +03:00
|
|
|
Address() string
|
2019-06-20 15:04:58 +03:00
|
|
|
// Network returns the network address of the router
|
2019-06-07 19:20:22 +03:00
|
|
|
Network() string
|
2019-07-08 23:03:54 +03:00
|
|
|
// Table returns the routing table
|
|
|
|
Table() table.Table
|
2019-07-08 18:16:50 +03:00
|
|
|
// Advertise advertises routes to the network
|
2019-07-04 04:06:59 +03:00
|
|
|
Advertise() (<-chan *Advert, error)
|
2019-06-28 00:52:51 +03:00
|
|
|
// Update updates the routing table
|
2019-07-04 04:06:59 +03:00
|
|
|
Update(*Advert) error
|
2019-06-28 20:35:53 +03:00
|
|
|
// Status returns router status
|
|
|
|
Status() Status
|
2019-06-13 00:30:42 +03:00
|
|
|
// Stop stops the router
|
2019-06-12 01:59:25 +03:00
|
|
|
Stop() error
|
2019-06-07 15:29:09 +03:00
|
|
|
// String returns debug info
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:50:07 +03:00
|
|
|
// Option used by the router
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
// UpdateType is route advertisement update type
|
|
|
|
type UpdateType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Announce is advertised when the router announces itself
|
|
|
|
Announce UpdateType = iota
|
2019-07-04 04:06:59 +03:00
|
|
|
// Update advertises route updates
|
|
|
|
Update
|
2019-07-03 21:50:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// String returns string representation of update event
|
|
|
|
func (ut UpdateType) String() string {
|
|
|
|
switch ut {
|
|
|
|
case Announce:
|
|
|
|
return "ANNOUNCE"
|
2019-07-04 04:06:59 +03:00
|
|
|
case Update:
|
|
|
|
return "UPDATE"
|
2019-07-03 21:50:07 +03:00
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// Advert contains a list of events advertised by the router to the network
|
2019-07-04 04:06:59 +03:00
|
|
|
type Advert struct {
|
2019-06-28 20:35:53 +03:00
|
|
|
// ID is the router ID
|
2019-06-28 00:52:51 +03:00
|
|
|
ID string
|
2019-07-03 21:50:07 +03:00
|
|
|
// Timestamp marks the time when the update is sent
|
2019-06-28 13:53:55 +03:00
|
|
|
Timestamp time.Time
|
2019-07-08 23:03:54 +03:00
|
|
|
// TTL is Advert TTL
|
|
|
|
// TODO: not used
|
|
|
|
TTL time.Time
|
2019-07-08 18:51:55 +03:00
|
|
|
// Events is a list of routing table events to advertise
|
|
|
|
Events []*table.Event
|
2019-06-28 00:52:51 +03:00
|
|
|
}
|
|
|
|
|
2019-06-28 20:35:53 +03: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
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2019-07-03 21:50:07 +03:00
|
|
|
// Running means the router is up and running
|
2019-07-08 18:16:50 +03:00
|
|
|
Running StatusCode = iota
|
2019-07-03 21:50:07 +03:00
|
|
|
// Stopped means the router has been stopped
|
2019-06-28 20:35:53 +03:00
|
|
|
Stopped
|
2019-07-03 21:50:07 +03:00
|
|
|
// Error means the router has encountered error
|
|
|
|
Error
|
2019-06-28 20:35:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// String returns human readable status code
|
|
|
|
func (sc StatusCode) String() string {
|
|
|
|
switch sc {
|
|
|
|
case Running:
|
|
|
|
return "RUNNING"
|
|
|
|
case Stopped:
|
|
|
|
return "STOPPED"
|
2019-07-03 21:50:07 +03:00
|
|
|
case Error:
|
|
|
|
return "ERROR"
|
2019-06-28 20:35:53 +03:00
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 18:37:40 +03:00
|
|
|
// NewRouter creates new Router and returns it
|
|
|
|
func NewRouter(opts ...Option) Router {
|
|
|
|
return newRouter(opts...)
|
|
|
|
}
|