mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-24 10:07:04 +02:00
Simplified API. Correct Router initialization. Debug printing.
This commit is contained in:
parent
da18ea4ab5
commit
5899134b66
@ -4,31 +4,36 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/go-micro/registry"
|
||||
"github.com/micro/go-micro/registry/gossip"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
)
|
||||
|
||||
type router struct {
|
||||
opts Options
|
||||
goss registry.Registry
|
||||
table Table
|
||||
id uuid.UUID
|
||||
}
|
||||
|
||||
func newRouter(opts ...Option) Router {
|
||||
// TODO: figure out how to supply gossip registry options
|
||||
r := &router{
|
||||
goss: gossip.NewRegistry(),
|
||||
table: NewTable(),
|
||||
id: uuid.New(),
|
||||
// set default options
|
||||
options := Options{
|
||||
Table: NewTable(),
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&r.opts)
|
||||
o(&options)
|
||||
}
|
||||
|
||||
// TODO: need to start some gossip.Registry watch here
|
||||
goss := gossip.NewRegistry(
|
||||
gossip.Address(options.GossipAddr),
|
||||
)
|
||||
|
||||
r := &router{
|
||||
opts: options,
|
||||
goss: goss,
|
||||
}
|
||||
|
||||
// TODO: start gossip.Registry watch here
|
||||
|
||||
return r
|
||||
}
|
||||
@ -46,37 +51,9 @@ func (r *router) Options() Options {
|
||||
return r.opts
|
||||
}
|
||||
|
||||
// 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 Route) error {
|
||||
return r.table.Add(e)
|
||||
}
|
||||
|
||||
// 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 Route) error {
|
||||
return r.table.Remove(e)
|
||||
}
|
||||
|
||||
// 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(opts ...RouteOption) error {
|
||||
return r.table.Update(opts...)
|
||||
}
|
||||
|
||||
// Lookup makes a query lookup and returns all matching entries
|
||||
func (r *router) Lookup(q Query) ([]*Route, error) {
|
||||
return nil, ErrNotImplemented
|
||||
}
|
||||
|
||||
// Table returns routing table
|
||||
func (r *router) Table() Table {
|
||||
return r.table
|
||||
}
|
||||
|
||||
// Network returns router's micro network
|
||||
func (r *router) Network() string {
|
||||
return r.opts.Network
|
||||
return r.opts.Table
|
||||
}
|
||||
|
||||
// Address returns router's bind address
|
||||
@ -84,21 +61,29 @@ func (r *router) Address() string {
|
||||
return r.opts.Address
|
||||
}
|
||||
|
||||
// Network returns router's micro network
|
||||
func (r *router) Network() string {
|
||||
return r.opts.NetworkAddr
|
||||
}
|
||||
|
||||
// String prints debugging information about router
|
||||
func (r *router) String() string {
|
||||
sb := &strings.Builder{}
|
||||
|
||||
s := fmt.Sprintf("Router ID: %s\n", r.id.String())
|
||||
sb.WriteString(s)
|
||||
table := tablewriter.NewWriter(sb)
|
||||
table.SetHeader([]string{"ID", "Address", "Gossip", "Network", "Table"})
|
||||
|
||||
s = fmt.Sprintf("Router Local Address: %s\n", r.opts.Address)
|
||||
sb.WriteString(s)
|
||||
data := []string{
|
||||
r.opts.ID,
|
||||
r.opts.Address,
|
||||
r.opts.GossipAddr,
|
||||
r.opts.NetworkAddr,
|
||||
fmt.Sprintf("%d", r.opts.Table.Size()),
|
||||
}
|
||||
table.Append(data)
|
||||
|
||||
s = fmt.Sprintf("Router Network Address: %s\n", r.opts.Network)
|
||||
sb.WriteString(s)
|
||||
|
||||
s = fmt.Sprintf("Routing table size: %d\n", r.opts.Table.Size())
|
||||
sb.WriteString(s)
|
||||
// render table into sb
|
||||
table.Render()
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
@ -16,9 +16,8 @@ type RouteOptions struct {
|
||||
DestAddr string
|
||||
// Hop is the next route hop
|
||||
Hop Router
|
||||
// SrcAddr defines local routing address
|
||||
// On local networkss, this will be the address of local router
|
||||
SrcAddr string
|
||||
// Network defines micro network
|
||||
Network string
|
||||
// Metric is route cost metric
|
||||
Metric int
|
||||
// Policy defines route addition policy
|
||||
@ -39,10 +38,10 @@ func Hop(r Router) RouteOption {
|
||||
}
|
||||
}
|
||||
|
||||
// SrcAddr sets source address
|
||||
func SrcAddr(a string) RouteOption {
|
||||
// Network sets micro network
|
||||
func Network(n string) RouteOption {
|
||||
return func(o *RouteOptions) {
|
||||
o.SrcAddr = a
|
||||
o.Network = n
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,10 +6,14 @@ import (
|
||||
|
||||
// Options allows to set Router options
|
||||
type Options struct {
|
||||
// ID is router ID
|
||||
ID string
|
||||
// Address is router address
|
||||
Address string
|
||||
// Network defines micro network address
|
||||
Network string
|
||||
// GossipAddr is router gossip address
|
||||
GossipAddr string
|
||||
// NetworkAddr defines micro network address
|
||||
NetworkAddr string
|
||||
// RIB is Routing Information Base
|
||||
RIB RIB
|
||||
// Table is routing table
|
||||
@ -18,6 +22,13 @@ type Options struct {
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
// ID sets Router ID
|
||||
func ID(id string) Option {
|
||||
return func(o *Options) {
|
||||
o.ID = id
|
||||
}
|
||||
}
|
||||
|
||||
// Address allows to set router address
|
||||
func Address(a string) Option {
|
||||
return func(o *Options) {
|
||||
@ -25,10 +36,17 @@ func Address(a string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// Network allows to set router network
|
||||
func Network(n string) Option {
|
||||
// GossipAddress allows to set router address
|
||||
func GossipAddress(a string) Option {
|
||||
return func(o *Options) {
|
||||
o.Network = n
|
||||
o.GossipAddr = a
|
||||
}
|
||||
}
|
||||
|
||||
// NetworkAddr allows to set router network
|
||||
func NetworkAddr(n string) Option {
|
||||
return func(o *Options) {
|
||||
o.NetworkAddr = n
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,19 +7,11 @@ type Router interface {
|
||||
Init(...Option) error
|
||||
// Options returns Router options
|
||||
Options() Options
|
||||
// Add adds new entry into routing table
|
||||
Add(Route) error
|
||||
// Remove removes entry from the routing table
|
||||
Remove(Route) error
|
||||
// Update updates entry in the routing table
|
||||
Update(...RouteOption) error
|
||||
// Lookup queries the routing table and returns matching entries
|
||||
Lookup(Query) ([]*Route, error)
|
||||
// Table returns routing table
|
||||
Table() Table
|
||||
// Address returns the router bind adddress
|
||||
// Address returns router gossip adddress
|
||||
Address() string
|
||||
// Network returns router's micro network bind address
|
||||
// Network returns micro network address
|
||||
Network() string
|
||||
// String implemens fmt.Stringer interface
|
||||
String() string
|
||||
@ -42,15 +34,5 @@ type QueryOption func(*QueryOptions)
|
||||
|
||||
// NewRouter creates new Router and returns it
|
||||
func NewRouter(opts ...Option) Router {
|
||||
// set default options
|
||||
ropts := Options{
|
||||
// Default table
|
||||
Table: NewTable(),
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&ropts)
|
||||
}
|
||||
|
||||
return newRouter(opts...)
|
||||
}
|
||||
|
@ -137,28 +137,31 @@ func (t *table) String() string {
|
||||
|
||||
// create nice table printing structure
|
||||
table := tablewriter.NewWriter(sb)
|
||||
table.SetHeader([]string{"Dest", "Hop", "Src", "Metric"})
|
||||
table.SetHeader([]string{"Service", "Gateway", "Network", "Metric"})
|
||||
|
||||
for _, route := range t.m {
|
||||
strRoute := []string{
|
||||
route.Options().DestAddr,
|
||||
route.Options().Hop.Address(),
|
||||
fmt.Sprintf("%d", route.Options().SrcAddr),
|
||||
route.Options().Network,
|
||||
fmt.Sprintf("%d", route.Options().Metric),
|
||||
}
|
||||
table.Append(strRoute)
|
||||
}
|
||||
|
||||
// render table into sb
|
||||
table.Render()
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func (t *table) hash(r Route) uint64 {
|
||||
srcAddr := r.Options().SrcAddr
|
||||
destAddr := r.Options().DestAddr
|
||||
routerAddr := r.Options().Hop.Address()
|
||||
network := r.Options().Network
|
||||
|
||||
t.h.Reset()
|
||||
t.h.Write([]byte(srcAddr + destAddr + routerAddr))
|
||||
t.h.Write([]byte(destAddr + routerAddr + network))
|
||||
|
||||
return t.h.Sum64()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user