2019-06-06 17:37:40 +02:00
|
|
|
// Package router provides an interface for micro network routers
|
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
)
|
|
|
|
|
2019-06-07 00:29:24 +02:00
|
|
|
var (
|
|
|
|
// DefaultRouter returns default micro router
|
|
|
|
DefaultRouter = NewRouter()
|
|
|
|
)
|
|
|
|
|
2019-06-06 17:37:40 +02:00
|
|
|
// Router is micro network router
|
|
|
|
type Router interface {
|
|
|
|
// Initi initializes Router with options
|
|
|
|
Init(...Option) error
|
|
|
|
// Options returns Router options
|
|
|
|
Options() Options
|
2019-06-07 00:29:24 +02:00
|
|
|
// Add adds new entry into routing table
|
|
|
|
Add(*Entry, ...RouteOption) error
|
|
|
|
// Remove removes entry from the routing table
|
|
|
|
Remove(*Entry) error
|
|
|
|
// Update updates entry in the routing table
|
|
|
|
Update(*Entry) error
|
|
|
|
// Lookup queries the routing table and returns matching entries
|
|
|
|
Lookup(Query) ([]*Entry, error)
|
|
|
|
// Table returns routing table
|
|
|
|
Table() *Table
|
|
|
|
// Address is Router adddress
|
|
|
|
Address() string
|
2019-06-06 17:37:40 +02:00
|
|
|
// String implemens fmt.Stringer interface
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Option used by the Router
|
|
|
|
type Option func(*Options)
|
|
|
|
|
2019-06-07 00:29:24 +02:00
|
|
|
// RouteOption is used by Router for adding routing table entries
|
|
|
|
type RouteOption func(*RouteOptions)
|
2019-06-06 17:37:40 +02:00
|
|
|
|
2019-06-07 00:29:24 +02:00
|
|
|
// QueryOption is used to defined routing table lookup query
|
|
|
|
type QueryOption func(*QueryOptions)
|
2019-06-06 17:37:40 +02:00
|
|
|
|
|
|
|
// NewRouter creates new Router and returns it
|
|
|
|
func NewRouter(opts ...Option) Router {
|
2019-06-07 00:29:24 +02:00
|
|
|
// router registry to DefaultRegistry
|
|
|
|
ropts := Options{
|
2019-06-06 17:37:40 +02:00
|
|
|
Registry: registry.DefaultRegistry,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
2019-06-07 00:29:24 +02:00
|
|
|
o(&ropts)
|
2019-06-06 17:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return newRouter(opts...)
|
|
|
|
}
|