mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-24 08:02:32 +02:00
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 {
|
||||
opts Options
|
||||
goss registry.Registry
|
||||
t *Table
|
||||
}
|
||||
|
||||
func newRouter(opts ...Option) Router {
|
||||
// TODO: for now default options
|
||||
goss := gossip.NewRegistry()
|
||||
|
||||
// TODO: figure out how to supply gossip registry options
|
||||
r := &router{
|
||||
goss: goss,
|
||||
goss: gossip.NewRegistry(),
|
||||
t: NewTable(),
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
@ -25,6 +25,7 @@ func newRouter(opts ...Option) Router {
|
||||
return r
|
||||
}
|
||||
|
||||
// Init initializes router with given options
|
||||
func (r *router) Init(opts ...Option) error {
|
||||
for _, o := range opts {
|
||||
o(&r.opts)
|
||||
@ -32,26 +33,45 @@ func (r *router) Init(opts ...Option) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Options returns router options
|
||||
func (r *router) Options() Options {
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (r *router) List() ([]*Route, error) {
|
||||
return nil, nil
|
||||
// Table returns routing table
|
||||
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 {
|
||||
return ""
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/micro/go-micro/registry"
|
||||
)
|
||||
@ -15,17 +14,33 @@ type Options struct {
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
// RouteOption allows to soecify routing table options
|
||||
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
|
||||
// Registry allows to set local service registry
|
||||
func Registry(r registry.Registry) Option {
|
||||
return func(o *Options) {
|
||||
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
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/micro/go-micro/registry"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultRouter returns default micro router
|
||||
DefaultRouter = NewRouter()
|
||||
)
|
||||
|
||||
// Router is micro network router
|
||||
type Router interface {
|
||||
// Initi initializes Router with options
|
||||
Init(...Option) error
|
||||
// Options returns Router options
|
||||
Options() Options
|
||||
// AddRoute adds new service route
|
||||
AddRoute(*Route, ...RouteOption) error
|
||||
// RemoveRoute removes service route
|
||||
RemoveRoute(*Route) error
|
||||
// GetRoute returns list of routes for service
|
||||
GetRoute(*Service) ([]*Route, error)
|
||||
// List returns all routes
|
||||
List() ([]*Route, error)
|
||||
// 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
|
||||
// String implemens fmt.Stringer interface
|
||||
String() string
|
||||
}
|
||||
@ -28,22 +35,21 @@ type Router interface {
|
||||
// Option used by the Router
|
||||
type Option func(*Options)
|
||||
|
||||
var (
|
||||
DefaultRouter = NewRouter()
|
||||
// RouteOption is used by Router for adding routing table entries
|
||||
type RouteOption func(*RouteOptions)
|
||||
|
||||
// Not found error when Get is called
|
||||
ErrNotFound = errors.New("route not found")
|
||||
)
|
||||
// QueryOption is used to defined routing table lookup query
|
||||
type QueryOption func(*QueryOptions)
|
||||
|
||||
// NewRouter creates new Router and returns it
|
||||
func NewRouter(opts ...Option) Router {
|
||||
// set Registry to DefaultRegistry
|
||||
opt := Options{
|
||||
// router registry to DefaultRegistry
|
||||
ropts := Options{
|
||||
Registry: registry.DefaultRegistry,
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&opt)
|
||||
o(&ropts)
|
||||
}
|
||||
|
||||
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