Package router

import "github.com/gonitro/nitro/app/router"

Overview ▾

Package router provides a network routing control plane


var (
    // DefaultLink is default network link
    DefaultLink = "local"
    // DefaultLocalMetric is default route cost for a local route
    DefaultMetric int64 = 1
    // DefaultNetwork is default micro network
    DefaultNetwork = "nitro"
    // 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")
var (
    // ErrNoneAvailable is returned by select when no routes were provided to select from
    ErrNoneAvailable = errors.New("none available")
var (
    // ErrWatcherStopped is returned when routing table watcher has been stopped
    ErrWatcherStopped = errors.New("watcher stopped")

type Event

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

type Event struct {
    // Unique id of the event
    Id string
    // Type defines type of event
    Type EventType
    // Timestamp is event timestamp
    Timestamp time.Time
    // Route is table route
    Route Route

type EventType

EventType defines routing table event

type EventType int
const (
    // Create is emitted when a new route has been created
    Create EventType = iota
    // Delete is emitted when an existing route has been deleted
    // Update is emitted when an existing route has been updated

func (EventType) String

func (t EventType) String() string

String returns human readable event type

type LookupOption

LookupOption sets routing table query options

type LookupOption func(*LookupOptions)

func LookupAddress

func LookupAddress(a string) LookupOption

LookupAddress sets service to query

func LookupGateway

func LookupGateway(g string) LookupOption

LookupGateway sets gateway address to query

func LookupLink(link string) LookupOption

LookupLink sets the link to query

func LookupNetwork

func LookupNetwork(n string) LookupOption

LookupNetwork sets network name to query

func LookupRouter

func LookupRouter(r string) LookupOption

LookupRouter sets router id to query

type LookupOptions

LookupOptions are routing table query options TODO replace with Filter(Route) bool

type LookupOptions struct {
    // Address of the service
    Address string
    // Gateway is route gateway
    Gateway string
    // Network is network address
    Network string
    // Router is router id
    Router string
    // Link to query
    Link string

func NewLookup

func NewLookup(opts ...LookupOption) LookupOptions

NewLookup creates new query and returns it

type Next

Next returns the next node

type Next func() string

type Option

Option used by the router

type Option func(*Options)

func Address

func Address(a string) Option

Address sets router service address

func Cache

func Cache() Option

Cache the routes

func Gateway

func Gateway(g string) Option

Gateway sets network gateway

func Id

func Id(id string) Option

Id sets Router Id

func Network

func Network(n string) Option

Network sets router network

func Registry

func Registry(r registry.Table) Option

Registry sets the local registry

type Options

Options are router options

type Options struct {
    // Id is router id
    Id string
    // Address is router address
    Address string
    // Gateway is network gateway
    Gateway string
    // Network is network address
    Network string
    // Registry is the local registry
    Registry registry.Table
    // Context for additional options
    Context context.Context
    // Cache routes
    Cache bool

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns router default options

type Random

type Random struct{}

func (*Random) Select

func (r *Random) Select(routes []string, opts ...SelectOption) (Next, error)

type ReadOption

type ReadOption func(o *ReadOptions)

func ReadApp

func ReadApp(s string) ReadOption

ReadApp sets the service to read from the table

type ReadOptions

type ReadOptions struct {
    App string

type RoundRobin

type RoundRobin struct{}

func (*RoundRobin) Select

func (r *RoundRobin) Select(routes []string, opts ...SelectOption) (Next, error)

type Route

Route is a network route

type Route struct {
    // App is destination service name
    App string
    // Address is service node address
    Address string
    // Gateway is route gateway
    Gateway string
    // Network is network address
    Network string
    // Router is router id
    Router string
    // Link is network link
    Link string
    // Metric is the route cost metric
    Metric int64
    // Metadata for the route
    Metadata map[string]string

func Filter

func Filter(routes []Route, opts LookupOptions) []Route

filterRoutes finds all the routes for given network and router and returns them

func (*Route) Hash

func (r *Route) Hash() uint64

Hash returns route hash sum.

type Router

Router is an interface for a routing control plane

type Router interface {
    // Init initializes the router with options
    Init(...Option) error
    // Options returns the router options
    Options() Options
    // The routing table
    Table() Table
    // Lookup queries routes in the routing table
    Lookup(service string, opts ...LookupOption) ([]Route, error)
    // Watch returns a watcher which tracks updates to the routing table
    Watch(opts ...WatchOption) (Watcher, error)
    // Close the router
    Close() error
    // Returns the router implementation
    String() string

type SelectOption

type SelectOption func(o *SelectorOptions)

type Selector

Selector selects a route from a pool

type Selector interface {
    // Select a route from the pool using the strategy
    Select([]string, ...SelectOption) (Next, error)

type SelectorOptions

type SelectorOptions struct{}

type StatusCode

StatusCode defines router status

type StatusCode int
const (
    // Running means the router is up and running
    Running StatusCode = iota
    // Stopped means the router has been stopped
    // Error means the router has encountered error

type Table

Table is an interface for routing table

type Table interface {
    // Create new route in the routing table
    Create(Route) error
    // Delete existing route from the routing table
    Delete(Route) error
    // Update route in the routing table
    Update(Route) error
    // Read is for querying the table
    Read(...ReadOption) ([]Route, error)

type WatchOption

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

type WatchOption func(*WatchOptions)

func WatchApp

func WatchApp(s string) WatchOption

WatchApp sets what service routes to watch App is the microservice name

type WatchOptions

WatchOptions are table watcher options TODO: expand the options to watch based on other criteria

type WatchOptions struct {
    // App allows to watch specific service routes
    App string

type Watcher

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

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


Name Synopsis
static Package static is a static router which returns the service name as the address + port