Skip to content

Commit

Permalink
move Table to table
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Jul 29, 2019
1 parent 4fc9b98 commit 47acdf6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 39 deletions.
4 changes: 2 additions & 2 deletions network/router/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var (
// router implements default router
type router struct {
// embed the table
*Table
*table
opts Options
status Status
exit chan struct{}
Expand All @@ -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{},
Expand Down
10 changes: 0 additions & 10 deletions network/router/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,20 +61,12 @@ 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{
Id: uuid.New().String(),
Address: DefaultAddress,
Network: DefaultNetwork,
Registry: registry.DefaultRegistry,
Table: NewTable(),
}
}
8 changes: 0 additions & 8 deletions network/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 12 additions & 17 deletions network/router/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ 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
watchers map[string]*tableWatcher
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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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: "*",
Expand All @@ -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()

Expand All @@ -191,8 +191,3 @@ func (t *Table) sendEvent(e *Event) {
}
}
}

// String returns debug information
func (t *Table) String() string {
return "table"
}
4 changes: 2 additions & 2 deletions network/router/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 47acdf6

Please sign in to comment.