2019-06-06 18:37:40 +03:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2019-06-10 01:09:38 +03:00
|
|
|
"fmt"
|
2019-07-05 21:15:32 +03:00
|
|
|
"math"
|
2019-07-03 21:50:07 +03:00
|
|
|
"sort"
|
2019-06-10 01:09:38 +03:00
|
|
|
"strings"
|
2019-06-12 01:59:25 +03:00
|
|
|
"sync"
|
2019-06-28 13:53:55 +03:00
|
|
|
"time"
|
2019-06-10 01:09:38 +03:00
|
|
|
|
2019-07-08 18:51:55 +03:00
|
|
|
"github.com/micro/go-micro/network/router/table"
|
2019-06-06 18:37:40 +03:00
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
)
|
|
|
|
|
2019-07-03 21:50:07 +03:00
|
|
|
const (
|
2019-07-11 14:36:39 +03:00
|
|
|
// AdvertiseEventsTick is time interval in which the router advertises route updates
|
|
|
|
AdvertiseEventsTick = 5 * time.Second
|
|
|
|
// AdvertiseTableTick is time interval in which router advertises all routes found in routing table
|
|
|
|
AdvertiseTableTick = 1 * time.Minute
|
2019-07-05 21:15:32 +03:00
|
|
|
// AdvertSuppress is advert suppression threshold
|
|
|
|
AdvertSuppress = 2000
|
2019-07-08 23:03:54 +03:00
|
|
|
// AdvertRecover is advert recovery threshold
|
2019-07-05 21:15:32 +03:00
|
|
|
AdvertRecover = 750
|
2019-07-08 23:03:54 +03:00
|
|
|
// DefaultAdvertTTL is default advertisement TTL
|
2019-07-11 14:36:39 +03:00
|
|
|
DefaultAdvertTTL = 1 * time.Minute
|
2019-07-08 23:03:54 +03:00
|
|
|
// PenaltyDecay is the penalty decay
|
2019-07-05 21:15:32 +03:00
|
|
|
PenaltyDecay = 1.15
|
2019-07-08 23:03:54 +03:00
|
|
|
// Delete penalises route addition and deletion
|
|
|
|
Delete = 1000
|
|
|
|
// UpdatePenalty penalises route updates
|
|
|
|
UpdatePenalty = 500
|
2019-07-03 21:50:07 +03:00
|
|
|
)
|
|
|
|
|
2019-06-26 18:03:19 +03:00
|
|
|
// router provides default router implementation
|
2019-06-06 18:37:40 +03:00
|
|
|
type router struct {
|
2019-07-10 09:45:27 +03:00
|
|
|
// embed the table
|
|
|
|
table.Table
|
2019-06-28 00:52:51 +03:00
|
|
|
opts Options
|
2019-06-28 20:35:53 +03:00
|
|
|
status Status
|
2019-06-28 00:52:51 +03:00
|
|
|
exit chan struct{}
|
2019-07-08 18:51:55 +03:00
|
|
|
eventChan chan *table.Event
|
2019-07-05 21:15:32 +03:00
|
|
|
advertChan chan *Advert
|
2019-07-09 14:46:15 +03:00
|
|
|
advertWg *sync.WaitGroup
|
2019-06-28 00:52:51 +03:00
|
|
|
wg *sync.WaitGroup
|
2019-06-28 20:35:53 +03:00
|
|
|
sync.RWMutex
|
2019-06-06 18:37:40 +03:00
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// newRouter creates a new router and returns it
|
2019-06-06 18:37:40 +03:00
|
|
|
func newRouter(opts ...Option) Router {
|
2019-06-13 00:30:42 +03:00
|
|
|
// get default options
|
|
|
|
options := DefaultOptions()
|
2019-06-06 18:37:40 +03:00
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
// apply requested options
|
2019-06-06 18:37:40 +03:00
|
|
|
for _, o := range opts {
|
2019-06-10 21:50:54 +03:00
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
return &router{
|
2019-07-10 09:45:27 +03:00
|
|
|
Table: options.Table,
|
2019-06-28 00:52:51 +03:00
|
|
|
opts: options,
|
2019-07-08 18:16:50 +03:00
|
|
|
status: Status{Error: nil, Code: Stopped},
|
2019-06-28 00:52:51 +03:00
|
|
|
exit: make(chan struct{}),
|
2019-07-08 18:51:55 +03:00
|
|
|
eventChan: make(chan *table.Event),
|
2019-07-05 21:15:32 +03:00
|
|
|
advertChan: make(chan *Advert),
|
2019-07-09 14:46:15 +03:00
|
|
|
advertWg: &sync.WaitGroup{},
|
2019-06-28 00:52:51 +03:00
|
|
|
wg: &sync.WaitGroup{},
|
2019-06-06 18:37:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
// Init initializes router with given options
|
2019-06-06 18:37:40 +03:00
|
|
|
func (r *router) Init(opts ...Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&r.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
// Options returns router options
|
2019-06-06 18:37:40 +03:00
|
|
|
func (r *router) Options() Options {
|
|
|
|
return r.opts
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:36:39 +03:00
|
|
|
// manageRoute applies route action on the routing table
|
|
|
|
func (r *router) manageRoute(route table.Route, action string) error {
|
|
|
|
switch action {
|
|
|
|
case "create":
|
|
|
|
if err := r.Create(route); err != nil && err != table.ErrDuplicateRoute {
|
|
|
|
return fmt.Errorf("failed adding route for service %s: %s", route.Service, err)
|
|
|
|
}
|
|
|
|
case "update":
|
|
|
|
if err := r.Update(route); err != nil && err != table.ErrDuplicateRoute {
|
|
|
|
return fmt.Errorf("failed updating route for service %s: %s", route.Service, err)
|
|
|
|
}
|
|
|
|
case "delete":
|
|
|
|
if err := r.Delete(route); err != nil && err != table.ErrRouteNotFound {
|
|
|
|
return fmt.Errorf("failed deleting route for service %s: %s", route.Service, err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("failed to manage route for service %s. Unknown action: %s", route.Service, action)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-08 23:03:54 +03:00
|
|
|
// manageServiceRoutes manages routes for a given service.
|
|
|
|
// It returns error of the routing table action fails.
|
2019-07-09 14:46:15 +03:00
|
|
|
func (r *router) manageServiceRoutes(service *registry.Service, action string) error {
|
2019-07-08 18:16:50 +03:00
|
|
|
// action is the routing table action
|
|
|
|
action = strings.ToLower(action)
|
2019-07-10 23:28:32 +03:00
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// take route action on each service node
|
|
|
|
for _, node := range service.Nodes {
|
2019-07-08 18:51:55 +03:00
|
|
|
route := table.Route{
|
2019-07-09 17:45:42 +03:00
|
|
|
Service: service.Name,
|
|
|
|
Address: node.Address,
|
|
|
|
Gateway: "",
|
|
|
|
Network: r.opts.Network,
|
|
|
|
Link: table.DefaultLink,
|
|
|
|
Metric: table.DefaultLocalMetric,
|
2019-07-08 18:16:50 +03:00
|
|
|
}
|
2019-07-10 23:28:32 +03:00
|
|
|
|
2019-07-11 14:36:39 +03:00
|
|
|
if err := r.manageRoute(route, action); err != nil {
|
|
|
|
return err
|
2019-07-08 18:16:50 +03:00
|
|
|
}
|
|
|
|
}
|
2019-07-10 23:28:32 +03:00
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// manageRegistryRoutes manages routes for each service found in the registry.
|
2019-07-08 23:03:54 +03:00
|
|
|
// It returns error if either the services failed to be listed or the routing table action fails.
|
2019-07-09 14:46:15 +03:00
|
|
|
func (r *router) manageRegistryRoutes(reg registry.Registry, action string) error {
|
2019-06-13 00:30:42 +03:00
|
|
|
services, err := reg.ListServices()
|
|
|
|
if err != nil {
|
2019-07-03 21:50:07 +03:00
|
|
|
return fmt.Errorf("failed listing services: %v", err)
|
2019-06-13 00:30:42 +03:00
|
|
|
}
|
|
|
|
|
2019-06-28 00:52:51 +03:00
|
|
|
// add each service node as a separate route
|
2019-06-13 00:30:42 +03:00
|
|
|
for _, service := range services {
|
2019-06-27 16:37:52 +03:00
|
|
|
// get the service to retrieve all its info
|
|
|
|
srvs, err := reg.GetService(service.Name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-08 18:51:55 +03:00
|
|
|
// manage the routes for all returned services
|
2019-07-11 14:36:39 +03:00
|
|
|
for _, srv := range srvs {
|
|
|
|
if err := r.manageServiceRoutes(srv, action); err != nil {
|
2019-07-08 18:16:50 +03:00
|
|
|
return err
|
2019-06-19 20:01:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 18:03:19 +03:00
|
|
|
|
|
|
|
return nil
|
2019-06-19 20:01:48 +03:00
|
|
|
}
|
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
// watchServices watches services in given registry and updates the routing table accordingly.
|
|
|
|
// It returns error if the service registry watcher stops or if the routing table can't be updated.
|
|
|
|
func (r *router) watchServices(w registry.Watcher) error {
|
2019-06-13 17:12:07 +03:00
|
|
|
// wait in the background for the router to stop
|
|
|
|
// when the router stops, stop the watcher and exit
|
2019-06-12 01:59:25 +03:00
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
<-r.exit
|
|
|
|
w.Stop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var watchErr error
|
|
|
|
|
|
|
|
for {
|
|
|
|
res, err := w.Next()
|
|
|
|
if err != nil {
|
2019-07-01 17:43:50 +03:00
|
|
|
if err != registry.ErrWatcherStopped {
|
|
|
|
watchErr = err
|
|
|
|
}
|
2019-06-12 01:59:25 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-07-09 14:46:15 +03:00
|
|
|
if err := r.manageServiceRoutes(res.Service, res.Action); err != nil {
|
2019-07-08 18:16:50 +03:00
|
|
|
return err
|
2019-06-13 00:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return watchErr
|
|
|
|
}
|
|
|
|
|
2019-06-28 00:52:51 +03:00
|
|
|
// watchTable watches routing table entries and either adds or deletes locally registered service to/from network registry
|
|
|
|
// It returns error if the locally registered services either fails to be added/deleted to/from network registry.
|
2019-07-08 18:51:55 +03:00
|
|
|
func (r *router) watchTable(w table.Watcher) error {
|
2019-06-28 00:52:51 +03:00
|
|
|
// wait in the background for the router to stop
|
|
|
|
// when the router stops, stop the watcher and exit
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
<-r.exit
|
|
|
|
w.Stop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var watchErr error
|
|
|
|
|
|
|
|
for {
|
|
|
|
event, err := w.Next()
|
|
|
|
if err != nil {
|
2019-07-08 18:51:55 +03:00
|
|
|
if err != table.ErrWatcherStopped {
|
2019-07-01 17:43:50 +03:00
|
|
|
watchErr = err
|
|
|
|
}
|
2019-06-28 00:52:51 +03:00
|
|
|
break
|
|
|
|
}
|
2019-07-08 18:51:55 +03:00
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
close(r.eventChan)
|
|
|
|
return nil
|
|
|
|
case r.eventChan <- event:
|
2019-06-28 13:53:55 +03:00
|
|
|
}
|
2019-07-05 21:15:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// close event channel on error
|
|
|
|
close(r.eventChan)
|
|
|
|
|
|
|
|
return watchErr
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:36:39 +03:00
|
|
|
// advertiseEvents advertises events to event subscribers
|
|
|
|
func (r *router) advertiseEvents(advType AdvertType, events []*table.Event) {
|
2019-07-09 14:46:15 +03:00
|
|
|
defer r.advertWg.Done()
|
|
|
|
|
|
|
|
a := &Advert{
|
2019-07-10 09:45:27 +03:00
|
|
|
Id: r.opts.Id,
|
2019-07-09 14:46:15 +03:00
|
|
|
Type: advType,
|
2019-07-11 14:36:39 +03:00
|
|
|
TTL: DefaultAdvertTTL,
|
2019-07-09 14:46:15 +03:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
Events: events,
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case r.advertChan <- a:
|
|
|
|
case <-r.exit:
|
|
|
|
return
|
|
|
|
}
|
2019-07-11 14:36:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// advertiseTable advertises the whole routing table to the network
|
|
|
|
func (r *router) advertiseTable() error {
|
|
|
|
// create table advertisement ticker
|
|
|
|
ticker := time.NewTicker(AdvertiseTableTick)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
// list routing table routes to announce
|
|
|
|
routes, err := r.List()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed listing routes: %s", err)
|
|
|
|
}
|
|
|
|
// collect all the added routes before we attempt to add default gateway
|
|
|
|
events := make([]*table.Event, len(routes))
|
|
|
|
for i, route := range routes {
|
|
|
|
event := &table.Event{
|
|
|
|
Type: table.Update,
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
Route: route,
|
|
|
|
}
|
|
|
|
events[i] = event
|
|
|
|
}
|
|
|
|
|
|
|
|
// advertise all routes as Update events to subscribers
|
|
|
|
if len(events) > 0 {
|
|
|
|
go func() {
|
|
|
|
r.advertWg.Add(1)
|
|
|
|
r.advertiseEvents(Update, events)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
case <-r.exit:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-07-09 14:46:15 +03:00
|
|
|
|
2019-07-11 14:36:39 +03:00
|
|
|
return nil
|
2019-07-09 14:46:15 +03:00
|
|
|
}
|
|
|
|
|
2019-07-08 23:03:54 +03:00
|
|
|
// isFlapping detects if the event is flapping based on the current and previous event status.
|
|
|
|
func isFlapping(curr, prev *table.Event) bool {
|
2019-07-08 18:51:55 +03:00
|
|
|
if curr.Type == table.Update && prev.Type == table.Update {
|
2019-07-05 21:15:32 +03:00
|
|
|
return true
|
|
|
|
}
|
2019-06-28 13:53:55 +03:00
|
|
|
|
2019-07-10 19:46:22 +03:00
|
|
|
if curr.Type == table.Create && prev.Type == table.Delete || curr.Type == table.Delete && prev.Type == table.Create {
|
2019-07-05 21:15:32 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:36:39 +03:00
|
|
|
// advertEvent is a table event enriched with advertisement data
|
|
|
|
type advertEvent struct {
|
2019-07-08 23:03:54 +03:00
|
|
|
*table.Event
|
|
|
|
// timestamp marks the time the event has been received
|
|
|
|
timestamp time.Time
|
|
|
|
// penalty is current event penalty
|
|
|
|
penalty float64
|
|
|
|
// isSuppressed flags if the event should be considered for flap detection
|
|
|
|
isSuppressed bool
|
|
|
|
// isFlapping marks the event as flapping event
|
|
|
|
isFlapping bool
|
|
|
|
}
|
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
// processEvents processes routing table events.
|
|
|
|
// It suppresses unhealthy flapping events and advertises healthy events upstream.
|
|
|
|
func (r *router) processEvents() error {
|
|
|
|
// ticker to periodically scan event for advertising
|
2019-07-11 14:36:39 +03:00
|
|
|
ticker := time.NewTicker(AdvertiseEventsTick)
|
2019-07-08 23:03:54 +03:00
|
|
|
// eventMap is a map of advert events
|
2019-07-11 14:36:39 +03:00
|
|
|
eventMap := make(map[uint64]*advertEvent)
|
2019-07-05 21:15:32 +03:00
|
|
|
|
|
|
|
for {
|
2019-06-28 13:53:55 +03:00
|
|
|
select {
|
2019-07-05 21:15:32 +03:00
|
|
|
case <-ticker.C:
|
2019-07-08 18:51:55 +03:00
|
|
|
var events []*table.Event
|
2019-07-08 23:03:54 +03:00
|
|
|
// collect all events which are not flapping
|
2019-07-11 14:36:39 +03:00
|
|
|
// TODO: decay the events and update suppression
|
2019-07-08 23:03:54 +03:00
|
|
|
for key, event := range eventMap {
|
|
|
|
if !event.isFlapping && !event.isSuppressed {
|
2019-07-08 18:51:55 +03:00
|
|
|
e := new(table.Event)
|
2019-07-05 21:15:32 +03:00
|
|
|
*e = *event.Event
|
|
|
|
events = append(events, e)
|
2019-07-06 02:36:15 +03:00
|
|
|
// this deletes the advertised event from the map
|
2019-07-08 23:03:54 +03:00
|
|
|
delete(eventMap, key)
|
2019-07-05 21:15:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:36:39 +03:00
|
|
|
// advertise all Update events to subscribers
|
2019-07-05 21:15:32 +03:00
|
|
|
if len(events) > 0 {
|
2019-07-09 14:46:15 +03:00
|
|
|
r.advertWg.Add(1)
|
2019-07-11 14:36:39 +03:00
|
|
|
go r.advertiseEvents(Update, events)
|
2019-07-05 21:15:32 +03:00
|
|
|
}
|
|
|
|
case e := <-r.eventChan:
|
2019-07-08 23:03:54 +03:00
|
|
|
// event timestamp
|
|
|
|
now := time.Now()
|
|
|
|
// if event is nil, continue
|
2019-07-05 21:15:32 +03:00
|
|
|
if e == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-09 17:55:39 +03:00
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
// determine the event penalty
|
|
|
|
var penalty float64
|
|
|
|
switch e.Type {
|
2019-07-08 18:51:55 +03:00
|
|
|
case table.Update:
|
2019-07-08 23:03:54 +03:00
|
|
|
penalty = UpdatePenalty
|
|
|
|
case table.Delete:
|
|
|
|
penalty = Delete
|
2019-07-05 21:15:32 +03:00
|
|
|
}
|
|
|
|
// we use route hash as eventMap key
|
|
|
|
hash := e.Route.Hash()
|
|
|
|
event, ok := eventMap[hash]
|
|
|
|
if !ok {
|
2019-07-11 14:36:39 +03:00
|
|
|
event = &advertEvent{
|
2019-07-05 21:15:32 +03:00
|
|
|
Event: e,
|
|
|
|
penalty: penalty,
|
|
|
|
timestamp: time.Now(),
|
|
|
|
}
|
|
|
|
eventMap[hash] = event
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// update penalty for existing event: decay existing and add new penalty
|
|
|
|
delta := time.Since(event.timestamp).Seconds()
|
2019-07-08 23:03:54 +03:00
|
|
|
event.penalty = event.penalty*math.Exp(-delta) + penalty
|
|
|
|
event.timestamp = now
|
2019-07-09 17:55:39 +03:00
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
// suppress or recover the event based on its current penalty
|
|
|
|
if !event.isSuppressed && event.penalty > AdvertSuppress {
|
|
|
|
event.isSuppressed = true
|
|
|
|
} else if event.penalty < AdvertRecover {
|
|
|
|
event.isSuppressed = false
|
|
|
|
}
|
|
|
|
// if not suppressed decide if if its flapping
|
|
|
|
if !event.isSuppressed {
|
2019-07-08 23:03:54 +03:00
|
|
|
// detect if its flapping by comparing current and previous event
|
|
|
|
event.isFlapping = isFlapping(e, event.Event)
|
2019-07-05 21:15:32 +03:00
|
|
|
}
|
2019-06-28 13:53:55 +03:00
|
|
|
case <-r.exit:
|
2019-07-09 14:46:15 +03:00
|
|
|
// first wait for the advertiser to finish
|
|
|
|
r.advertWg.Wait()
|
|
|
|
// close the advert channel
|
|
|
|
close(r.advertChan)
|
|
|
|
return nil
|
2019-06-28 00:52:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:36:39 +03:00
|
|
|
// we probably never reach this code path
|
2019-07-05 21:15:32 +03:00
|
|
|
|
|
|
|
return nil
|
2019-06-28 00:52:51 +03:00
|
|
|
}
|
|
|
|
|
2019-07-08 18:51:55 +03:00
|
|
|
// watchErrors watches router errors and takes appropriate actions
|
|
|
|
func (r *router) watchErrors(errChan <-chan error) {
|
2019-06-29 02:46:22 +03:00
|
|
|
defer r.wg.Done()
|
|
|
|
|
|
|
|
var code StatusCode
|
|
|
|
var err error
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
code = Stopped
|
|
|
|
case err = <-errChan:
|
|
|
|
code = Error
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2019-07-01 17:43:50 +03:00
|
|
|
status := Status{
|
|
|
|
Code: code,
|
|
|
|
Error: err,
|
|
|
|
}
|
|
|
|
r.status = status
|
2019-06-29 02:46:22 +03:00
|
|
|
|
|
|
|
// stop the router if some error happened
|
|
|
|
if err != nil && code != Stopped {
|
2019-07-01 22:33:08 +03:00
|
|
|
// this will stop watchers which will close r.advertChan
|
2019-06-29 02:46:22 +03:00
|
|
|
close(r.exit)
|
2019-07-01 22:33:08 +03:00
|
|
|
// drain the advertise channel
|
|
|
|
for range r.advertChan {
|
|
|
|
}
|
2019-07-05 21:15:32 +03:00
|
|
|
// drain the event channel
|
|
|
|
for range r.eventChan {
|
|
|
|
}
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
2019-07-05 21:15:32 +03:00
|
|
|
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Advertise advertises the routes to the network.
|
|
|
|
// It returns error if any of the launched goroutines fail with error.
|
2019-07-04 04:06:59 +03:00
|
|
|
func (r *router) Advertise() (<-chan *Advert, error) {
|
2019-06-29 02:46:22 +03:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
if r.status.Code != Running {
|
2019-07-08 18:16:50 +03:00
|
|
|
// add all local service routes into the routing table
|
2019-07-10 23:28:32 +03:00
|
|
|
if err := r.manageRegistryRoutes(r.opts.Registry, "create"); err != nil {
|
2019-07-09 14:46:15 +03:00
|
|
|
return nil, fmt.Errorf("failed adding routes: %s", err)
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
2019-07-09 17:01:52 +03:00
|
|
|
|
2019-07-09 14:46:15 +03:00
|
|
|
// list routing table routes to announce
|
2019-07-10 23:28:32 +03:00
|
|
|
routes, err := r.List()
|
2019-07-09 14:46:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed listing routes: %s", err)
|
|
|
|
}
|
2019-07-09 17:01:52 +03:00
|
|
|
// collect all the added routes before we attempt to add default gateway
|
|
|
|
events := make([]*table.Event, len(routes))
|
|
|
|
for i, route := range routes {
|
|
|
|
event := &table.Event{
|
2019-07-10 19:46:22 +03:00
|
|
|
Type: table.Create,
|
2019-07-09 17:01:52 +03:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
Route: route,
|
|
|
|
}
|
|
|
|
events[i] = event
|
|
|
|
}
|
|
|
|
|
2019-06-29 02:46:22 +03:00
|
|
|
// add default gateway into routing table
|
|
|
|
if r.opts.Gateway != "" {
|
|
|
|
// note, the only non-default value is the gateway
|
2019-07-08 18:51:55 +03:00
|
|
|
route := table.Route{
|
2019-07-09 17:45:42 +03:00
|
|
|
Service: "*",
|
|
|
|
Address: "*",
|
|
|
|
Gateway: r.opts.Gateway,
|
|
|
|
Network: "*",
|
|
|
|
Metric: table.DefaultLocalMetric,
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
2019-07-10 23:28:32 +03:00
|
|
|
if err := r.Create(route); err != nil {
|
2019-07-03 21:50:07 +03:00
|
|
|
return nil, fmt.Errorf("failed adding default gateway route: %s", err)
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 23:03:54 +03:00
|
|
|
// NOTE: we only need to recreate these if the router errored or was stopped
|
2019-07-09 14:46:15 +03:00
|
|
|
// TODO: These probably dont need to be struct members
|
2019-06-29 02:46:22 +03:00
|
|
|
if r.status.Code == Error || r.status.Code == Stopped {
|
|
|
|
r.exit = make(chan struct{})
|
2019-07-08 18:51:55 +03:00
|
|
|
r.eventChan = make(chan *table.Event)
|
2019-07-04 04:06:59 +03:00
|
|
|
r.advertChan = make(chan *Advert)
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
|
|
|
|
2019-07-09 14:46:15 +03:00
|
|
|
// routing table watcher
|
2019-07-10 23:28:32 +03:00
|
|
|
tableWatcher, err := r.Watch()
|
2019-06-29 02:46:22 +03:00
|
|
|
if err != nil {
|
2019-07-03 21:50:07 +03:00
|
|
|
return nil, fmt.Errorf("failed creating routing table watcher: %v", err)
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
2019-07-05 21:15:32 +03:00
|
|
|
// service registry watcher
|
|
|
|
svcWatcher, err := r.opts.Registry.Watch()
|
2019-06-29 02:46:22 +03:00
|
|
|
if err != nil {
|
2019-07-05 21:15:32 +03:00
|
|
|
return nil, fmt.Errorf("failed creating service registry watcher: %v", err)
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// error channel collecting goroutine errors
|
2019-07-11 14:36:39 +03:00
|
|
|
errChan := make(chan error, 4)
|
2019-06-29 02:46:22 +03:00
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
// watch local registry and register routes in routine table
|
2019-07-05 21:15:32 +03:00
|
|
|
errChan <- r.watchServices(svcWatcher)
|
2019-06-29 02:46:22 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
// watch local registry and register routes in routing table
|
|
|
|
errChan <- r.watchTable(tableWatcher)
|
2019-07-05 21:15:32 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
2019-07-11 14:36:39 +03:00
|
|
|
// watch routing table events and process them
|
2019-07-05 21:15:32 +03:00
|
|
|
errChan <- r.processEvents()
|
2019-06-29 02:46:22 +03:00
|
|
|
}()
|
|
|
|
|
2019-07-11 14:36:39 +03:00
|
|
|
r.advertWg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.advertWg.Done()
|
|
|
|
// advertise the whole routing table
|
|
|
|
errChan <- r.advertiseTable()
|
|
|
|
}()
|
2019-07-01 17:43:50 +03:00
|
|
|
|
2019-07-09 14:46:15 +03:00
|
|
|
// advertise your presence
|
|
|
|
r.advertWg.Add(1)
|
2019-07-11 14:36:39 +03:00
|
|
|
go r.advertiseEvents(Announce, events)
|
|
|
|
|
|
|
|
// watch for errors and cleanup
|
|
|
|
r.wg.Add(1)
|
|
|
|
go r.watchErrors(errChan)
|
2019-07-08 23:03:54 +03:00
|
|
|
|
2019-07-01 17:43:50 +03:00
|
|
|
// mark router as running and set its Error to nil
|
2019-07-11 14:36:39 +03:00
|
|
|
r.status = Status{Code: Running, Error: nil}
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return r.advertChan, nil
|
|
|
|
}
|
|
|
|
|
2019-07-10 09:45:27 +03:00
|
|
|
// Process updates the routing table using the advertised values
|
|
|
|
func (r *router) Process(a *Advert) error {
|
2019-07-03 21:50:07 +03:00
|
|
|
// NOTE: event sorting might not be necessary
|
|
|
|
// copy update events intp new slices
|
2019-07-08 18:51:55 +03:00
|
|
|
events := make([]*table.Event, len(a.Events))
|
2019-07-04 04:06:59 +03:00
|
|
|
copy(events, a.Events)
|
2019-07-03 21:50:07 +03:00
|
|
|
// sort events by timestamp
|
|
|
|
sort.Slice(events, func(i, j int) bool {
|
|
|
|
return events[i].Timestamp.Before(events[j].Timestamp)
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, event := range events {
|
2019-07-09 17:45:42 +03:00
|
|
|
// create a copy of the route
|
|
|
|
route := event.Route
|
2019-07-11 14:36:39 +03:00
|
|
|
action := event.Type
|
|
|
|
if err := r.manageRoute(route, fmt.Sprintf("%s", action)); err != nil {
|
|
|
|
return fmt.Errorf("failed applying action %s to routing table: %s", action, err)
|
2019-07-03 21:50:07 +03:00
|
|
|
}
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 21:50:07 +03:00
|
|
|
return nil
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
|
|
|
|
2019-06-28 20:35:53 +03:00
|
|
|
// Status returns router status
|
|
|
|
func (r *router) Status() Status {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
|
2019-06-29 02:46:22 +03:00
|
|
|
// make a copy of the status
|
|
|
|
status := r.status
|
|
|
|
|
|
|
|
return status
|
2019-06-28 20:35:53 +03:00
|
|
|
}
|
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
// Stop stops the router
|
|
|
|
func (r *router) Stop() error {
|
2019-06-29 02:46:22 +03:00
|
|
|
r.RLock()
|
|
|
|
// only close the channel if the router is running
|
|
|
|
if r.status.Code == Running {
|
|
|
|
// notify all goroutines to finish
|
|
|
|
close(r.exit)
|
2019-07-01 22:33:08 +03:00
|
|
|
// drain the advertise channel
|
|
|
|
for range r.advertChan {
|
|
|
|
}
|
2019-07-05 21:15:32 +03:00
|
|
|
// drain the event channel
|
|
|
|
for range r.eventChan {
|
|
|
|
}
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
2019-07-01 17:43:50 +03:00
|
|
|
r.RUnlock()
|
|
|
|
|
|
|
|
// wait for all goroutines to finish
|
|
|
|
r.wg.Wait()
|
2019-06-19 20:01:48 +03:00
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
// String prints debugging information about router
|
2019-07-10 09:45:27 +03:00
|
|
|
func (r *router) String() string {
|
2019-07-09 18:17:18 +03:00
|
|
|
return "router"
|
2019-06-06 18:37:40 +03:00
|
|
|
}
|