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
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{},

View File

@ -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(),
}
}

View File

@ -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

View File

@ -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"
}

View File

@ -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",