2019-06-06 18:37:40 +03:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2019-06-10 01:09:38 +03:00
|
|
|
"fmt"
|
2019-06-13 00:30:42 +03:00
|
|
|
"strconv"
|
2019-06-10 01:09:38 +03:00
|
|
|
"strings"
|
2019-06-12 01:59:25 +03:00
|
|
|
"sync"
|
2019-06-13 00:30:42 +03:00
|
|
|
"time"
|
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"
|
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
|
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-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-06-10 21:50:54 +03:00
|
|
|
opts: options,
|
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-13 00:30:42 +03:00
|
|
|
// Gossip returns gossip bind address
|
|
|
|
func (r *router) Gossip() string {
|
|
|
|
return r.opts.GossipAddress
|
|
|
|
}
|
|
|
|
|
2019-06-10 21:50:54 +03:00
|
|
|
// Network returns router's micro network
|
|
|
|
func (r *router) Network() string {
|
2019-06-13 00:30:42 +03:00
|
|
|
return r.opts.NetworkAddress
|
2019-06-10 21:50:54 +03:00
|
|
|
}
|
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
// Start starts the router
|
|
|
|
func (r *router) Start() error {
|
2019-06-13 00:30:42 +03:00
|
|
|
// add local service routes into routing table
|
|
|
|
if err := r.addServiceRoutes(r.opts.LocalRegistry, "local", 1); err != nil {
|
|
|
|
return fmt.Errorf("failed to add service routes for local services: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add network service routes into routing table
|
|
|
|
if err := r.addServiceRoutes(r.opts.NetworkRegistry, r.opts.NetworkAddress, 10); err != nil {
|
|
|
|
return fmt.Errorf("failed to add service routes for network services: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup local service routes and advertise them in network registry
|
|
|
|
query := NewQuery(QueryNetwork("local"))
|
|
|
|
localRoutes, err := r.opts.Table.Lookup(query)
|
|
|
|
if err != nil && err != ErrRouteNotFound {
|
|
|
|
return fmt.Errorf("failed to lookup local service routes: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := strings.Split(r.opts.Address, ":")
|
|
|
|
port, err := strconv.Atoi(addr[1])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Errorf("could not parse router address from %s: %v", r.opts.Address, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, route := range localRoutes {
|
|
|
|
node := ®istry.Node{
|
|
|
|
Id: r.opts.ID,
|
|
|
|
Address: addr[0],
|
|
|
|
Port: port,
|
|
|
|
}
|
|
|
|
|
|
|
|
service := ®istry.Service{
|
|
|
|
Name: route.Options().DestAddr,
|
|
|
|
Nodes: []*registry.Node{node},
|
|
|
|
}
|
|
|
|
if err := r.opts.NetworkRegistry.Register(service, registry.RegisterTTL(10*time.Second)); err != nil {
|
|
|
|
return fmt.Errorf("failed to register service %s in network registry: %v", service.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lWatcher, err := r.opts.LocalRegistry.Watch()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create local registry watcher: %v", err)
|
|
|
|
}
|
2019-06-12 01:59:25 +03:00
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
rWatcher, err := r.opts.NetworkRegistry.Watch()
|
2019-06-12 01:59:25 +03:00
|
|
|
if err != nil {
|
2019-06-13 00:30:42 +03:00
|
|
|
return fmt.Errorf("failed to create network registry watcher: %v", err)
|
2019-06-12 01:59:25 +03:00
|
|
|
}
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
// we only watch local entries which we resend to network registry
|
|
|
|
tWatcher, err := r.opts.Table.Watch(WatchNetwork("local"))
|
2019-06-12 01:59:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create routing table watcher: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
2019-06-13 00:30:42 +03:00
|
|
|
go r.watchLocal(lWatcher)
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go r.watchRemote(rWatcher)
|
2019-06-12 01:59:25 +03:00
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go r.watchTable(tWatcher)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
func (r *router) addServiceRoutes(reg registry.Registry, network string, metric int) error {
|
|
|
|
// list all local services
|
|
|
|
services, err := reg.ListServices()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to list services: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add services to routing table
|
|
|
|
for _, service := range services {
|
|
|
|
// create new micro network route
|
|
|
|
route := NewRoute(
|
|
|
|
DestAddr(service.Name),
|
|
|
|
Gateway(r),
|
|
|
|
Network(network),
|
|
|
|
Metric(metric),
|
|
|
|
)
|
|
|
|
// add new route to routing table
|
|
|
|
if err := r.opts.Table.Add(route); err != nil {
|
|
|
|
return fmt.Errorf("failed to add route for service: %s", service.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// watch local registry
|
|
|
|
func (r *router) watchLocal(w registry.Watcher) error {
|
2019-06-12 01:59:25 +03:00
|
|
|
defer r.wg.Done()
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
<-r.exit
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
// create new route
|
|
|
|
route := NewRoute(
|
|
|
|
DestAddr(res.Service.Name),
|
|
|
|
Gateway(r),
|
|
|
|
Network("local"),
|
|
|
|
Metric(1),
|
|
|
|
)
|
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
switch res.Action {
|
|
|
|
case "create":
|
|
|
|
if len(res.Service.Nodes) > 0 {
|
2019-06-13 00:30:42 +03:00
|
|
|
if err := r.opts.Table.Add(route); err != nil {
|
|
|
|
log.Logf("[router] failed to add route for local service: %v", res.Service.Name)
|
|
|
|
}
|
2019-06-12 01:59:25 +03:00
|
|
|
}
|
|
|
|
case "delete":
|
2019-06-13 00:30:42 +03:00
|
|
|
if err := r.opts.Table.Remove(route); err != nil {
|
|
|
|
log.Logf("[router] failed to remove route for local service: %v", res.Service.Name)
|
|
|
|
}
|
2019-06-12 01:59:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return watchErr
|
|
|
|
}
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
// watch remote registry
|
|
|
|
func (r *router) watchRemote(w registry.Watcher) error {
|
|
|
|
defer r.wg.Done()
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
<-r.exit
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new route
|
|
|
|
route := NewRoute(
|
|
|
|
DestAddr(res.Service.Name),
|
|
|
|
Gateway(r),
|
|
|
|
Network(r.opts.NetworkAddress),
|
|
|
|
Metric(10),
|
|
|
|
RoutePolicy(IgnoreIfExists),
|
|
|
|
)
|
|
|
|
|
|
|
|
switch res.Action {
|
|
|
|
case "create":
|
|
|
|
if len(res.Service.Nodes) > 0 {
|
|
|
|
if err := r.opts.Table.Add(route); err != nil {
|
|
|
|
log.Logf("[router] failed to add route for network service: %v", res.Service.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "delete":
|
|
|
|
if err := r.opts.Table.Remove(route); err != nil {
|
|
|
|
log.Logf("[router] failed to remove route for network service: %v", res.Service.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return watchErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// watch routing table changes
|
2019-06-12 01:59:25 +03:00
|
|
|
func (r *router) watchTable(w Watcher) error {
|
|
|
|
defer r.wg.Done()
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
<-r.exit
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
addr := strings.Split(r.opts.Address, ":")
|
|
|
|
port, err := strconv.Atoi(addr[1])
|
|
|
|
if err != nil {
|
|
|
|
log.Logf("[router] could not parse router address from %s: %v", r.opts.Address, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
node := ®istry.Node{
|
|
|
|
Id: r.opts.ID,
|
|
|
|
Address: addr[0],
|
|
|
|
Port: port,
|
|
|
|
}
|
|
|
|
|
|
|
|
service := ®istry.Service{
|
|
|
|
Name: res.Route.Options().DestAddr,
|
|
|
|
Nodes: []*registry.Node{node},
|
|
|
|
}
|
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
switch res.Action {
|
|
|
|
case "add":
|
2019-06-13 00:30:42 +03:00
|
|
|
log.Logf("[router] routing table action: %s, route: %v", res.Action, res.Route)
|
|
|
|
if err := r.opts.NetworkRegistry.Register(service, registry.RegisterTTL(10*time.Second)); err != nil {
|
|
|
|
log.Logf("[router] failed to register service %s in network registry: %v", service.Name, err)
|
|
|
|
}
|
2019-06-12 01:59:25 +03:00
|
|
|
case "remove":
|
2019-06-13 00:30:42 +03:00
|
|
|
log.Logf("[router] routing table action: %s, route: %v", res.Action, res.Route)
|
|
|
|
if err := r.opts.NetworkRegistry.Register(service); err != nil {
|
|
|
|
log.Logf("[router] failed to deregister service %s from network registry: %v", service.Name, err)
|
|
|
|
}
|
2019-06-12 01:59:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-06-13 00:30:42 +03:00
|
|
|
table.SetHeader([]string{"ID", "Address", "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,
|
2019-06-13 00:30:42 +03:00
|
|
|
r.opts.NetworkAddress,
|
2019-06-10 21:50:54 +03:00
|
|
|
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
|
|
|
}
|