move Table to table

This commit is contained in:
Asim Aslam 2019-07-29 12:40:13 +01:00
parent 4fc9b9821a
commit 47acdf6a4b
5 changed files with 16 additions and 39 deletions

View File

@ -42,7 +42,7 @@ var (
// router implements default router // router implements default router
type router struct { type router struct {
// embed the table // embed the table
*Table *table
opts Options opts Options
status Status status Status
exit chan struct{} exit chan struct{}
@ -65,7 +65,7 @@ func newRouter(opts ...Option) Router {
} }
r := &router{ r := &router{
Table: options.Table, table: newTable(),
opts: options, opts: options,
status: Status{Code: Stopped, Error: nil}, status: Status{Code: Stopped, Error: nil},
advertWg: &sync.WaitGroup{}, advertWg: &sync.WaitGroup{},

View File

@ -24,8 +24,6 @@ type Options struct {
Network string Network string
// Registry is the local registry // Registry is the local registry
Registry registry.Registry Registry registry.Registry
// Table is routing table
Table *Table
} }
// Id sets Router Id // Id sets Router Id
@ -63,13 +61,6 @@ func Registry(r registry.Registry) Option {
} }
} }
// RoutingTable sets the routing table
func RoutingTable(t *Table) Option {
return func(o *Options) {
o.Table = t
}
}
// DefaultOptions returns router default options // DefaultOptions returns router default options
func DefaultOptions() Options { func DefaultOptions() Options {
return Options{ return Options{
@ -77,6 +68,5 @@ func DefaultOptions() Options {
Address: DefaultAddress, Address: DefaultAddress,
Network: DefaultNetwork, Network: DefaultNetwork,
Registry: registry.DefaultRegistry, Registry: registry.DefaultRegistry,
Table: NewTable(),
} }
} }

View File

@ -22,14 +22,6 @@ type Router interface {
Advertise() (<-chan *Advert, error) Advertise() (<-chan *Advert, error)
// Process processes incoming adverts // Process processes incoming adverts
Process(*Advert) error Process(*Advert) error
// Create new route in the routing table
Create(Route) error
// Delete existing route from the routing table
Delete(Route) error
// Update exiting route in the routing table
Update(Route) error
// List lists all routes in the routing table
List() ([]Route, error)
// Lookup queries routes in the routing table // Lookup queries routes in the routing table
Lookup(Query) ([]Route, error) Lookup(Query) ([]Route, error)
// Watch returns a watcher which tracks updates to the routing table // Watch returns a watcher which tracks updates to the routing table

View File

@ -15,8 +15,8 @@ var (
ErrDuplicateRoute = errors.New("duplicate route") ErrDuplicateRoute = errors.New("duplicate route")
) )
// Table is an in memory routing table // table is an in memory routing table
type Table struct { type table struct {
// routes stores service routes // routes stores service routes
routes map[string]map[uint64]Route routes map[string]map[uint64]Route
// watchers stores table watchers // watchers stores table watchers
@ -24,16 +24,16 @@ type Table struct {
sync.RWMutex sync.RWMutex
} }
// NewTable creates a new routing table and returns it // newtable creates a new routing table and returns it
func NewTable(opts ...Option) *Table { func newTable(opts ...Option) *table {
return &Table{ return &table{
routes: make(map[string]map[uint64]Route), routes: make(map[string]map[uint64]Route),
watchers: make(map[string]*tableWatcher), watchers: make(map[string]*tableWatcher),
} }
} }
// Create creates new route in the routing table // Create creates new route in the routing table
func (t *Table) Create(r Route) error { func (t *table) Create(r Route) error {
service := r.Service service := r.Service
sum := r.Hash() sum := r.Hash()
@ -59,7 +59,7 @@ func (t *Table) Create(r Route) error {
} }
// Delete deletes the route from the routing table // Delete deletes the route from the routing table
func (t *Table) Delete(r Route) error { func (t *table) Delete(r Route) error {
service := r.Service service := r.Service
sum := r.Hash() sum := r.Hash()
@ -77,7 +77,7 @@ func (t *Table) Delete(r Route) error {
} }
// Update updates routing table with the new route // Update updates routing table with the new route
func (t *Table) Update(r Route) error { func (t *table) Update(r Route) error {
service := r.Service service := r.Service
sum := r.Hash() sum := r.Hash()
@ -99,7 +99,7 @@ func (t *Table) Update(r Route) error {
} }
// List returns a list of all routes in the table // List returns a list of all routes in the table
func (t *Table) List() ([]Route, error) { func (t *table) List() ([]Route, error) {
t.RLock() t.RLock()
defer t.RUnlock() defer t.RUnlock()
@ -135,7 +135,7 @@ func findRoutes(routes map[uint64]Route, network, router string) []Route {
} }
// Lookup queries routing table and returns all routes that match the lookup query // Lookup queries routing table and returns all routes that match the lookup query
func (t *Table) Lookup(q Query) ([]Route, error) { func (t *table) Lookup(q Query) ([]Route, error) {
t.RLock() t.RLock()
defer t.RUnlock() defer t.RUnlock()
@ -156,7 +156,7 @@ func (t *Table) Lookup(q Query) ([]Route, error) {
} }
// Watch returns routing table entry watcher // Watch returns routing table entry watcher
func (t *Table) Watch(opts ...WatchOption) (Watcher, error) { func (t *table) Watch(opts ...WatchOption) (Watcher, error) {
// by default watch everything // by default watch everything
wopts := WatchOptions{ wopts := WatchOptions{
Service: "*", Service: "*",
@ -180,7 +180,7 @@ func (t *Table) Watch(opts ...WatchOption) (Watcher, error) {
} }
// sendEvent sends events to all subscribed watchers // sendEvent sends events to all subscribed watchers
func (t *Table) sendEvent(e *Event) { func (t *table) sendEvent(e *Event) {
t.RLock() t.RLock()
defer t.RUnlock() defer t.RUnlock()
@ -191,8 +191,3 @@ func (t *Table) sendEvent(e *Event) {
} }
} }
} }
// String returns debug information
func (t *Table) String() string {
return "table"
}

View File

@ -2,8 +2,8 @@ package router
import "testing" import "testing"
func testSetup() (*Table, Route) { func testSetup() (*table, Route) {
table := NewTable() table := newTable()
route := Route{ route := Route{
Service: "dest.svc", Service: "dest.svc",