Table.Add is now Table.Create. Insesrt event is now Create event.

This commit is contained in:
Milos Gajdos 2019-07-10 17:46:22 +01:00
parent 86dfa82dfa
commit 8f5aed707e
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F
5 changed files with 24 additions and 43 deletions

View File

@ -95,7 +95,7 @@ func (r *router) manageServiceRoutes(service *registry.Service, action string) e
} }
switch action { switch action {
case "insert", "create": case "insert", "create":
if err := r.opts.Table.Add(route); err != nil && err != table.ErrDuplicateRoute { if err := r.opts.Table.Create(route); err != nil && err != table.ErrDuplicateRoute {
return fmt.Errorf("failed adding route for service %s: %s", service.Name, err) return fmt.Errorf("failed adding route for service %s: %s", service.Name, err)
} }
case "delete": case "delete":
@ -227,7 +227,7 @@ func isFlapping(curr, prev *table.Event) bool {
return true return true
} }
if curr.Type == table.Insert && prev.Type == table.Delete || curr.Type == table.Delete && prev.Type == table.Insert { if curr.Type == table.Create && prev.Type == table.Delete || curr.Type == table.Delete && prev.Type == table.Create {
return true return true
} }
@ -389,7 +389,7 @@ func (r *router) Advertise() (<-chan *Advert, error) {
events := make([]*table.Event, len(routes)) events := make([]*table.Event, len(routes))
for i, route := range routes { for i, route := range routes {
event := &table.Event{ event := &table.Event{
Type: table.Insert, Type: table.Create,
Timestamp: time.Now(), Timestamp: time.Now(),
Route: route, Route: route,
} }
@ -406,7 +406,7 @@ func (r *router) Advertise() (<-chan *Advert, error) {
Network: "*", Network: "*",
Metric: table.DefaultLocalMetric, Metric: table.DefaultLocalMetric,
} }
if err := r.opts.Table.Add(route); err != nil { if err := r.opts.Table.Create(route); err != nil {
return nil, fmt.Errorf("failed adding default gateway route: %s", err) return nil, fmt.Errorf("failed adding default gateway route: %s", err)
} }
} }

View File

@ -2,6 +2,7 @@ package table
import ( import (
"sync" "sync"
"time"
"github.com/google/uuid" "github.com/google/uuid"
) )
@ -51,8 +52,8 @@ func (t *table) Options() TableOptions {
return t.opts return t.opts
} }
// Add adds a route to the routing table // Create creates new route in the routing table
func (t *table) Add(r Route) error { func (t *table) Create(r Route) error {
service := r.Service service := r.Service
sum := r.Hash() sum := r.Hash()
@ -63,14 +64,14 @@ func (t *table) Add(r Route) error {
if _, ok := t.m[service]; !ok { if _, ok := t.m[service]; !ok {
t.m[service] = make(map[uint64]Route) t.m[service] = make(map[uint64]Route)
t.m[service][sum] = r t.m[service][sum] = r
go t.sendEvent(&Event{Type: Insert, Route: r}) go t.sendEvent(&Event{Type: Create, Timestamp: time.Now(), Route: r})
return nil return nil
} }
// add new route to the table for the route destination // add new route to the table for the route destination
if _, ok := t.m[service][sum]; !ok { if _, ok := t.m[service][sum]; !ok {
t.m[service][sum] = r t.m[service][sum] = r
go t.sendEvent(&Event{Type: Insert, Route: r}) go t.sendEvent(&Event{Type: Create, Timestamp: time.Now(), Route: r})
return nil return nil
} }
@ -90,7 +91,7 @@ func (t *table) Delete(r Route) error {
} }
delete(t.m[service], sum) delete(t.m[service], sum)
go t.sendEvent(&Event{Type: Delete, Route: r}) go t.sendEvent(&Event{Type: Delete, Timestamp: time.Now(), Route: r})
return nil return nil
} }
@ -111,7 +112,7 @@ func (t *table) Update(r Route) error {
// if the route has been found update it // if the route has been found update it
if _, ok := t.m[service][sum]; ok { if _, ok := t.m[service][sum]; ok {
t.m[service][sum] = r t.m[service][sum] = r
go t.sendEvent(&Event{Type: Update, Route: r}) go t.sendEvent(&Event{Type: Update, Timestamp: time.Now(), Route: r})
return nil return nil
} }

View File

@ -16,11 +16,11 @@ func testSetup() (Table, Route) {
return table, route return table, route
} }
func TestAdd(t *testing.T) { func TestCreate(t *testing.T) {
table, route := testSetup() table, route := testSetup()
testTableSize := table.Size() testTableSize := table.Size()
if err := table.Add(route); err != nil { if err := table.Create(route); err != nil {
t.Errorf("error adding route: %s", err) t.Errorf("error adding route: %s", err)
} }
testTableSize += 1 testTableSize += 1
@ -28,7 +28,7 @@ func TestAdd(t *testing.T) {
// adds new route for the original destination // adds new route for the original destination
route.Gateway = "dest.gw2" route.Gateway = "dest.gw2"
if err := table.Add(route); err != nil { if err := table.Create(route); err != nil {
t.Errorf("error adding route: %s", err) t.Errorf("error adding route: %s", err)
} }
testTableSize += 1 testTableSize += 1
@ -38,7 +38,7 @@ func TestAdd(t *testing.T) {
} }
// adding the same route under Insert policy must error // adding the same route under Insert policy must error
if err := table.Add(route); err != ErrDuplicateRoute { if err := table.Create(route); err != ErrDuplicateRoute {
t.Errorf("error adding route. Expected error: %s, found: %s", ErrDuplicateRoute, err) t.Errorf("error adding route. Expected error: %s, found: %s", ErrDuplicateRoute, err)
} }
} }
@ -47,7 +47,7 @@ func TestDelete(t *testing.T) {
table, route := testSetup() table, route := testSetup()
testTableSize := table.Size() testTableSize := table.Size()
if err := table.Add(route); err != nil { if err := table.Create(route); err != nil {
t.Errorf("error adding route: %s", err) t.Errorf("error adding route: %s", err)
} }
testTableSize += 1 testTableSize += 1
@ -77,7 +77,7 @@ func TestUpdate(t *testing.T) {
table, route := testSetup() table, route := testSetup()
testTableSize := table.Size() testTableSize := table.Size()
if err := table.Add(route); err != nil { if err := table.Create(route); err != nil {
t.Errorf("error adding route: %s", err) t.Errorf("error adding route: %s", err)
} }
testTableSize += 1 testTableSize += 1
@ -113,7 +113,7 @@ func TestList(t *testing.T) {
for i := 0; i < len(svc); i++ { for i := 0; i < len(svc); i++ {
route.Service = svc[i] route.Service = svc[i]
if err := table.Add(route); err != nil { if err := table.Create(route); err != nil {
t.Errorf("error adding route: %s", err) t.Errorf("error adding route: %s", err)
} }
} }
@ -143,7 +143,7 @@ func TestLookup(t *testing.T) {
route.Service = svc[i] route.Service = svc[i]
route.Network = net[i] route.Network = net[i]
route.Gateway = gw[i] route.Gateway = gw[i]
if err := table.Add(route); err != nil { if err := table.Create(route); err != nil {
t.Errorf("error adding route: %s", err) t.Errorf("error adding route: %s", err)
} }
} }

View File

@ -13,8 +13,8 @@ var (
// Table defines routing table interface // Table defines routing table interface
type Table interface { type Table interface {
// Add adds new route to the routing table // Create new route in the routing table
Add(Route) error Create(Route) error
// Delete deletes existing route from the routing table // Delete deletes existing route from the routing table
Delete(Route) error Delete(Route) error
// Update updates route in the routing table // Update updates route in the routing table

View File

@ -2,7 +2,6 @@ package table
import ( import (
"errors" "errors"
"fmt"
"time" "time"
) )
@ -15,28 +14,14 @@ var (
type EventType int type EventType int
const ( const (
// Insert is emitted when a new route has been inserted // Create is emitted when a new route has been created
Insert EventType = iota Create EventType = iota
// Delete is emitted when an existing route has been deleted // Delete is emitted when an existing route has been deleted
Delete Delete
// Update is emitted when an existing route has been updated // Update is emitted when an existing route has been updated
Update Update
) )
// String returns string representation of the event
func (et EventType) String() string {
switch et {
case Insert:
return "INSERT"
case Delete:
return "DELETE"
case Update:
return "UPDATE"
default:
return "UNKNOWN"
}
}
// Event is returned by a call to Next on the watcher. // Event is returned by a call to Next on the watcher.
type Event struct { type Event struct {
// Type defines type of event // Type defines type of event
@ -47,11 +32,6 @@ type Event struct {
Route Route Route Route
} }
// String prints human readable Event
func (e Event) String() string {
return fmt.Sprintf("[EVENT] time: %s type: %s", e.Timestamp, e.Type)
}
// WatchOption is used to define what routes to watch in the table // WatchOption is used to define what routes to watch in the table
type WatchOption func(*WatchOptions) type WatchOption func(*WatchOptions)
@ -88,7 +68,7 @@ type tableWatcher struct {
// Next returns the next noticed action taken on table // Next returns the next noticed action taken on table
// TODO: this needs to be thought through properly; // TODO: this needs to be thought through properly;
// right now we only allow to watch destination // right now we only allow to watch service
func (w *tableWatcher) Next() (*Event, error) { func (w *tableWatcher) Next() (*Event, error) {
for { for {
select { select {