Skip to content

Commit

Permalink
Advertisement is now Update; started bit is now running.
Browse files Browse the repository at this point in the history
  • Loading branch information
milosgajdos committed Jul 1, 2019
1 parent 9d74206 commit 8ad2f73
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
34 changes: 22 additions & 12 deletions network/router/default_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"sync"
"time"

"github.com/micro/go-micro/registry"
"github.com/olekukonko/tablewriter"
Expand All @@ -12,8 +13,8 @@ import (
// router provides default router implementation
type router struct {
opts Options
started bool
advertChan chan *Advertisement
running bool
advertChan chan *Update
exit chan struct{}
wg *sync.WaitGroup
}
Expand All @@ -30,8 +31,8 @@ func newRouter(opts ...Option) Router {

return &router{
opts: options,
started: false,
advertChan: make(chan *Advertisement),
running: false,
advertChan: make(chan *Update),
exit: make(chan struct{}),
wg: &sync.WaitGroup{},
}
Expand Down Expand Up @@ -72,8 +73,8 @@ func (r *router) Network() string {

// Advertise advertises the routes to the network.
// It returns error if any of the launched goroutines fail with error.
func (r *router) Advertise() (<-chan *Advertisement, error) {
if !r.started {
func (r *router) Advertise() (<-chan *Update, error) {
if !r.running {
// add local service routes into the routing table
if err := r.addServiceRoutes(r.opts.Registry, "local", DefaultLocalMetric); err != nil {
return nil, fmt.Errorf("failed adding routes: %v", err)
Expand Down Expand Up @@ -135,17 +136,19 @@ func (r *router) Advertise() (<-chan *Advertisement, error) {

// close the advertise channel
close(r.advertChan)
// mark the router as stopped
r.running = false
}()

// mark the router as started
r.started = true
// mark the router as running
r.running = true
}

return r.advertChan, nil
}

// 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
route := Route{
Destination: a.Event.Route.Destination,
Expand Down Expand Up @@ -279,9 +282,16 @@ func (r *router) watchTable(w Watcher) error {
break
}

r.advertChan <- &Advertisement{
ID: r.ID(),
Event: event,
u := &Update{
ID: r.ID(),
Timestamp: time.Now(),
Event: event,
}

select {
case <-r.exit:
return nil
case r.advertChan <- u:
}
}

Expand Down
12 changes: 8 additions & 4 deletions network/router/router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Package router provides a network routing control plane
package router

import "time"

var (
// DefaultRouter is default network router
DefaultRouter = NewRouter()
Expand All @@ -21,19 +23,21 @@ type Router interface {
// Network returns the network address of the router
Network() string
// Advertise starts advertising routes to the network
Advertise() (<-chan *Advertisement, error)
Advertise() (<-chan *Update, error)
// Update updates the routing table
Update(*Advertisement) error
Update(*Update) error
// Stop stops the router
Stop() error
// String returns debug info
String() string
}

// Advertisement is sent by the router to the network
type Advertisement struct {
// Update is sent by the router to the network
type Update struct {
// ID is the source router ID
ID string
// Timestamp marks the time when update is sent
Timestamp time.Time
// Event defines advertisement even
Event *Event
}
Expand Down

0 comments on commit 8ad2f73

Please sign in to comment.