Table.Add is now Table.Create. Insesrt event is now Create event.
This commit is contained in:
parent
86dfa82dfa
commit
8f5aed707e
@ -95,7 +95,7 @@ func (r *router) manageServiceRoutes(service *registry.Service, action string) e
|
||||
}
|
||||
switch action {
|
||||
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)
|
||||
}
|
||||
case "delete":
|
||||
@ -227,7 +227,7 @@ func isFlapping(curr, prev *table.Event) bool {
|
||||
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
|
||||
}
|
||||
|
||||
@ -389,7 +389,7 @@ func (r *router) Advertise() (<-chan *Advert, error) {
|
||||
events := make([]*table.Event, len(routes))
|
||||
for i, route := range routes {
|
||||
event := &table.Event{
|
||||
Type: table.Insert,
|
||||
Type: table.Create,
|
||||
Timestamp: time.Now(),
|
||||
Route: route,
|
||||
}
|
||||
@ -406,7 +406,7 @@ func (r *router) Advertise() (<-chan *Advert, error) {
|
||||
Network: "*",
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package table
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@ -51,8 +52,8 @@ func (t *table) Options() TableOptions {
|
||||
return t.opts
|
||||
}
|
||||
|
||||
// Add adds a route to the routing table
|
||||
func (t *table) Add(r Route) error {
|
||||
// Create creates new route in the routing table
|
||||
func (t *table) Create(r Route) error {
|
||||
service := r.Service
|
||||
sum := r.Hash()
|
||||
|
||||
@ -63,14 +64,14 @@ func (t *table) Add(r Route) error {
|
||||
if _, ok := t.m[service]; !ok {
|
||||
t.m[service] = make(map[uint64]Route)
|
||||
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
|
||||
}
|
||||
|
||||
// add new route to the table for the route destination
|
||||
if _, ok := t.m[service][sum]; !ok {
|
||||
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
|
||||
}
|
||||
|
||||
@ -90,7 +91,7 @@ func (t *table) Delete(r Route) error {
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@ -111,7 +112,7 @@ func (t *table) Update(r Route) error {
|
||||
// if the route has been found update it
|
||||
if _, ok := t.m[service][sum]; ok {
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -16,11 +16,11 @@ func testSetup() (Table, Route) {
|
||||
return table, route
|
||||
}
|
||||
|
||||
func TestAdd(t *testing.T) {
|
||||
func TestCreate(t *testing.T) {
|
||||
table, route := testSetup()
|
||||
testTableSize := table.Size()
|
||||
|
||||
if err := table.Add(route); err != nil {
|
||||
if err := table.Create(route); err != nil {
|
||||
t.Errorf("error adding route: %s", err)
|
||||
}
|
||||
testTableSize += 1
|
||||
@ -28,7 +28,7 @@ func TestAdd(t *testing.T) {
|
||||
// adds new route for the original destination
|
||||
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)
|
||||
}
|
||||
testTableSize += 1
|
||||
@ -38,7 +38,7 @@ func TestAdd(t *testing.T) {
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
@ -47,7 +47,7 @@ func TestDelete(t *testing.T) {
|
||||
table, route := testSetup()
|
||||
testTableSize := table.Size()
|
||||
|
||||
if err := table.Add(route); err != nil {
|
||||
if err := table.Create(route); err != nil {
|
||||
t.Errorf("error adding route: %s", err)
|
||||
}
|
||||
testTableSize += 1
|
||||
@ -77,7 +77,7 @@ func TestUpdate(t *testing.T) {
|
||||
table, route := testSetup()
|
||||
testTableSize := table.Size()
|
||||
|
||||
if err := table.Add(route); err != nil {
|
||||
if err := table.Create(route); err != nil {
|
||||
t.Errorf("error adding route: %s", err)
|
||||
}
|
||||
testTableSize += 1
|
||||
@ -113,7 +113,7 @@ func TestList(t *testing.T) {
|
||||
|
||||
for i := 0; i < len(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)
|
||||
}
|
||||
}
|
||||
@ -143,7 +143,7 @@ func TestLookup(t *testing.T) {
|
||||
route.Service = svc[i]
|
||||
route.Network = net[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)
|
||||
}
|
||||
}
|
||||
|
@ -13,8 +13,8 @@ var (
|
||||
|
||||
// Table defines routing table interface
|
||||
type Table interface {
|
||||
// Add adds new route to the routing table
|
||||
Add(Route) error
|
||||
// Create new route in the routing table
|
||||
Create(Route) error
|
||||
// Delete deletes existing route from the routing table
|
||||
Delete(Route) error
|
||||
// Update updates route in the routing table
|
||||
|
@ -2,7 +2,6 @@ package table
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -15,28 +14,14 @@ var (
|
||||
type EventType int
|
||||
|
||||
const (
|
||||
// Insert is emitted when a new route has been inserted
|
||||
Insert EventType = iota
|
||||
// Create is emitted when a new route has been created
|
||||
Create EventType = iota
|
||||
// Delete is emitted when an existing route has been deleted
|
||||
Delete
|
||||
// Update is emitted when an existing route has been updated
|
||||
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.
|
||||
type Event struct {
|
||||
// Type defines type of event
|
||||
@ -47,11 +32,6 @@ type Event struct {
|
||||
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
|
||||
type WatchOption func(*WatchOptions)
|
||||
|
||||
@ -88,7 +68,7 @@ type tableWatcher struct {
|
||||
|
||||
// Next returns the next noticed action taken on table
|
||||
// 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) {
|
||||
for {
|
||||
select {
|
||||
|
Loading…
Reference in New Issue
Block a user