Renamed variables, options and functions

This commit is contained in:
Milos Gajdos 2019-06-19 21:22:14 +01:00
parent 4e5fbbf7eb
commit 10a3636a9f
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F
8 changed files with 158 additions and 260 deletions

View File

@ -12,12 +12,12 @@ import (
) )
var ( var (
// AdvertiseToNetworkTick defines how often in seconds do we scal the local registry // AdvertiseTick defines how often in seconds do we scal the local registry
// to advertise the local services to the network registry // to advertise the local services to the network registry
AdvertiseToNetworkTick = 5 * time.Second AdvertiseTick = 5 * time.Second
// AdvertiseNetworkTTL defines network registry TTL in seconds // AdvertiseTTL defines network registry TTL in seconds
// NOTE: this is a rather arbitrary picked value subject to change // NOTE: this is a rather arbitrary picked value subject to change
AdvertiseNetworkTTL = 120 * time.Second AdvertiseTTL = 120 * time.Second
) )
type router struct { type router struct {
@ -71,25 +71,20 @@ func (r *router) Address() string {
return r.opts.Address return r.opts.Address
} }
// Gossip returns gossip bind address // Network returns the address router advertises to the network
func (r *router) Gossip() string {
return r.opts.GossipAddress
}
// Network returns router's micro network
func (r *router) Network() string { func (r *router) Network() string {
return r.opts.NetworkAddress return r.opts.Advertise
} }
// Start starts the router // Start starts the router
func (r *router) Start() error { func (r *router) Start() error {
// add local service routes into the routing table // add local service routes into the routing table
if err := r.addServiceRoutes(r.opts.LocalRegistry, r.opts.Address, DefaultLocalMetric); err != nil { if err := r.addServiceRoutes(r.opts.Registry, DefaultLocalMetric); err != nil {
return fmt.Errorf("failed adding routes for local services: %v", err) return fmt.Errorf("failed adding routes for local services: %v", err)
} }
// add network service routes into the routing table // add network service routes into the routing table
if err := r.addServiceRoutes(r.opts.NetworkRegistry, r.opts.NetworkAddress, DefaultNetworkMetric); err != nil { if err := r.addServiceRoutes(r.opts.Network, DefaultNetworkMetric); err != nil {
return fmt.Errorf("failed adding routes for network services: %v", err) return fmt.Errorf("failed adding routes for network services: %v", err)
} }
@ -98,12 +93,12 @@ func (r *router) Start() error {
return fmt.Errorf("failed to parse router into service node: %v", err) return fmt.Errorf("failed to parse router into service node: %v", err)
} }
localRegWatcher, err := r.opts.LocalRegistry.Watch() localWatcher, err := r.opts.Registry.Watch()
if err != nil { if err != nil {
return fmt.Errorf("failed to create local registry watcher: %v", err) return fmt.Errorf("failed to create local registry watcher: %v", err)
} }
networkRegWatcher, err := r.opts.NetworkRegistry.Watch() networkWatcher, err := r.opts.Network.Watch()
if err != nil { if err != nil {
return fmt.Errorf("failed to create network registry watcher: %v", err) return fmt.Errorf("failed to create network registry watcher: %v", err)
} }
@ -115,14 +110,14 @@ func (r *router) Start() error {
go func() { go func() {
defer r.wg.Done() defer r.wg.Done()
// watch local registry and register routes in routine table // watch local registry and register routes in routine table
errChan <- r.manageServiceRoutes(localRegWatcher, r.opts.Address, DefaultLocalMetric) errChan <- r.manageServiceRoutes(localWatcher, DefaultLocalMetric)
}() }()
r.wg.Add(1) r.wg.Add(1)
go func() { go func() {
defer r.wg.Done() defer r.wg.Done()
// watch network registry and register routes in routine table // watch network registry and register routes in routine table
errChan <- r.manageServiceRoutes(networkRegWatcher, r.opts.NetworkAddress, DefaultNetworkMetric) errChan <- r.manageServiceRoutes(networkWatcher, DefaultNetworkMetric)
}() }()
r.wg.Add(1) r.wg.Add(1)
@ -138,19 +133,19 @@ func (r *router) Start() error {
// addServiceRouteslists all available services in given registry and adds them to the routing table. // addServiceRouteslists all available services in given registry and adds them to the routing table.
// NOTE: this is a one-off operation done when bootstrapping the routing table of the new router. // NOTE: this is a one-off operation done when bootstrapping the routing table of the new router.
// It returns error if either the services could not be listed or if the routes could not be added to the routing table. // It returns error if either the services could not be listed or if the routes could not be added to the routing table.
func (r *router) addServiceRoutes(reg registry.Registry, network string, metric int) error { func (r *router) addServiceRoutes(reg registry.Registry, metric int) error {
services, err := reg.ListServices() services, err := reg.ListServices()
if err != nil { if err != nil {
return fmt.Errorf("failed to list services: %v", err) return fmt.Errorf("failed to list services: %v", err)
} }
for _, service := range services { for _, service := range services {
route := NewRoute( route := Route{
DestAddr(service.Name), Destination: service.Name,
Gateway(r), Router: r,
Network(network), Network: r.opts.Advertise,
Metric(metric), Metric: metric,
) }
if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute { if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute {
return fmt.Errorf("error adding route for service: %s", service.Name) return fmt.Errorf("error adding route for service: %s", service.Name)
} }
@ -160,15 +155,15 @@ func (r *router) addServiceRoutes(reg registry.Registry, network string, metric
} }
// parseToNode parses router into registry.Node and returns the result. // parseToNode parses router into registry.Node and returns the result.
// It returns error if the router network address could not be parsed into service host and port. // It returns error if the router network address could not be parsed into host and port.
// NOTE: We use ":" as the default delimiter when we split the network address. // NOTE: We use ":" as the delimiter when we splitting the router network address.
func (r *router) parseToNode() (*registry.Node, error) { func (r *router) parseToNode() (*registry.Node, error) {
// split on ":" as a standard host/port delimiter // split on ":" as a standard host/port delimiter
addr := strings.Split(r.opts.NetworkAddress, ":") addr := strings.Split(r.opts.Advertise, ":")
// try to parse network port into integer // try to parse network port into integer
port, err := strconv.Atoi(addr[1]) port, err := strconv.Atoi(addr[1])
if err != nil { if err != nil {
return nil, fmt.Errorf("could not parse router network address from %s: %v", r.opts.NetworkAddress, err) return nil, fmt.Errorf("could not parse router network address: %v", err)
} }
node := &registry.Node{ node := &registry.Node{
@ -184,7 +179,7 @@ func (r *router) parseToNode() (*registry.Node, error) {
// It returns error if either the local services failed to be listed or if it fails to register local service in network registry. // It returns error if either the local services failed to be listed or if it fails to register local service in network registry.
func (r *router) advertiseToNetwork(node *registry.Node) error { func (r *router) advertiseToNetwork(node *registry.Node) error {
// ticker to periodically scan the local registry // ticker to periodically scan the local registry
ticker := time.NewTicker(AdvertiseToNetworkTick) ticker := time.NewTicker(AdvertiseTick)
for { for {
select { select {
@ -192,7 +187,7 @@ func (r *router) advertiseToNetwork(node *registry.Node) error {
return nil return nil
case <-ticker.C: case <-ticker.C:
// list all local services // list all local services
services, err := r.opts.LocalRegistry.ListServices() services, err := r.opts.Registry.ListServices()
if err != nil { if err != nil {
return fmt.Errorf("failed to list local services: %v", err) return fmt.Errorf("failed to list local services: %v", err)
} }
@ -203,7 +198,7 @@ func (r *router) advertiseToNetwork(node *registry.Node) error {
Nodes: []*registry.Node{node}, Nodes: []*registry.Node{node},
} }
// register the local service in the network registry // register the local service in the network registry
if err := r.opts.NetworkRegistry.Register(svc, registry.RegisterTTL(AdvertiseNetworkTTL)); err != nil { if err := r.opts.Network.Register(svc, registry.RegisterTTL(AdvertiseTTL)); err != nil {
return fmt.Errorf("failed to register service %s in network registry: %v", svc.Name, err) return fmt.Errorf("failed to register service %s in network registry: %v", svc.Name, err)
} }
} }
@ -213,7 +208,7 @@ func (r *router) advertiseToNetwork(node *registry.Node) error {
// manageServiceRoutes watches services in given registry and updates the routing table accordingly. // manageServiceRoutes watches services in given registry and updates the routing table accordingly.
// It returns error if the service registry watcher has stopped or if the routing table failed to be updated. // It returns error if the service registry watcher has stopped or if the routing table failed to be updated.
func (r *router) manageServiceRoutes(w registry.Watcher, network string, metric int) error { func (r *router) manageServiceRoutes(w registry.Watcher, metric int) error {
// wait in the background for the router to stop // wait in the background for the router to stop
// when the router stops, stop the watcher and exit // when the router stops, stop the watcher and exit
r.wg.Add(1) r.wg.Add(1)
@ -236,17 +231,17 @@ func (r *router) manageServiceRoutes(w registry.Watcher, network string, metric
break break
} }
route := NewRoute( route := Route{
DestAddr(res.Service.Name), Destination: res.Service.Name,
Gateway(r), Router: r,
Network(network), Network: r.opts.Advertise,
Metric(metric), Metric: metric,
) }
switch res.Action { switch res.Action {
case "create": case "create":
if len(res.Service.Nodes) > 0 { if len(res.Service.Nodes) > 0 {
/// only return error if the route is not duplicate, but something else has failed // only return error if the route is not duplicate, but something else has failed
if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute { if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute {
return fmt.Errorf("failed to add route for service: %v", res.Service.Name) return fmt.Errorf("failed to add route for service: %v", res.Service.Name)
} }
@ -274,8 +269,7 @@ func (r *router) Stop() error {
// NOTE: we need a more efficient way of doing this e.g. network routes // NOTE: we need a more efficient way of doing this e.g. network routes
// should ideally be autodeleted when the router stops gossiping // should ideally be autodeleted when the router stops gossiping
// deregister all services advertised by this router from remote registry query := NewQuery(QueryRouter(r), QueryNetwork(r.opts.Advertise))
query := NewQuery(QueryGateway(r), QueryNetwork(r.opts.NetworkAddress))
routes, err := r.opts.Table.Lookup(query) routes, err := r.opts.Table.Lookup(query)
if err != nil && err != ErrRouteNotFound { if err != nil && err != ErrRouteNotFound {
return fmt.Errorf("failed to lookup routes for router %s: %v", r.opts.ID, err) return fmt.Errorf("failed to lookup routes for router %s: %v", r.opts.ID, err)
@ -289,10 +283,10 @@ func (r *router) Stop() error {
for _, route := range routes { for _, route := range routes {
service := &registry.Service{ service := &registry.Service{
Name: route.Options().DestAddr, Name: route.Destination,
Nodes: []*registry.Node{node}, Nodes: []*registry.Node{node},
} }
if err := r.opts.NetworkRegistry.Deregister(service); err != nil { if err := r.opts.Network.Deregister(service); err != nil {
return fmt.Errorf("failed to deregister service %s from network registry: %v", service.Name, err) return fmt.Errorf("failed to deregister service %s from network registry: %v", service.Name, err)
} }
} }
@ -310,7 +304,7 @@ func (r *router) String() string {
data := []string{ data := []string{
r.opts.ID, r.opts.ID,
r.opts.Address, r.opts.Address,
r.opts.NetworkAddress, r.opts.Advertise,
fmt.Sprintf("%d", r.opts.Table.Size()), fmt.Sprintf("%d", r.opts.Table.Size()),
} }
table.Append(data) table.Append(data)

View File

@ -64,7 +64,7 @@ func (t *table) Options() TableOptions {
// Add adds a route to the routing table // Add adds a route to the routing table
func (t *table) Add(r Route) error { func (t *table) Add(r Route) error {
destAddr := r.Options().DestAddr destAddr := r.Destination
sum := t.hash(r) sum := t.hash(r)
t.Lock() t.Lock()
@ -86,7 +86,7 @@ func (t *table) Add(r Route) error {
} }
// only add the route if the route override is explicitly requested // only add the route if the route override is explicitly requested
if _, ok := t.m[destAddr][sum]; ok && r.Options().Policy == OverrideIfExists { if _, ok := t.m[destAddr][sum]; ok && r.Policy == OverrideIfExists {
t.m[destAddr][sum] = r t.m[destAddr][sum] = r
go t.sendEvent(&Event{Type: UpdateEvent, Route: r}) go t.sendEvent(&Event{Type: UpdateEvent, Route: r})
return nil return nil
@ -94,7 +94,7 @@ func (t *table) Add(r Route) error {
// if we reached this point without already returning the route already exists // if we reached this point without already returning the route already exists
// we return nil only if explicitly requested by the client // we return nil only if explicitly requested by the client
if r.Options().Policy == IgnoreIfExists { if r.Policy == IgnoreIfExists {
return nil return nil
} }
@ -106,7 +106,7 @@ func (t *table) Delete(r Route) error {
t.Lock() t.Lock()
defer t.Unlock() defer t.Unlock()
destAddr := r.Options().DestAddr destAddr := r.Destination
sum := t.hash(r) sum := t.hash(r)
if _, ok := t.m[destAddr]; !ok { if _, ok := t.m[destAddr]; !ok {
@ -121,7 +121,7 @@ func (t *table) Delete(r Route) error {
// Update updates routing table with new route // Update updates routing table with new route
func (t *table) Update(r Route) error { func (t *table) Update(r Route) error {
destAddr := r.Options().DestAddr destAddr := r.Destination
sum := t.hash(r) sum := t.hash(r)
t.Lock() t.Lock()
@ -165,24 +165,28 @@ func (t *table) Lookup(q Query) ([]Route, error) {
var results []Route var results []Route
for destAddr, routes := range t.m { for destAddr, routes := range t.m {
if q.Options().DestAddr != "*" { if q.Options().Destination != "*" {
if q.Options().DestAddr != destAddr { if q.Options().Destination != destAddr {
continue continue
} }
for _, route := range routes { for _, route := range routes {
if q.Options().Network == "*" || q.Options().Network == route.Options().Network { if q.Options().Network == "*" || q.Options().Network == route.Network {
if q.Options().Gateway.ID() == "*" || q.Options().Gateway.ID() == route.Options().Gateway.ID() { if q.Options().Router.ID() == "*" || q.Options().Router.ID() == route.Router.ID() {
results = append(results, route) if route.Metric <= q.Options().Metric {
results = append(results, route)
}
} }
} }
} }
} }
if q.Options().DestAddr == "*" { if q.Options().Destination == "*" {
for _, route := range routes { for _, route := range routes {
if q.Options().Network == "*" || q.Options().Network == route.Options().Network { if q.Options().Network == "*" || q.Options().Network == route.Router.Network() {
if q.Options().Gateway.ID() == "*" || q.Options().Gateway.ID() == route.Options().Gateway.ID() { if q.Options().Router.ID() == "*" || q.Options().Router.ID() == route.Router.ID() {
results = append(results, route) if route.Metric <= q.Options().Metric {
results = append(results, route)
}
} }
} }
} }
@ -200,8 +204,8 @@ func (t *table) Lookup(q Query) ([]Route, error) {
func (t *table) Watch(opts ...WatchOption) (Watcher, error) { func (t *table) Watch(opts ...WatchOption) (Watcher, error) {
// by default watch everything // by default watch everything
wopts := WatchOptions{ wopts := WatchOptions{
DestAddr: "*", Destination: "*",
Network: "*", Network: "*",
} }
for _, o := range opts { for _, o := range opts {
@ -252,15 +256,15 @@ func (t *table) String() string {
// create nice table printing structure // create nice table printing structure
table := tablewriter.NewWriter(sb) table := tablewriter.NewWriter(sb)
table.SetHeader([]string{"Destination", "Gateway", "Network", "Metric"}) table.SetHeader([]string{"Destination", "Router", "Network", "Metric"})
for _, destRoute := range t.m { for _, destRoute := range t.m {
for _, route := range destRoute { for _, route := range destRoute {
strRoute := []string{ strRoute := []string{
route.Options().DestAddr, route.Destination,
route.Options().Gateway.Address(), route.Router.Address(),
route.Options().Network, route.Network,
fmt.Sprintf("%d", route.Options().Metric), fmt.Sprintf("%d", route.Metric),
} }
table.Append(strRoute) table.Append(strRoute)
} }
@ -274,12 +278,12 @@ func (t *table) String() string {
// hash hashes the route using router gateway and network address // hash hashes the route using router gateway and network address
func (t *table) hash(r Route) uint64 { func (t *table) hash(r Route) uint64 {
destAddr := r.Options().DestAddr destAddr := r.Destination
gwAddr := r.Options().Gateway.Address() routerAddr := r.Router.Address()
netAddr := r.Options().Network netAddr := r.Network
t.h.Reset() t.h.Reset()
t.h.Write([]byte(destAddr + gwAddr + netAddr)) t.h.Write([]byte(destAddr + routerAddr + netAddr))
return t.h.Sum64() return t.h.Sum64()
} }

View File

@ -6,31 +6,26 @@ import (
) )
var ( var (
// DefaultGossipAddress is default gossip bind address // DefaultAddress is default router address
DefaultGossipAddress = ":9093" DefaultAddress = ":9093"
// DefaultNetworkAddress is default network bind address // DefaultAdvertise is default address advertised to the network
DefaultNetworkAddress = ":9094" DefaultAdvertise = ":9094"
) )
// Options are router options // Options are router options
type Options struct { type Options struct {
// ID is router id // ID is router id
ID string ID string
// Address is router micro service address // Address is router address
Address string Address string
// GossipAddress is router gossip address // Advertise is the address advertised to the network
GossipAddress string Advertise string
// NetworkAddress is micro network address // Registry is the local registry
NetworkAddress string Registry registry.Registry
// LocalRegistry is router local registry // Networkis the network registry
LocalRegistry registry.Registry Network registry.Registry
// NetworkRegistry is router remote registry
// NOTE: we need some abstraction on top of gossip.Registry
NetworkRegistry registry.Registry
// Table is routing table // Table is routing table
Table Table Table Table
// RIB is Routing Information Base
RIB RIB
} }
// ID sets Router ID // ID sets Router ID
@ -47,59 +42,43 @@ func Address(a string) Option {
} }
} }
// GossipAddress sets router gossip address // Advertise sets the address that is advertise to the network
func GossipAddress(a string) Option { func Advertise(n string) Option {
return func(o *Options) { return func(o *Options) {
o.GossipAddress = a o.Advertise = n
} }
} }
// NetworkAddress sets router network address // RoutingTable sets the routing table
func NetworkAddress(n string) Option {
return func(o *Options) {
o.NetworkAddress = n
}
}
// RoutingTable allows to specify custom routing table
func RoutingTable(t Table) Option { func RoutingTable(t Table) Option {
return func(o *Options) { return func(o *Options) {
o.Table = t o.Table = t
} }
} }
// LocalRegistry allows to specify local registry // Registry sets the local registry
func LocalRegistry(r registry.Registry) Option { func Registry(r registry.Registry) Option {
return func(o *Options) { return func(o *Options) {
o.LocalRegistry = r o.Registry = r
} }
} }
// NetworkRegistry allows to specify remote registry // Network sets the network registry
func NetworkRegistry(r registry.Registry) Option { func Network(r registry.Registry) Option {
return func(o *Options) { return func(o *Options) {
o.NetworkRegistry = r o.Network = r
}
}
// RouterRIB allows to configure RIB
func RouterRIB(r RIB) Option {
return func(o *Options) {
o.RIB = r
} }
} }
// DefaultOptions returns router default options // DefaultOptions returns router default options
func DefaultOptions() Options { func DefaultOptions() Options {
// NOTE: by default both local and network registies use default registry i.e. mdns // NOTE: by default both local and network registies use default registry i.e. mdns
// TODO: DefaultRIB needs to be added once it's properly figured out
return Options{ return Options{
ID: uuid.New().String(), ID: uuid.New().String(),
Address: ":8083", Address: DefaultAddress,
GossipAddress: DefaultGossipAddress, Advertise: DefaultAdvertise,
NetworkAddress: DefaultNetworkAddress, Registry: registry.DefaultRegistry,
LocalRegistry: registry.DefaultRegistry, Network: registry.DefaultRegistry,
NetworkRegistry: registry.DefaultRegistry, Table: NewTable(),
Table: NewTable(),
} }
} }

View File

@ -27,20 +27,22 @@ type QueryOption func(*QueryOptions)
// QueryOptions are routing table query options // QueryOptions are routing table query options
type QueryOptions struct { type QueryOptions struct {
// DestAddr is destination address // Destination is destination address
DestAddr string Destination string
// NetworkAddress is network address // Network is network address
Network string Network string
// Gateway is gateway address // Router is gateway address
Gateway Router Router Router
// Metric is route metric
Metric int
// Policy is query lookup policy // Policy is query lookup policy
Policy LookupPolicy Policy LookupPolicy
} }
// QueryDestAddr sets query destination address // QueryDestination sets query destination address
func QueryDestAddr(a string) QueryOption { func QueryDestination(a string) QueryOption {
return func(o *QueryOptions) { return func(o *QueryOptions) {
o.DestAddr = a o.Destination = a
} }
} }
@ -51,14 +53,22 @@ func QueryNetwork(a string) QueryOption {
} }
} }
// QueryGateway sets query gateway address // QueryRouter sets query gateway address
func QueryGateway(r Router) QueryOption { func QueryRouter(r Router) QueryOption {
return func(o *QueryOptions) { return func(o *QueryOptions) {
o.Gateway = r o.Router = r
}
}
// QueryMetric sets query metric
func QueryMetric(m int) QueryOption {
return func(o *QueryOptions) {
o.Metric = m
} }
} }
// QueryPolicy sets query policy // QueryPolicy sets query policy
// NOTE: this might be renamed to filter or some such
func QueryPolicy(p LookupPolicy) QueryOption { func QueryPolicy(p LookupPolicy) QueryOption {
return func(o *QueryOptions) { return func(o *QueryOptions) {
o.Policy = p o.Policy = p
@ -82,11 +92,13 @@ func NewQuery(opts ...QueryOption) Query {
r := newRouter(ID("*")) r := newRouter(ID("*"))
// default options // default options
// NOTE: by default we use DefaultNetworkMetric
qopts := QueryOptions{ qopts := QueryOptions{
DestAddr: "*", Destination: "*",
Network: "*", Network: "*",
Gateway: r, Router: r,
Policy: DiscardNoRoute, Metric: DefaultNetworkMetric,
Policy: DiscardNoRoute,
} }
for _, o := range opts { for _, o := range opts {

View File

@ -1,30 +0,0 @@
package router
// RIB is Routing Information Base.
// RIB is used to source the base routing table.
type RIB interface {
// Initi initializes RIB
Init(...RIBOption) error
// Options returns RIB options
Options() RIBOptions
// Routes returns routes
Routes() []Route
// String returns debug info
String() string
}
// RIBOption sets RIB options
type RIBOption func(*RIBOptions)
// RIBOptions are RIB options
type RIBOptions struct {
// Source defines RIB source URL
Source string
}
// Source sets RIB source
func Source(s string) RIBOption {
return func(o *RIBOptions) {
o.Source = s
}
}

View File

@ -14,18 +14,18 @@ var (
DefaultNetworkMetric = 10 DefaultNetworkMetric = 10
) )
// AddPolicy defines routing table addition policy // RoutePolicy defines routing table addition policy
type AddPolicy int type RoutePolicy int
const ( const (
// OverrideIfExists overrides route if it already exists // OverrideIfExists overrides route if it already exists
OverrideIfExists AddPolicy = iota OverrideIfExists RoutePolicy = iota
// IgnoreIfExists does not modify existing route // IgnoreIfExists does not modify existing route
IgnoreIfExists IgnoreIfExists
) )
// String returns human reprensentation of policy // String returns human reprensentation of policy
func (p AddPolicy) String() string { func (p RoutePolicy) String() string {
switch p { switch p {
case OverrideIfExists: case OverrideIfExists:
return "OVERRIDE" return "OVERRIDE"
@ -36,100 +36,34 @@ func (p AddPolicy) String() string {
} }
} }
// RouteOption is used to set routing table entry options // Route is network route
type RouteOption func(*RouteOptions) type Route struct {
// Destination is destination address
// RouteOptions are route options Destination string
type RouteOptions struct { // Router is the network router
// DestAddr is destination address Router Router
DestAddr string // Network is micro network address
// Gateway is the next route hop
Gateway Router
// Network defines micro network
Network string Network string
// Metric is route cost metric // Metric is the route cost metric
Metric int Metric int
// Policy defines route addition policy // Policy defines route policy
Policy AddPolicy Policy RoutePolicy
}
// DestAddr sets destination address
func DestAddr(a string) RouteOption {
return func(o *RouteOptions) {
o.DestAddr = a
}
}
// Gateway sets the route gateway
func Gateway(r Router) RouteOption {
return func(o *RouteOptions) {
o.Gateway = r
}
}
// Network sets micro network
func Network(n string) RouteOption {
return func(o *RouteOptions) {
o.Network = n
}
}
// Metric sets route metric
func Metric(m int) RouteOption {
return func(o *RouteOptions) {
o.Metric = m
}
}
// RoutePolicy sets add route policy
func RoutePolicy(p AddPolicy) RouteOption {
return func(o *RouteOptions) {
o.Policy = p
}
}
// Route is routing table route
type Route interface {
// Options returns route options
Options() RouteOptions
}
type route struct {
opts RouteOptions
}
// NewRoute returns new routing table route
func NewRoute(opts ...RouteOption) Route {
eopts := RouteOptions{}
for _, o := range opts {
o(&eopts)
}
return &route{
opts: eopts,
}
}
// Options returns route options
func (r *route) Options() RouteOptions {
return r.opts
} }
// String allows to print the route // String allows to print the route
func (r *route) String() string { func (r *Route) String() string {
// this will help us build routing table string // this will help us build routing table string
sb := &strings.Builder{} sb := &strings.Builder{}
// create nice table printing structure // create nice table printing structure
table := tablewriter.NewWriter(sb) table := tablewriter.NewWriter(sb)
table.SetHeader([]string{"Destination", "Gateway", "Network", "Metric"}) table.SetHeader([]string{"Destination", "Router", "Network", "Metric"})
strRoute := []string{ strRoute := []string{
r.opts.DestAddr, r.Destination,
r.opts.Gateway.Address(), r.Router.Address(),
r.opts.Network, r.Network,
fmt.Sprintf("%d", r.opts.Metric), fmt.Sprintf("%d", r.Metric),
} }
table.Append(strRoute) table.Append(strRoute)

View File

@ -13,8 +13,6 @@ type Router interface {
Table() Table Table() Table
// Address returns the router adddress // Address returns the router adddress
Address() string Address() string
// Gossip returns the router gossip address
Gossip() string
// Network returns the router network address // Network returns the router network address
Network() string Network() string
// Start starts the router // Start starts the router

View File

@ -54,6 +54,8 @@ type WatchOption func(*WatchOptions)
type Watcher interface { type Watcher interface {
// Next is a blocking call that returns watch result // Next is a blocking call that returns watch result
Next() (*Event, error) Next() (*Event, error)
// Chan returns event channel
Chan() (<-chan *Event, error)
// Stop stops watcher // Stop stops watcher
Stop() Stop()
} }
@ -61,16 +63,16 @@ type Watcher interface {
// WatchOptions are table watcher options // WatchOptions are table watcher options
type WatchOptions struct { type WatchOptions struct {
// Specify destination address to watch // Specify destination address to watch
DestAddr string Destination string
// Specify network to watch // Specify network to watch
Network string Network string
} }
// WatchDestAddr sets what destination to watch // WatchDestination sets what destination to watch
// Destination is usually microservice name // Destination is usually microservice name
func WatchDestAddr(a string) WatchOption { func WatchDestination(a string) WatchOption {
return func(o *WatchOptions) { return func(o *WatchOptions) {
o.DestAddr = a o.Destination = a
} }
} }
@ -94,13 +96,13 @@ func (w *tableWatcher) Next() (*Event, error) {
for { for {
select { select {
case res := <-w.resChan: case res := <-w.resChan:
switch w.opts.DestAddr { switch w.opts.Destination {
case "*", "": case "*", "":
if w.opts.Network == "*" || w.opts.Network == res.Route.Options().Network { if w.opts.Network == "*" || w.opts.Network == res.Route.Network {
return res, nil return res, nil
} }
case res.Route.Options().DestAddr: case res.Route.Destination:
if w.opts.Network == "*" || w.opts.Network == res.Route.Options().Network { if w.opts.Network == "*" || w.opts.Network == res.Route.Network {
return res, nil return res, nil
} }
} }
@ -110,6 +112,11 @@ func (w *tableWatcher) Next() (*Event, error) {
} }
} }
// Chan returns watcher events channel
func (w *tableWatcher) Chan() (<-chan *Event, error) {
return w.resChan, nil
}
// Stop stops routing table watcher // Stop stops routing table watcher
func (w *tableWatcher) Stop() { func (w *tableWatcher) Stop() {
select { select {
@ -125,10 +132,10 @@ func (w *tableWatcher) String() string {
sb := &strings.Builder{} sb := &strings.Builder{}
table := tablewriter.NewWriter(sb) table := tablewriter.NewWriter(sb)
table.SetHeader([]string{"DestAddr", "Network"}) table.SetHeader([]string{"Destination", "Network"})
data := []string{ data := []string{
w.opts.DestAddr, w.opts.Destination,
w.opts.Network, w.opts.Network,
} }
table.Append(data) table.Append(data)