Redefined and polished some interfaces and data structures.

This commit is contained in:
Milos Gajdos 2019-06-07 13:29:09 +01:00
parent ee8b6b3114
commit e4311c3a10
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F
4 changed files with 75 additions and 31 deletions

View File

@ -8,7 +8,7 @@ import (
type router struct { type router struct {
opts Options opts Options
goss registry.Registry goss registry.Registry
t *Table t Table
} }
func newRouter(opts ...Option) Router { func newRouter(opts ...Option) Router {
@ -62,13 +62,13 @@ func (r *router) Lookup(q Query) ([]*Entry, error) {
} }
// Table returns routing table // Table returns routing table
func (r *router) Table() *Table { func (r *router) Table() Table {
return nil return nil
} }
// Address returns router's network address // Address returns router's network address
func (r *router) Address() string { func (r *router) Address() string {
return "" return r.opts.Address
} }
// String prints debugging information about router // String prints debugging information about router

View File

@ -2,22 +2,38 @@ package router
import ( import (
"context" "context"
"github.com/micro/go-micro/registry"
) )
// Options allows to set Router options // Options allows to set Router options
type Options struct { type Options struct {
// Registry is route source registry i.e. local registry // Address is router address
Registry registry.Registry Address string
// RIB is Routing Information Base
RIB RIB
// Table is routing table
Table Table
// Context stores arbitrary options // Context stores arbitrary options
Context context.Context Context context.Context
} }
// Registry allows to set local service registry // RIBase allows to configure RIB
func Registry(r registry.Registry) Option { func RIBase(r RIB) Option {
return func(o *Options) { return func(o *Options) {
o.Registry = r o.RIB = r
}
}
// Address allows to set router address
func Address(a string) Option {
return func(o *Options) {
o.Address = a
}
}
// RoutingTable allows to specify custom routing table
func RoutingTable(t Table) Option {
return func(o *Options) {
o.Table = t
} }
} }

View File

@ -1,10 +1,6 @@
// Package router provides an interface for micro network routers // Package router provides an interface for micro network routers
package router package router
import (
"github.com/micro/go-micro/registry"
)
var ( var (
// DefaultRouter returns default micro router // DefaultRouter returns default micro router
DefaultRouter = NewRouter() DefaultRouter = NewRouter()
@ -25,13 +21,19 @@ type Router interface {
// Lookup queries the routing table and returns matching entries // Lookup queries the routing table and returns matching entries
Lookup(Query) ([]*Entry, error) Lookup(Query) ([]*Entry, error)
// Table returns routing table // Table returns routing table
Table() *Table Table() Table
// Address is Router adddress // Address is Router adddress
Address() string Address() string
// String implemens fmt.Stringer interface // String implemens fmt.Stringer interface
String() string String() string
} }
// RIB is Routing Information Base
type RIB interface {
// String returns debug info
String() string
}
// Option used by the Router // Option used by the Router
type Option func(*Options) type Option func(*Options)
@ -43,9 +45,9 @@ 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 {
// router registry to DefaultRegistry // set default options
ropts := Options{ ropts := Options{
Registry: registry.DefaultRegistry, Table: DefaultTable,
} }
for _, o := range opts { for _, o := range opts {

View File

@ -3,12 +3,34 @@ package router
import "errors" import "errors"
var ( var (
// ErrRouteNotFound is returned when no rout was found // DefaultRouter returns default micro router
DefaultTable = NewTable()
// ErrRouteNotFound is returned when no route was found
ErrRouteNotFound = errors.New("route not found") ErrRouteNotFound = errors.New("route not found")
// ErrDuplicateRoute is return when route already exists
ErrDuplicateRoute = errors.New("duplicate route")
) )
// Table is routing table
type Table interface {
// Add adds new route to the table
Add(*Entry) error
// Remove removes route from the table
Remove(*Entry) error
// Update updates route in the table
Update(*Entry) error
// Lookup looks up routes in the table
Lookup(Query) ([]*Entry, error)
// Size returns the size of the table
Size() int
// String prints the routing table
String() string
}
// Entry is micro network routing table entry // Entry is micro network routing table entry
type Entry struct { type Entry struct {
// DestAddr is destination address
DestAddr string
// NetID is micro network ID // NetID is micro network ID
NetID string NetID string
// Hop is the next route hop // Hop is the next route hop
@ -17,42 +39,46 @@ type Entry struct {
Metric int Metric int
} }
// Table is routing table // table is routing table
// It maps service name to routes // It maps service name to routes
type Table struct { type table struct {
// m stores routing table map // m stores routing table map
m map[string][]Entry m map[string][]Entry
} }
// NewTable creates new routing table and returns it // NewTable creates new routing table and returns it
func NewTable() *Table { func NewTable() Table {
return &Table{ return &table{
m: make(map[string][]Entry), m: make(map[string][]Entry),
} }
} }
// TODO: Define lookup query interface // Add adds new routing entry
// Lookup looks up entry in the routing table func (t *table) Add(e *Entry) error {
func (t *Table) Lookup() (*Entry, error) { return nil
return nil, nil
} }
// Remove removes entry from the routing table // Remove removes entry from the routing table
func (t *Table) Remove(e *Entry) error { func (t *table) Remove(e *Entry) error {
return nil return nil
} }
// Update updates routin entry // Update updates routin entry
func (t *Table) Update(e *Entry) error { func (t *table) Update(e *Entry) error {
return nil return nil
} }
// Lookup looks up entry in the routing table
func (t *table) Lookup(q Query) ([]*Entry, error) {
return nil, nil
}
// Size returns the size of the routing table // Size returns the size of the routing table
func (t *Table) Size() int { func (t *table) Size() int {
return 0 return len(t.m)
} }
// String returns text representation of routing table // String returns text representation of routing table
func (t *Table) String() string { func (t *table) String() string {
return "" return ""
} }