2019-07-26 00:19:05 +02:00
|
|
|
package router
|
2019-06-13 13:09:49 +02:00
|
|
|
|
|
|
|
import (
|
2019-07-26 00:19:05 +02:00
|
|
|
"errors"
|
2019-06-13 13:09:49 +02:00
|
|
|
"sync"
|
2019-07-10 18:46:22 +02:00
|
|
|
"time"
|
2019-06-13 13:09:49 +02:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
2019-07-29 13:40:13 +02:00
|
|
|
// table is an in memory routing table
|
|
|
|
type table struct {
|
2019-07-29 13:44:28 +02:00
|
|
|
sync.RWMutex
|
2019-07-26 00:19:05 +02:00
|
|
|
// routes stores service routes
|
|
|
|
routes map[string]map[uint64]Route
|
|
|
|
// watchers stores table watchers
|
|
|
|
watchers map[string]*tableWatcher
|
2019-06-13 13:09:49 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 13:40:13 +02:00
|
|
|
// newtable creates a new routing table and returns it
|
|
|
|
func newTable(opts ...Option) *table {
|
|
|
|
return &table{
|
2019-07-26 00:19:05 +02:00
|
|
|
routes: make(map[string]map[uint64]Route),
|
|
|
|
watchers: make(map[string]*tableWatcher),
|
2019-06-13 13:09:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 18:46:22 +02:00
|
|
|
// Create creates new route in the routing table
|
2019-07-29 13:40:13 +02:00
|
|
|
func (t *table) Create(r Route) error {
|
2019-07-09 16:45:42 +02:00
|
|
|
service := r.Service
|
2019-07-08 17:16:50 +02:00
|
|
|
sum := r.Hash()
|
2019-06-13 13:09:49 +02:00
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-08 17:16:50 +02:00
|
|
|
// check if there are any routes in the table for the route destination
|
2019-07-26 00:19:05 +02:00
|
|
|
if _, ok := t.routes[service]; !ok {
|
|
|
|
t.routes[service] = make(map[uint64]Route)
|
|
|
|
t.routes[service][sum] = r
|
2019-07-10 18:46:22 +02:00
|
|
|
go t.sendEvent(&Event{Type: Create, Timestamp: time.Now(), Route: r})
|
2019-06-13 13:09:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-08 17:16:50 +02:00
|
|
|
// add new route to the table for the route destination
|
2019-07-26 00:19:05 +02:00
|
|
|
if _, ok := t.routes[service][sum]; !ok {
|
|
|
|
t.routes[service][sum] = r
|
2019-07-10 18:46:22 +02:00
|
|
|
go t.sendEvent(&Event{Type: Create, Timestamp: time.Now(), Route: r})
|
2019-06-13 13:09:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrDuplicateRoute
|
|
|
|
}
|
|
|
|
|
2019-06-17 20:51:13 +02:00
|
|
|
// Delete deletes the route from the routing table
|
2019-07-29 13:40:13 +02:00
|
|
|
func (t *table) Delete(r Route) error {
|
2019-07-09 16:45:42 +02:00
|
|
|
service := r.Service
|
2019-07-08 17:16:50 +02:00
|
|
|
sum := r.Hash()
|
|
|
|
|
2019-06-13 13:09:49 +02:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-26 00:19:05 +02:00
|
|
|
if _, ok := t.routes[service]; !ok {
|
2019-06-13 13:09:49 +02:00
|
|
|
return ErrRouteNotFound
|
|
|
|
}
|
|
|
|
|
2019-07-26 00:19:05 +02:00
|
|
|
delete(t.routes[service], sum)
|
2019-07-10 18:46:22 +02:00
|
|
|
go t.sendEvent(&Event{Type: Delete, Timestamp: time.Now(), Route: r})
|
2019-06-13 13:09:49 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-08 17:16:50 +02:00
|
|
|
// Update updates routing table with the new route
|
2019-07-29 13:40:13 +02:00
|
|
|
func (t *table) Update(r Route) error {
|
2019-07-09 16:45:42 +02:00
|
|
|
service := r.Service
|
2019-07-08 17:16:50 +02:00
|
|
|
sum := r.Hash()
|
2019-06-13 13:09:49 +02:00
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-08 17:16:50 +02:00
|
|
|
// check if the route destination has any routes in the table
|
2019-07-26 00:19:05 +02:00
|
|
|
if _, ok := t.routes[service]; !ok {
|
|
|
|
t.routes[service] = make(map[uint64]Route)
|
|
|
|
t.routes[service][sum] = r
|
2019-07-10 22:28:32 +02:00
|
|
|
go t.sendEvent(&Event{Type: Create, Timestamp: time.Now(), Route: r})
|
2019-06-13 13:09:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 00:19:05 +02:00
|
|
|
t.routes[service][sum] = r
|
2019-07-10 22:28:32 +02:00
|
|
|
go t.sendEvent(&Event{Type: Update, Timestamp: time.Now(), Route: r})
|
|
|
|
|
|
|
|
return nil
|
2019-06-13 13:09:49 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 19:01:48 +02:00
|
|
|
// List returns a list of all routes in the table
|
2019-07-29 13:40:13 +02:00
|
|
|
func (t *table) List() ([]Route, error) {
|
2019-06-19 19:01:48 +02:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
var routes []Route
|
2019-07-26 00:19:05 +02:00
|
|
|
for _, rmap := range t.routes {
|
2019-06-19 19:01:48 +02:00
|
|
|
for _, route := range rmap {
|
|
|
|
routes = append(routes, route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2019-07-01 21:33:08 +02:00
|
|
|
// isMatch checks if the route matches given network and router
|
|
|
|
func isMatch(route Route, network, router string) bool {
|
|
|
|
if network == "*" || network == route.Network {
|
2019-07-09 16:45:42 +02:00
|
|
|
if router == "*" || router == route.Gateway {
|
2019-07-01 21:33:08 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-06-13 13:09:49 +02:00
|
|
|
|
2019-07-01 21:33:08 +02:00
|
|
|
// findRoutes finds all the routes for given network and router and returns them
|
|
|
|
func findRoutes(routes map[uint64]Route, network, router string) []Route {
|
2019-06-13 13:09:49 +02:00
|
|
|
var results []Route
|
2019-07-01 21:33:08 +02:00
|
|
|
for _, route := range routes {
|
|
|
|
if isMatch(route, network, router) {
|
|
|
|
results = append(results, route)
|
2019-06-13 13:09:49 +02:00
|
|
|
}
|
2019-07-01 21:33:08 +02:00
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
2019-06-13 13:09:49 +02:00
|
|
|
|
2019-07-01 21:33:08 +02:00
|
|
|
// Lookup queries routing table and returns all routes that match the lookup query
|
2019-07-29 19:57:40 +02:00
|
|
|
func (t *table) Query(q Query) ([]Route, error) {
|
2019-07-01 21:33:08 +02:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
2019-07-09 16:45:42 +02:00
|
|
|
if q.Options().Service != "*" {
|
2019-07-26 00:19:05 +02:00
|
|
|
if _, ok := t.routes[q.Options().Service]; !ok {
|
2019-07-01 21:33:08 +02:00
|
|
|
return nil, ErrRouteNotFound
|
2019-06-13 13:09:49 +02:00
|
|
|
}
|
2019-07-26 00:19:05 +02:00
|
|
|
return findRoutes(t.routes[q.Options().Service], q.Options().Network, q.Options().Gateway), nil
|
2019-06-13 13:09:49 +02:00
|
|
|
}
|
|
|
|
|
2019-07-01 21:33:08 +02:00
|
|
|
var results []Route
|
|
|
|
// search through all destinations
|
2019-07-26 00:19:05 +02:00
|
|
|
for _, routes := range t.routes {
|
2019-07-09 16:45:42 +02:00
|
|
|
results = append(results, findRoutes(routes, q.Options().Network, q.Options().Gateway)...)
|
2019-06-13 13:09:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch returns routing table entry watcher
|
2019-07-29 13:40:13 +02:00
|
|
|
func (t *table) Watch(opts ...WatchOption) (Watcher, error) {
|
2019-06-13 13:09:49 +02:00
|
|
|
// by default watch everything
|
|
|
|
wopts := WatchOptions{
|
2019-07-09 16:45:42 +02:00
|
|
|
Service: "*",
|
2019-06-13 13:09:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&wopts)
|
|
|
|
}
|
|
|
|
|
2019-07-26 00:19:05 +02:00
|
|
|
w := &tableWatcher{
|
2019-08-02 15:59:08 +02:00
|
|
|
id: uuid.New().String(),
|
2019-06-13 13:09:49 +02:00
|
|
|
opts: wopts,
|
2019-06-18 11:57:43 +02:00
|
|
|
resChan: make(chan *Event, 10),
|
2019-06-13 13:09:49 +02:00
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
2019-08-02 15:59:08 +02:00
|
|
|
// when the watcher is stopped delete it
|
|
|
|
go func() {
|
|
|
|
<-w.done
|
|
|
|
t.Lock()
|
|
|
|
delete(t.watchers, w.id)
|
|
|
|
t.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// save the watcher
|
2019-06-13 13:09:49 +02:00
|
|
|
t.Lock()
|
2019-08-02 15:59:08 +02:00
|
|
|
t.watchers[w.id] = w
|
2019-06-13 13:09:49 +02:00
|
|
|
t.Unlock()
|
|
|
|
|
2019-07-26 00:19:05 +02:00
|
|
|
return w, nil
|
2019-06-13 13:09:49 +02:00
|
|
|
}
|
|
|
|
|
2019-07-26 18:11:59 +02:00
|
|
|
// sendEvent sends events to all subscribed watchers
|
2019-07-29 13:40:13 +02:00
|
|
|
func (t *table) sendEvent(e *Event) {
|
2019-06-13 13:09:49 +02:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
2019-07-26 00:19:05 +02:00
|
|
|
for _, w := range t.watchers {
|
2019-06-13 13:09:49 +02:00
|
|
|
select {
|
2019-07-26 18:11:59 +02:00
|
|
|
case w.resChan <- e:
|
2019-06-13 13:09:49 +02:00
|
|
|
case <-w.done:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 13:44:59 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrRouteNotFound is returned when no route was found in the routing table
|
|
|
|
ErrRouteNotFound = errors.New("route not found")
|
|
|
|
// ErrDuplicateRoute is returned when the route already exists
|
|
|
|
ErrDuplicateRoute = errors.New("duplicate route")
|
|
|
|
)
|