From 47acdf6a4b7fe2c63a05f5866a7a220aa9a34aad Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Mon, 29 Jul 2019 12:40:13 +0100 Subject: [PATCH] move Table to table --- network/router/default.go | 4 ++-- network/router/options.go | 10 ---------- network/router/router.go | 8 -------- network/router/table.go | 29 ++++++++++++----------------- network/router/table_test.go | 4 ++-- 5 files changed, 16 insertions(+), 39 deletions(-) diff --git a/network/router/default.go b/network/router/default.go index 8cf08e51..aa31688d 100644 --- a/network/router/default.go +++ b/network/router/default.go @@ -42,7 +42,7 @@ var ( // router implements default router type router struct { // embed the table - *Table + *table opts Options status Status exit chan struct{} @@ -65,7 +65,7 @@ func newRouter(opts ...Option) Router { } r := &router{ - Table: options.Table, + table: newTable(), opts: options, status: Status{Code: Stopped, Error: nil}, advertWg: &sync.WaitGroup{}, diff --git a/network/router/options.go b/network/router/options.go index ac108b32..240da437 100644 --- a/network/router/options.go +++ b/network/router/options.go @@ -24,8 +24,6 @@ type Options struct { Network string // Registry is the local registry Registry registry.Registry - // Table is routing table - Table *Table } // 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 func DefaultOptions() Options { return Options{ @@ -77,6 +68,5 @@ func DefaultOptions() Options { Address: DefaultAddress, Network: DefaultNetwork, Registry: registry.DefaultRegistry, - Table: NewTable(), } } diff --git a/network/router/router.go b/network/router/router.go index 80ad7e1c..4740a915 100644 --- a/network/router/router.go +++ b/network/router/router.go @@ -22,14 +22,6 @@ type Router interface { Advertise() (<-chan *Advert, error) // Process processes incoming adverts 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(Query) ([]Route, error) // Watch returns a watcher which tracks updates to the routing table diff --git a/network/router/table.go b/network/router/table.go index 75b7a10d..f9cfd23d 100644 --- a/network/router/table.go +++ b/network/router/table.go @@ -15,8 +15,8 @@ var ( ErrDuplicateRoute = errors.New("duplicate route") ) -// Table is an in memory routing table -type Table struct { +// table is an in memory routing table +type table struct { // routes stores service routes routes map[string]map[uint64]Route // watchers stores table watchers @@ -24,16 +24,16 @@ type Table struct { sync.RWMutex } -// NewTable creates a new routing table and returns it -func NewTable(opts ...Option) *Table { - return &Table{ +// newtable creates a new routing table and returns it +func newTable(opts ...Option) *table { + return &table{ routes: make(map[string]map[uint64]Route), watchers: make(map[string]*tableWatcher), } } // 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 sum := r.Hash() @@ -59,7 +59,7 @@ func (t *Table) Create(r Route) error { } // 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 sum := r.Hash() @@ -77,7 +77,7 @@ func (t *Table) Delete(r Route) error { } // 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 sum := r.Hash() @@ -99,7 +99,7 @@ func (t *Table) Update(r Route) error { } // List returns a list of all routes in the table -func (t *Table) List() ([]Route, error) { +func (t *table) List() ([]Route, error) { t.RLock() 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 -func (t *Table) Lookup(q Query) ([]Route, error) { +func (t *table) Lookup(q Query) ([]Route, error) { t.RLock() defer t.RUnlock() @@ -156,7 +156,7 @@ func (t *Table) Lookup(q Query) ([]Route, error) { } // 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 wopts := WatchOptions{ Service: "*", @@ -180,7 +180,7 @@ func (t *Table) Watch(opts ...WatchOption) (Watcher, error) { } // sendEvent sends events to all subscribed watchers -func (t *Table) sendEvent(e *Event) { +func (t *table) sendEvent(e *Event) { t.RLock() defer t.RUnlock() @@ -191,8 +191,3 @@ func (t *Table) sendEvent(e *Event) { } } } - -// String returns debug information -func (t *Table) String() string { - return "table" -} diff --git a/network/router/table_test.go b/network/router/table_test.go index eee734a2..dc35a5f1 100644 --- a/network/router/table_test.go +++ b/network/router/table_test.go @@ -2,8 +2,8 @@ package router import "testing" -func testSetup() (*Table, Route) { - table := NewTable() +func testSetup() (*table, Route) { + table := newTable() route := Route{ Service: "dest.svc",