2019-06-06 18:37:40 +03:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2019-06-13 00:30:42 +03:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/micro/go-micro/registry"
|
2019-06-06 18:37:40 +03:00
|
|
|
)
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
var (
|
2019-06-13 17:12:07 +03:00
|
|
|
// DefaultGossipAddress is default gossip bind address
|
|
|
|
DefaultGossipAddress = ":9093"
|
|
|
|
// DefaultNetworkAddress is default network bind address
|
2019-06-13 00:30:42 +03:00
|
|
|
DefaultNetworkAddress = ":9094"
|
|
|
|
)
|
|
|
|
|
2019-06-13 14:09:49 +03:00
|
|
|
// Options are router options
|
2019-06-06 18:37:40 +03:00
|
|
|
type Options struct {
|
2019-06-13 17:12:07 +03:00
|
|
|
// ID is router id
|
2019-06-10 21:50:54 +03:00
|
|
|
ID string
|
2019-06-13 17:12:07 +03:00
|
|
|
// Address is router micro service address
|
2019-06-07 15:29:09 +03:00
|
|
|
Address string
|
2019-06-13 00:30:42 +03:00
|
|
|
// GossipAddress is router gossip address
|
|
|
|
GossipAddress string
|
|
|
|
// NetworkAddress is micro network address
|
|
|
|
NetworkAddress string
|
|
|
|
// LocalRegistry is router local registry
|
|
|
|
LocalRegistry registry.Registry
|
|
|
|
// NetworkRegistry is router remote registry
|
2019-06-14 00:28:47 +03:00
|
|
|
// NOTE: we need some abstraction on top of gossip.Registry
|
2019-06-13 00:30:42 +03:00
|
|
|
NetworkRegistry registry.Registry
|
2019-06-07 15:29:09 +03:00
|
|
|
// Table is routing table
|
|
|
|
Table Table
|
2019-06-13 00:30:42 +03:00
|
|
|
// RIB is Routing Information Base
|
|
|
|
RIB RIB
|
2019-06-06 18:37:40 +03:00
|
|
|
}
|
|
|
|
|
2019-06-10 21:50:54 +03:00
|
|
|
// ID sets Router ID
|
|
|
|
func ID(id string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.ID = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 17:12:07 +03:00
|
|
|
// Address sets router service address
|
2019-06-07 19:20:22 +03:00
|
|
|
func Address(a string) Option {
|
2019-06-07 15:29:09 +03:00
|
|
|
return func(o *Options) {
|
2019-06-07 19:20:22 +03:00
|
|
|
o.Address = a
|
2019-06-07 15:29:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
// GossipAddress sets router gossip address
|
|
|
|
func GossipAddress(a string) Option {
|
2019-06-10 21:50:54 +03:00
|
|
|
return func(o *Options) {
|
2019-06-13 00:30:42 +03:00
|
|
|
o.GossipAddress = a
|
2019-06-10 21:50:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
// NetworkAddress sets router network address
|
|
|
|
func NetworkAddress(n string) Option {
|
2019-06-07 15:29:09 +03:00
|
|
|
return func(o *Options) {
|
2019-06-13 00:30:42 +03:00
|
|
|
o.NetworkAddress = n
|
2019-06-07 19:20:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
// RoutingTable allows to specify custom routing table
|
|
|
|
func RoutingTable(t Table) Option {
|
2019-06-07 19:20:22 +03:00
|
|
|
return func(o *Options) {
|
2019-06-13 00:30:42 +03:00
|
|
|
o.Table = t
|
2019-06-07 15:29:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
// LocalRegistry allows to specify local registry
|
|
|
|
func LocalRegistry(r registry.Registry) Option {
|
2019-06-07 01:29:24 +03:00
|
|
|
return func(o *Options) {
|
2019-06-13 00:30:42 +03:00
|
|
|
o.LocalRegistry = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkRegistry allows to specify remote registry
|
|
|
|
func NetworkRegistry(r registry.Registry) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.NetworkRegistry = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
// RouterRIB allows to configure RIB
|
2019-06-13 14:09:49 +03:00
|
|
|
func RouterRIB(r RIB) Option {
|
2019-06-13 00:30:42 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.RIB = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultOptions returns router default options
|
|
|
|
func DefaultOptions() Options {
|
|
|
|
// 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{
|
|
|
|
ID: uuid.New().String(),
|
2019-06-13 17:12:07 +03:00
|
|
|
Address: ":8083",
|
|
|
|
GossipAddress: DefaultGossipAddress,
|
2019-06-13 00:30:42 +03:00
|
|
|
NetworkAddress: DefaultNetworkAddress,
|
|
|
|
LocalRegistry: registry.DefaultRegistry,
|
|
|
|
NetworkRegistry: registry.DefaultRegistry,
|
|
|
|
Table: NewTable(),
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
}
|