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

View File

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

View File

@ -7,43 +7,6 @@ import (
"github.com/micro/go-micro/network/router/table" "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 ( const (
// Announce is advertised when the router announces itself // Announce is advertised when the router announces itself
Announce AdvertType = iota Announce AdvertType = iota
@ -51,6 +14,15 @@ const (
Update 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 // String returns string representation of update event
func (at AdvertType) String() string { func (at AdvertType) String() string {
switch at { 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 // Advert contains a list of events advertised by the router to the network
type Advert struct { type Advert struct {
// ID is the router ID // Id is the router Id
ID string Id string
// Type is type of advert // Type is type of advert
Type AdvertType Type AdvertType
// Timestamp marks the time when the update is sent // Timestamp marks the time when the update is sent
@ -89,29 +101,11 @@ type Status struct {
Code StatusCode Code StatusCode
} }
const ( var (
// Running means the router is up and running // DefaultRouter is default network router
Running StatusCode = iota DefaultRouter = NewRouter()
// Stopped means the router has been stopped
Stopped
// Error means the router has encountered error
Error
) )
// 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 // NewRouter creates new Router and returns it
func NewRouter(opts ...Option) Router { func NewRouter(opts ...Option) Router {
return newRouter(opts...) return newRouter(opts...)

View File

@ -34,11 +34,5 @@ func (r *Route) Hash() uint64 {
h := fnv.New64() h := fnv.New64()
h.Reset() h.Reset()
h.Write([]byte(r.Service + r.Address + r.Gateway + r.Network + r.Link)) h.Write([]byte(r.Service + r.Address + r.Gateway + r.Network + r.Link))
return h.Sum64() 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 // Table defines routing table interface
type 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 adds new route to the routing table
Add(Route) error Add(Route) error
// Delete deletes existing route from the routing table // Delete deletes existing route from the routing table
@ -31,8 +27,6 @@ type Table interface {
Watch(opts ...WatchOption) (Watcher, error) Watch(opts ...WatchOption) (Watcher, error)
// Size returns the size of the routing table // Size returns the size of the routing table
Size() int Size() int
// String prints the routing table
String() string
} }
// TableOption used by the routing table // TableOption used by the routing table