router

package
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2019 License: Apache-2.0 Imports: 12 Imported by: 48

Documentation

Overview

Package router provides an interface for micro network router

Index

Constants

This section is empty.

Variables

View Source
var (
	// AdvertiseTick defines how often in seconds do we scal the local registry
	// to advertise the local services to the network registry
	AdvertiseTick = 5 * time.Second
	// AdvertiseTTL defines network registry TTL in seconds
	// NOTE: this is a rather arbitrary picked value subject to change
	AdvertiseTTL = 120 * time.Second
)
View Source
var (
	// DefaultAddress is default router address
	DefaultAddress = ":9093"
	// DefaultAdvertise is default address advertised to the network
	DefaultAdvertise = ":9094"
)
View Source
var (
	// DefaultLocalMetric is default route cost for local network
	DefaultLocalMetric = 1
	// DefaultNetworkMetric is default route cost for micro network
	DefaultNetworkMetric = 10
)
View Source
var (
	// ErrRouteNotFound is returned when no route was found in the routing table
	ErrRouteNotFound = errors.New("route not found")
	// ErrDuplicateRoute is returned when the route already exists
	ErrDuplicateRoute = errors.New("duplicate route")
)
View Source
var (
	// ErrWatcherStopped is returned when routing table watcher has been stopped
	ErrWatcherStopped = errors.New("routing table watcher stopped")
)

Functions

This section is empty.

Types

type Event

type Event struct {
	// Type defines type of event
	Type EventType
	// Route is table rout
	Route Route
}

Event is returned by a call to Next on the watcher.

type EventType

type EventType int

EventType defines routing table event

const (
	// CreateEvent is emitted when new route has been created
	CreateEvent EventType = iota
	// DeleteEvent is emitted when an existing route has been deleted
	DeleteEvent
	// UpdateEvent is emitted when a routing table has been updated
	UpdateEvent
)

func (EventType) String

func (et EventType) String() string

String returns string representation of the event

type LookupPolicy

type LookupPolicy int

LookupPolicy defines query policy

const (
	// DiscardNoRoute discards query when no route is found
	DiscardNoRoute LookupPolicy = iota
	// ClosestMatch returns closest match to supplied query
	ClosestMatch
)

func (LookupPolicy) String

func (lp LookupPolicy) String() string

String returns human representation of LookupPolicy

type Option

type Option func(*Options)

Option used by the router

func Address

func Address(a string) Option

Address sets router service address

func Advertise(n string) Option

Advertise sets the address that is advertise to the network

func ID

func ID(id string) Option

ID sets Router ID

func Network

func Network(r registry.Registry) Option

Network sets the network registry

func Registry

func Registry(r registry.Registry) Option

Registry sets the local registry

func RoutingTable

func RoutingTable(t Table) Option

RoutingTable sets the routing table

type Options

type Options struct {
	// ID is router id
	ID string
	// Address is router address
	Address string
	// Advertise is the address advertised to the network
	Advertise string
	// Registry is the local registry
	Registry registry.Registry
	// Networkis the network registry
	Network registry.Registry
	// Table is routing table
	Table Table
}

Options are router options

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns router default options

type Query

type Query interface {
	// Options returns query options
	Options() QueryOptions
}

Query is routing table query

func NewQuery

func NewQuery(opts ...QueryOption) Query

NewQuery creates new query and returns it

type QueryOption

type QueryOption func(*QueryOptions)

QueryOption sets routing table query options

func QueryDestination

func QueryDestination(a string) QueryOption

QueryDestination sets query destination address

func QueryMetric

func QueryMetric(m int) QueryOption

QueryMetric sets query metric

func QueryNetwork

func QueryNetwork(a string) QueryOption

QueryNetwork sets query network address

func QueryPolicy

func QueryPolicy(p LookupPolicy) QueryOption

QueryPolicy sets query policy NOTE: this might be renamed to filter or some such

func QueryRouter

func QueryRouter(r Router) QueryOption

QueryRouter sets query gateway address

type QueryOptions

type QueryOptions struct {
	// Destination is destination address
	Destination string
	// Network is network address
	Network string
	// Router is gateway address
	Router Router
	// Metric is route metric
	Metric int
	// Policy is query lookup policy
	Policy LookupPolicy
}

QueryOptions are routing table query options

type Route

type Route struct {
	// Destination is destination address
	Destination string
	// Router is the network router
	Router Router
	// Network is micro network address
	Network string
	// Metric is the route cost metric
	Metric int
	// Policy defines route policy
	Policy RoutePolicy
}

Route is network route

func (*Route) String

func (r *Route) String() string

String allows to print the route

type RoutePolicy

type RoutePolicy int

RoutePolicy defines routing table addition policy

const (
	// OverrideIfExists overrides route if it already exists
	OverrideIfExists RoutePolicy = iota
	// IgnoreIfExists does not modify existing route
	IgnoreIfExists
)

func (RoutePolicy) String

func (p RoutePolicy) String() string

String returns human reprensentation of policy

type Router

type Router interface {
	// Init initializes the router with options
	Init(...Option) error
	// Options returns the router options
	Options() Options
	// ID returns the id of the router
	ID() string
	// Table returns the routing table
	Table() Table
	// Address returns the router adddress
	Address() string
	// Network returns the network address of the router
	Network() string
	// Advertise starts advertising the routes to the network
	Advertise() error
	// Stop stops the router
	Stop() error
	// String returns debug info
	String() string
}

Router is micro network router

func NewRouter

func NewRouter(opts ...Option) Router

NewRouter creates new Router and returns it

type Table

type Table interface {
	// Init initializes the router with options
	Init(...TableOption) error
	// Options returns the router options
	Options() TableOptions
	// Add adds new route to the routing table
	Add(Route) error
	// Delete deletes existing route from the routing table
	Delete(Route) error
	// Update updates route in the routing table
	Update(Route) error
	// List returns the list of all routes in the table
	List() ([]Route, error)
	// Lookup looks up routes in the routing table and returns them
	Lookup(Query) ([]Route, error)
	// Watch returns a watcher which allows to track updates to the routing table
	Watch(opts ...WatchOption) (Watcher, error)
	// Size returns the size of the routing table
	Size() int
	// String prints the routing table
	String() string
}

Table defines routing table interface

func NewTable

func NewTable(opts ...TableOption) Table

NewTable creates new routing table and returns it

type TableOption

type TableOption func(*TableOptions)

TableOption used by the routing table

type TableOptions

type TableOptions struct{}

TableOptions are routing table options TODO: table options TBD in the future

type WatchOption

type WatchOption func(*WatchOptions)

WatchOption is used to define what routes to watch in the table

func WatchDestination

func WatchDestination(a string) WatchOption

WatchDestination sets what destination to watch Destination is usually microservice name

func WatchNetwork

func WatchNetwork(n string) WatchOption

WatchNetwork sets what network to watch

type WatchOptions

type WatchOptions struct {
	// Specify destination address to watch
	Destination string
	// Specify network to watch
	Network string
}

WatchOptions are table watcher options

type Watcher

type Watcher interface {
	// Next is a blocking call that returns watch result
	Next() (*Event, error)
	// Chan returns event channel
	Chan() (<-chan *Event, error)
	// Stop stops watcher
	Stop()
}

Watcher defines routing table watcher interface Watcher returns updates to the routing table

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL