Redefeind interfaces; Added better modelled data strauctures
Router interface has been redefined which fits better with what we are looking for. Routing table now offers a comprehensive set of information about its entries which will make up for rich queries in the future Query interface has been defined to enable current basic and more advanced queries in the future.
This commit is contained in:
parent
08da7c1283
commit
ee8b6b3114
@ -8,14 +8,14 @@ import (
|
|||||||
type router struct {
|
type router struct {
|
||||||
opts Options
|
opts Options
|
||||||
goss registry.Registry
|
goss registry.Registry
|
||||||
|
t *Table
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRouter(opts ...Option) Router {
|
func newRouter(opts ...Option) Router {
|
||||||
// TODO: for now default options
|
// TODO: figure out how to supply gossip registry options
|
||||||
goss := gossip.NewRegistry()
|
|
||||||
|
|
||||||
r := &router{
|
r := &router{
|
||||||
goss: goss,
|
goss: gossip.NewRegistry(),
|
||||||
|
t: NewTable(),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
@ -25,6 +25,7 @@ func newRouter(opts ...Option) Router {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Init initializes router with given options
|
||||||
func (r *router) Init(opts ...Option) error {
|
func (r *router) Init(opts ...Option) error {
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&r.opts)
|
o(&r.opts)
|
||||||
@ -32,26 +33,45 @@ func (r *router) Init(opts ...Option) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Options returns router options
|
||||||
func (r *router) Options() Options {
|
func (r *router) Options() Options {
|
||||||
return r.opts
|
return r.opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *router) AddRoute(route *Route, opts ...RouteOption) error {
|
// Add adds new entry into routing table with given options.
|
||||||
|
// It returns error if the entry could not be added.
|
||||||
|
func (r *router) Add(e *Entry, opts ...RouteOption) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *router) RemoveRoute(route *Route) error {
|
// Remove removes entry from the routing table.
|
||||||
|
// It returns error if either the entry could not be removed or it does not exist.
|
||||||
|
func (r *router) Remove(e *Entry) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *router) GetRoute(s *Service) ([]*Route, error) {
|
// Update updates an entry in the router's routing table
|
||||||
|
// It returns error if the entry was not found or it failed to be updated.
|
||||||
|
func (r *router) Update(e *Entry) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lookup makes a query lookup and returns all matching entries
|
||||||
|
func (r *router) Lookup(q Query) ([]*Entry, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *router) List() ([]*Route, error) {
|
// Table returns routing table
|
||||||
return nil, nil
|
func (r *router) Table() *Table {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Address returns router's network address
|
||||||
|
func (r *router) Address() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// String prints debugging information about router
|
||||||
func (r *router) String() string {
|
func (r *router) String() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package router
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
||||||
)
|
)
|
||||||
@ -15,17 +14,33 @@ type Options struct {
|
|||||||
Context context.Context
|
Context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// RouteOption allows to soecify routing table options
|
// Registry allows to set local service registry
|
||||||
type RouteOption struct {
|
|
||||||
// TTL defines route entry lifetime
|
|
||||||
TTL time.Duration
|
|
||||||
// COntext allows to specify other arbitrary options
|
|
||||||
Context context.Context
|
|
||||||
}
|
|
||||||
|
|
||||||
// Registry is local registry
|
|
||||||
func Registry(r registry.Registry) Option {
|
func Registry(r registry.Registry) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.Registry = r
|
o.Registry = r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RouteOptions allows to specify routing table options
|
||||||
|
type RouteOptions struct {
|
||||||
|
// NetID is network ID
|
||||||
|
NetID string
|
||||||
|
// Metric is route metric
|
||||||
|
Metric int
|
||||||
|
// COntext allows to specify other arbitrary options
|
||||||
|
Context context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
// NetID allows to set micro network ID
|
||||||
|
func NetID(id string) RouteOption {
|
||||||
|
return func(o *RouteOptions) {
|
||||||
|
o.NetID = id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Metric allows to set route cost metric
|
||||||
|
func Metric(m int) RouteOption {
|
||||||
|
return func(o *RouteOptions) {
|
||||||
|
o.Metric = m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
70
router/query.go
Normal file
70
router/query.go
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package router
|
||||||
|
|
||||||
|
// Policy defines query policy
|
||||||
|
type QueryPolicy int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DiscardNoRoute discards query when no rout is found
|
||||||
|
DiscardNoRoute QueryPolicy = iota
|
||||||
|
// ClosestMatch returns closest match to query
|
||||||
|
ClosestMatch
|
||||||
|
)
|
||||||
|
|
||||||
|
// QueryOptions allow to define routing table query options
|
||||||
|
type QueryOptions struct {
|
||||||
|
// Route allows to set route options
|
||||||
|
Route *RouteOptions
|
||||||
|
// Service is micro service name
|
||||||
|
Service string
|
||||||
|
// Policy defines query lookup policy
|
||||||
|
Policy QueryPolicy
|
||||||
|
}
|
||||||
|
|
||||||
|
// Route allows to set the route query options
|
||||||
|
func Route(r *RouteOptions) QueryOption {
|
||||||
|
return func(o *QueryOptions) {
|
||||||
|
o.Route = r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Service allows to set the service name in routing query
|
||||||
|
func Service(s string) QueryOption {
|
||||||
|
return func(o *QueryOptions) {
|
||||||
|
o.Service = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Policy allows to define query lookup policy
|
||||||
|
func Policy(p QueryPolicy) QueryOption {
|
||||||
|
return func(o *QueryOptions) {
|
||||||
|
o.Policy = p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query defines routing table query
|
||||||
|
type Query interface {
|
||||||
|
// Options returns query options
|
||||||
|
Options() QueryOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
type query struct {
|
||||||
|
opts QueryOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewQuery creates new query and returns it
|
||||||
|
func NewQuery(opts ...QueryOption) Query {
|
||||||
|
qopts := QueryOptions{}
|
||||||
|
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&qopts)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &query{
|
||||||
|
opts: qopts,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Options returns query options
|
||||||
|
func (q *query) Options() QueryOptions {
|
||||||
|
return q.opts
|
||||||
|
}
|
@ -1,7 +0,0 @@
|
|||||||
package router
|
|
||||||
|
|
||||||
// Route is micro network route
|
|
||||||
type Route struct {
|
|
||||||
// Hop is the next route hop
|
|
||||||
Hop Router
|
|
||||||
}
|
|
@ -2,25 +2,32 @@
|
|||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// DefaultRouter returns default micro router
|
||||||
|
DefaultRouter = NewRouter()
|
||||||
|
)
|
||||||
|
|
||||||
// Router is micro network router
|
// Router is micro network router
|
||||||
type Router interface {
|
type Router interface {
|
||||||
// Initi initializes Router with options
|
// Initi initializes Router with options
|
||||||
Init(...Option) error
|
Init(...Option) error
|
||||||
// Options returns Router options
|
// Options returns Router options
|
||||||
Options() Options
|
Options() Options
|
||||||
// AddRoute adds new service route
|
// Add adds new entry into routing table
|
||||||
AddRoute(*Route, ...RouteOption) error
|
Add(*Entry, ...RouteOption) error
|
||||||
// RemoveRoute removes service route
|
// Remove removes entry from the routing table
|
||||||
RemoveRoute(*Route) error
|
Remove(*Entry) error
|
||||||
// GetRoute returns list of routes for service
|
// Update updates entry in the routing table
|
||||||
GetRoute(*Service) ([]*Route, error)
|
Update(*Entry) error
|
||||||
// List returns all routes
|
// Lookup queries the routing table and returns matching entries
|
||||||
List() ([]*Route, error)
|
Lookup(Query) ([]*Entry, error)
|
||||||
|
// Table returns routing table
|
||||||
|
Table() *Table
|
||||||
|
// Address is Router adddress
|
||||||
|
Address() string
|
||||||
// String implemens fmt.Stringer interface
|
// String implemens fmt.Stringer interface
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
@ -28,22 +35,21 @@ type Router interface {
|
|||||||
// Option used by the Router
|
// Option used by the Router
|
||||||
type Option func(*Options)
|
type Option func(*Options)
|
||||||
|
|
||||||
var (
|
// RouteOption is used by Router for adding routing table entries
|
||||||
DefaultRouter = NewRouter()
|
type RouteOption func(*RouteOptions)
|
||||||
|
|
||||||
// Not found error when Get is called
|
// QueryOption is used to defined routing table lookup query
|
||||||
ErrNotFound = errors.New("route not found")
|
type QueryOption func(*QueryOptions)
|
||||||
)
|
|
||||||
|
|
||||||
// NewRouter creates new Router and returns it
|
// NewRouter creates new Router and returns it
|
||||||
func NewRouter(opts ...Option) Router {
|
func NewRouter(opts ...Option) Router {
|
||||||
// set Registry to DefaultRegistry
|
// router registry to DefaultRegistry
|
||||||
opt := Options{
|
ropts := Options{
|
||||||
Registry: registry.DefaultRegistry,
|
Registry: registry.DefaultRegistry,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&opt)
|
o(&ropts)
|
||||||
}
|
}
|
||||||
|
|
||||||
return newRouter(opts...)
|
return newRouter(opts...)
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
package router
|
|
||||||
|
|
||||||
// Service is a service to route to
|
|
||||||
type Service struct {
|
|
||||||
// Name is service name
|
|
||||||
Name string
|
|
||||||
}
|
|
58
router/table.go
Normal file
58
router/table.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package router
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrRouteNotFound is returned when no rout was found
|
||||||
|
ErrRouteNotFound = errors.New("route not found")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Entry is micro network routing table entry
|
||||||
|
type Entry struct {
|
||||||
|
// NetID is micro network ID
|
||||||
|
NetID string
|
||||||
|
// Hop is the next route hop
|
||||||
|
Hop Router
|
||||||
|
// Metric is route cost metric
|
||||||
|
Metric int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Table is routing table
|
||||||
|
// It maps service name to routes
|
||||||
|
type Table struct {
|
||||||
|
// m stores routing table map
|
||||||
|
m map[string][]Entry
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewTable creates new routing table and returns it
|
||||||
|
func NewTable() *Table {
|
||||||
|
return &Table{
|
||||||
|
m: make(map[string][]Entry),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Define lookup query interface
|
||||||
|
// Lookup looks up entry in the routing table
|
||||||
|
func (t *Table) Lookup() (*Entry, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove removes entry from the routing table
|
||||||
|
func (t *Table) Remove(e *Entry) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update updates routin entry
|
||||||
|
func (t *Table) Update(e *Entry) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size returns the size of the routing table
|
||||||
|
func (t *Table) Size() int {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns text representation of routing table
|
||||||
|
func (t *Table) String() string {
|
||||||
|
return ""
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user