2019-06-06 18:37:40 +03:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2019-06-10 01:09:38 +03:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-06-12 01:59:25 +03:00
|
|
|
"sync"
|
2019-06-10 01:09:38 +03:00
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
"github.com/micro/go-log"
|
2019-06-06 18:37:40 +03:00
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
"github.com/micro/go-micro/registry/gossip"
|
2019-06-10 21:50:54 +03:00
|
|
|
"github.com/olekukonko/tablewriter"
|
2019-06-06 18:37:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type router struct {
|
2019-06-10 21:50:54 +03:00
|
|
|
opts Options
|
|
|
|
goss registry.Registry
|
2019-06-12 01:59:25 +03:00
|
|
|
exit chan struct{}
|
|
|
|
wg *sync.WaitGroup
|
2019-06-06 18:37:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newRouter(opts ...Option) Router {
|
2019-06-10 21:50:54 +03:00
|
|
|
// set default options
|
|
|
|
options := Options{
|
|
|
|
Table: NewTable(),
|
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
|
|
|
// bind to gossip address to join gossip registry
|
2019-06-10 21:50:54 +03:00
|
|
|
goss := gossip.NewRegistry(
|
|
|
|
gossip.Address(options.GossipAddr),
|
|
|
|
)
|
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
return &router{
|
2019-06-10 21:50:54 +03:00
|
|
|
opts: options,
|
|
|
|
goss: goss,
|
2019-06-12 01:59:25 +03:00
|
|
|
exit: make(chan struct{}),
|
|
|
|
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-06-07 01:29:24 +03:00
|
|
|
// Table returns routing table
|
2019-06-07 15:29:09 +03:00
|
|
|
func (r *router) Table() Table {
|
2019-06-10 21:50:54 +03:00
|
|
|
return r.opts.Table
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 19:20:22 +03:00
|
|
|
// Address returns router's bind address
|
2019-06-07 01:29:24 +03:00
|
|
|
func (r *router) Address() string {
|
2019-06-07 15:29:09 +03:00
|
|
|
return r.opts.Address
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
|
2019-06-10 21:50:54 +03:00
|
|
|
// Network returns router's micro network
|
|
|
|
func (r *router) Network() string {
|
|
|
|
return r.opts.NetworkAddr
|
|
|
|
}
|
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
// Start starts the router
|
|
|
|
func (r *router) Start() error {
|
|
|
|
// TODO:
|
|
|
|
// - list all remote services and populate routing table
|
|
|
|
// - list all local services and populate remote registry
|
|
|
|
|
|
|
|
gWatcher, err := r.goss.Watch()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create router gossip registry watcher: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tWatcher, err := r.opts.Table.Watch()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create routing table watcher: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go r.watchGossip(gWatcher)
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go r.watchTable(tWatcher)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// watch gossip registry
|
|
|
|
func (r *router) watchGossip(w registry.Watcher) error {
|
|
|
|
defer r.wg.Done()
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
<-r.exit
|
|
|
|
// stop gossip registry watcher
|
|
|
|
w.Stop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var watchErr error
|
|
|
|
|
|
|
|
// watch for changes to services
|
|
|
|
for {
|
|
|
|
res, err := w.Next()
|
|
|
|
if err == registry.ErrWatcherStopped {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
watchErr = err
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch res.Action {
|
|
|
|
case "create":
|
|
|
|
if len(res.Service.Nodes) > 0 {
|
|
|
|
log.Logf("Action: %s, Service: %v", res.Action, res.Service.Name)
|
|
|
|
}
|
|
|
|
case "delete":
|
|
|
|
log.Logf("Action: %s, Service: %v", res.Action, res.Service.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return watchErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// watch gossip registry
|
|
|
|
func (r *router) watchTable(w Watcher) error {
|
|
|
|
defer r.wg.Done()
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
<-r.exit
|
|
|
|
// stop gossip registry watcher
|
|
|
|
w.Stop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var watchErr error
|
|
|
|
|
|
|
|
// watch for changes to services
|
|
|
|
for {
|
|
|
|
res, err := w.Next()
|
|
|
|
if err == ErrWatcherStopped {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
watchErr = err
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch res.Action {
|
|
|
|
case "add":
|
|
|
|
log.Logf("Action: %s, Route: %v", res.Action, res.Route)
|
|
|
|
case "remove":
|
|
|
|
log.Logf("Action: %s, Route: %v", res.Action, res.Route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return watchErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the router
|
|
|
|
func (r *router) Stop() error {
|
|
|
|
// notify all goroutines to finish
|
|
|
|
close(r.exit)
|
|
|
|
|
|
|
|
// wait for all goroutines to finish
|
|
|
|
r.wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
// String prints debugging information about router
|
2019-06-06 18:37:40 +03:00
|
|
|
func (r *router) String() string {
|
2019-06-10 01:09:38 +03:00
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
2019-06-10 21:50:54 +03:00
|
|
|
table := tablewriter.NewWriter(sb)
|
|
|
|
table.SetHeader([]string{"ID", "Address", "Gossip", "Network", "Table"})
|
2019-06-10 01:09:38 +03:00
|
|
|
|
2019-06-10 21:50:54 +03:00
|
|
|
data := []string{
|
|
|
|
r.opts.ID,
|
|
|
|
r.opts.Address,
|
|
|
|
r.opts.GossipAddr,
|
|
|
|
r.opts.NetworkAddr,
|
|
|
|
fmt.Sprintf("%d", r.opts.Table.Size()),
|
|
|
|
}
|
|
|
|
table.Append(data)
|
2019-06-10 01:09:38 +03:00
|
|
|
|
2019-06-10 21:50:54 +03:00
|
|
|
// render table into sb
|
|
|
|
table.Render()
|
2019-06-10 01:09:38 +03:00
|
|
|
|
|
|
|
return sb.String()
|
2019-06-06 18:37:40 +03:00
|
|
|
}
|