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