visual cleanup of router code

This commit is contained in:
Asim Aslam 2019-07-10 07:45:27 +01:00
parent 34967e8e33
commit 0a1b657221
5 changed files with 69 additions and 104 deletions

View File

@ -31,6 +31,8 @@ const (
// router provides default router implementation
type router struct {
// embed the table
table.Table
opts Options
status Status
exit chan struct{}
@ -52,6 +54,7 @@ func newRouter(opts ...Option) Router {
}
return &router{
Table: options.Table,
opts: options,
status: Status{Error: nil, Code: Stopped},
exit: make(chan struct{}),
@ -75,26 +78,6 @@ func (r *router) Options() Options {
return r.opts
}
// ID returns router ID
func (r *router) ID() string {
return r.opts.ID
}
// Table returns routing table
func (r *router) Table() table.Table {
return r.opts.Table
}
// Address returns router's bind address
func (r *router) Address() string {
return r.opts.Address
}
// Network returns the address router advertises to the network
func (r *router) Network() string {
return r.opts.Network
}
// manageServiceRoutes manages routes for a given service.
// It returns error of the routing table action fails.
func (r *router) manageServiceRoutes(service *registry.Service, action string) error {
@ -224,7 +207,7 @@ func (r *router) advertEvents(advType AdvertType, events []*table.Event) {
defer r.advertWg.Done()
a := &Advert{
ID: r.ID(),
Id: r.opts.Id,
Type: advType,
Timestamp: time.Now(),
Events: events,
@ -490,8 +473,8 @@ func (r *router) Advertise() (<-chan *Advert, error) {
return r.advertChan, nil
}
// Update updates the routing table using the advertised values
func (r *router) Update(a *Advert) error {
// Process updates the routing table using the advertised values
func (r *router) Process(a *Advert) error {
// NOTE: event sorting might not be necessary
// copy update events intp new slices
events := make([]*table.Event, len(a.Events))
@ -546,6 +529,6 @@ func (r *router) Stop() error {
}
// String prints debugging information about router
func (r router) String() string {
func (r *router) String() string {
return "router"
}

View File

@ -15,8 +15,8 @@ var (
// Options are router options
type Options struct {
// ID is router id
ID string
// Id is router id
Id string
// Address is router address
Address string
// Gateway is micro network gateway
@ -29,10 +29,10 @@ type Options struct {
Table table.Table
}
// ID sets Router ID
func ID(id string) Option {
// Id sets Router Id
func Id(id string) Option {
return func(o *Options) {
o.ID = id
o.Id = id
}
}
@ -57,8 +57,8 @@ func Network(n string) Option {
}
}
// RoutingTable sets the routing table
func RoutingTable(t table.Table) Option {
// Table sets the routing table
func Table(t table.Table) Option {
return func(o *Options) {
o.Table = t
}
@ -74,7 +74,7 @@ func Registry(r registry.Registry) Option {
// DefaultOptions returns router default options
func DefaultOptions() Options {
return Options{
ID: uuid.New().String(),
Id: uuid.New().String(),
Address: DefaultAddress,
Network: DefaultNetwork,
Registry: registry.DefaultRegistry,

View File

@ -7,43 +7,6 @@ import (
"github.com/micro/go-micro/network/router/table"
)
var (
// DefaultRouter is default network router
DefaultRouter = NewRouter()
)
// Router is an interface for a routing control plane
type Router interface {
// Init initializes the router with options
Init(...Option) error
// Options returns the router options
Options() Options
// ID returns the ID of the router
ID() string
// Address returns the router adddress
Address() string
// Network returns the network address of the router
Network() string
// Table returns the routing table
Table() table.Table
// Advertise advertises routes to the network
Advertise() (<-chan *Advert, error)
// Update updates the routing table
Update(*Advert) error
// Status returns router status
Status() Status
// Stop stops the router
Stop() error
// String returns debug info
String() string
}
// Option used by the router
type Option func(*Options)
// AdvertType is route advertisement type
type AdvertType int
const (
// Announce is advertised when the router announces itself
Announce AdvertType = iota
@ -51,6 +14,15 @@ const (
Update
)
const (
// Running means the router is up and running
Running StatusCode = iota
// Stopped means the router has been stopped
Stopped
// Error means the router has encountered error
Error
)
// String returns string representation of update event
func (at AdvertType) String() string {
switch at {
@ -63,10 +35,50 @@ func (at AdvertType) String() string {
}
}
// String returns human readable status code
func (sc StatusCode) String() string {
switch sc {
case Running:
return "RUNNING"
case Stopped:
return "STOPPED"
case Error:
return "ERROR"
default:
return "UNKNOWN"
}
}
// Router is an interface for a routing control plane
type Router interface {
// Init initializes the router with options
Init(...Option) error
// Options returns the router options
Options() Options
// Table returns the routing table
table.Table
// Advertise advertises routes to the network
Advertise() (<-chan *Advert, error)
// Process processes incoming adverts
Process(*Advert) error
// Status returns router status
Status() Status
// Stop stops the router
Stop() error
// String returns debug info
String() string
}
// Option used by the router
type Option func(*Options)
// AdvertType is route advertisement type
type AdvertType int
// Advert contains a list of events advertised by the router to the network
type Advert struct {
// ID is the router ID
ID string
// Id is the router Id
Id string
// Type is type of advert
Type AdvertType
// Timestamp marks the time when the update is sent
@ -89,29 +101,11 @@ type Status struct {
Code StatusCode
}
const (
// Running means the router is up and running
Running StatusCode = iota
// Stopped means the router has been stopped
Stopped
// Error means the router has encountered error
Error
var (
// DefaultRouter is default network router
DefaultRouter = NewRouter()
)
// String returns human readable status code
func (sc StatusCode) String() string {
switch sc {
case Running:
return "RUNNING"
case Stopped:
return "STOPPED"
case Error:
return "ERROR"
default:
return "UNKNOWN"
}
}
// NewRouter creates new Router and returns it
func NewRouter(opts ...Option) Router {
return newRouter(opts...)

View File

@ -34,11 +34,5 @@ func (r *Route) Hash() uint64 {
h := fnv.New64()
h.Reset()
h.Write([]byte(r.Service + r.Address + r.Gateway + r.Network + r.Link))
return h.Sum64()
}
// String returns human readable route
func (r Route) String() string {
return "route"
}

View File

@ -13,10 +13,6 @@ var (
// Table defines routing table interface
type Table interface {
// Init initializes the router with options
Init(...TableOption) error
// Options returns the router options
Options() TableOptions
// Add adds new route to the routing table
Add(Route) error
// Delete deletes existing route from the routing table
@ -31,8 +27,6 @@ type Table interface {
Watch(opts ...WatchOption) (Watcher, error)
// Size returns the size of the routing table
Size() int
// String prints the routing table
String() string
}
// TableOption used by the routing table