Advertisement is now Update; started bit is now running.
This commit is contained in:
parent
9d7420658d
commit
8ad2f73ad6
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
@ -12,8 +13,8 @@ import (
|
|||||||
// router provides default router implementation
|
// router provides default router implementation
|
||||||
type router struct {
|
type router struct {
|
||||||
opts Options
|
opts Options
|
||||||
started bool
|
running bool
|
||||||
advertChan chan *Advertisement
|
advertChan chan *Update
|
||||||
exit chan struct{}
|
exit chan struct{}
|
||||||
wg *sync.WaitGroup
|
wg *sync.WaitGroup
|
||||||
}
|
}
|
||||||
@ -30,8 +31,8 @@ func newRouter(opts ...Option) Router {
|
|||||||
|
|
||||||
return &router{
|
return &router{
|
||||||
opts: options,
|
opts: options,
|
||||||
started: false,
|
running: false,
|
||||||
advertChan: make(chan *Advertisement),
|
advertChan: make(chan *Update),
|
||||||
exit: make(chan struct{}),
|
exit: make(chan struct{}),
|
||||||
wg: &sync.WaitGroup{},
|
wg: &sync.WaitGroup{},
|
||||||
}
|
}
|
||||||
@ -72,8 +73,8 @@ func (r *router) Network() string {
|
|||||||
|
|
||||||
// Advertise advertises the routes to the network.
|
// Advertise advertises the routes to the network.
|
||||||
// It returns error if any of the launched goroutines fail with error.
|
// It returns error if any of the launched goroutines fail with error.
|
||||||
func (r *router) Advertise() (<-chan *Advertisement, error) {
|
func (r *router) Advertise() (<-chan *Update, error) {
|
||||||
if !r.started {
|
if !r.running {
|
||||||
// add local service routes into the routing table
|
// add local service routes into the routing table
|
||||||
if err := r.addServiceRoutes(r.opts.Registry, "local", DefaultLocalMetric); err != nil {
|
if err := r.addServiceRoutes(r.opts.Registry, "local", DefaultLocalMetric); err != nil {
|
||||||
return nil, fmt.Errorf("failed adding routes: %v", err)
|
return nil, fmt.Errorf("failed adding routes: %v", err)
|
||||||
@ -135,17 +136,19 @@ func (r *router) Advertise() (<-chan *Advertisement, error) {
|
|||||||
|
|
||||||
// close the advertise channel
|
// close the advertise channel
|
||||||
close(r.advertChan)
|
close(r.advertChan)
|
||||||
|
// mark the router as stopped
|
||||||
|
r.running = false
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// mark the router as started
|
// mark the router as running
|
||||||
r.started = true
|
r.running = true
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.advertChan, nil
|
return r.advertChan, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update updates the routing table using the advertised values
|
// Update updates the routing table using the advertised values
|
||||||
func (r *router) Update(a *Advertisement) error {
|
func (r *router) Update(a *Update) error {
|
||||||
// we extract the route from advertisement and update the routing table
|
// we extract the route from advertisement and update the routing table
|
||||||
route := Route{
|
route := Route{
|
||||||
Destination: a.Event.Route.Destination,
|
Destination: a.Event.Route.Destination,
|
||||||
@ -279,9 +282,16 @@ func (r *router) watchTable(w Watcher) error {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
r.advertChan <- &Advertisement{
|
u := &Update{
|
||||||
ID: r.ID(),
|
ID: r.ID(),
|
||||||
Event: event,
|
Timestamp: time.Now(),
|
||||||
|
Event: event,
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-r.exit:
|
||||||
|
return nil
|
||||||
|
case r.advertChan <- u:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
// Package router provides a network routing control plane
|
// Package router provides a network routing control plane
|
||||||
package router
|
package router
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// DefaultRouter is default network router
|
// DefaultRouter is default network router
|
||||||
DefaultRouter = NewRouter()
|
DefaultRouter = NewRouter()
|
||||||
@ -21,19 +23,21 @@ type Router interface {
|
|||||||
// Network returns the network address of the router
|
// Network returns the network address of the router
|
||||||
Network() string
|
Network() string
|
||||||
// Advertise starts advertising routes to the network
|
// Advertise starts advertising routes to the network
|
||||||
Advertise() (<-chan *Advertisement, error)
|
Advertise() (<-chan *Update, error)
|
||||||
// Update updates the routing table
|
// Update updates the routing table
|
||||||
Update(*Advertisement) error
|
Update(*Update) error
|
||||||
// Stop stops the router
|
// Stop stops the router
|
||||||
Stop() error
|
Stop() error
|
||||||
// String returns debug info
|
// String returns debug info
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Advertisement is sent by the router to the network
|
// Update is sent by the router to the network
|
||||||
type Advertisement struct {
|
type Update struct {
|
||||||
// ID is the source router ID
|
// ID is the source router ID
|
||||||
ID string
|
ID string
|
||||||
|
// Timestamp marks the time when update is sent
|
||||||
|
Timestamp time.Time
|
||||||
// Event defines advertisement even
|
// Event defines advertisement even
|
||||||
Event *Event
|
Event *Event
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user