router.Start() is now router.Advertise(). Updated code documentation.

This commit is contained in:
Milos Gajdos 2019-06-20 13:04:58 +01:00
parent 8d5d812e32
commit 1765be049b
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F
2 changed files with 20 additions and 13 deletions

View File

@ -2,6 +2,7 @@ package router
import ( import (
"fmt" "fmt"
"net"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -76,8 +77,11 @@ func (r *router) Network() string {
return r.opts.Advertise return r.opts.Advertise
} }
// Start starts the router // Advertise advertises the router routes to the network.
func (r *router) Start() error { // Advertise is a blocking function. It launches multiple goroutines that watch
// service registries and advertise the router routes to other routers in the network.
// It returns error if any of the launched goroutines fail with error.
func (r *router) Advertise() error {
// add local service routes into the routing table // add local service routes into the routing table
if err := r.addServiceRoutes(r.opts.Registry, DefaultLocalMetric); err != nil { if err := r.addServiceRoutes(r.opts.Registry, DefaultLocalMetric); err != nil {
return fmt.Errorf("failed adding routes for local services: %v", err) return fmt.Errorf("failed adding routes for local services: %v", err)
@ -130,7 +134,7 @@ func (r *router) Start() error {
return <-errChan return <-errChan
} }
// addServiceRouteslists all available services in given registry and adds them to the routing table. // addServiceRoutes adds all services in given registry to the routing table.
// NOTE: this is a one-off operation done when bootstrapping the routing table of the new router. // NOTE: this is a one-off operation done when bootstrapping the routing table of the new router.
// It returns error if either the services could not be listed or if the routes could not be added to the routing table. // It returns error if either the services could not be listed or if the routes could not be added to the routing table.
func (r *router) addServiceRoutes(reg registry.Registry, metric int) error { func (r *router) addServiceRoutes(reg registry.Registry, metric int) error {
@ -156,19 +160,22 @@ func (r *router) addServiceRoutes(reg registry.Registry, metric int) error {
// parseToNode parses router into registry.Node and returns the result. // parseToNode parses router into registry.Node and returns the result.
// It returns error if the router network address could not be parsed into host and port. // It returns error if the router network address could not be parsed into host and port.
// NOTE: We use ":" as the delimiter when we splitting the router network address.
func (r *router) parseToNode() (*registry.Node, error) { func (r *router) parseToNode() (*registry.Node, error) {
// split on ":" as a standard host/port delimiter // split router address to host and port part
addr := strings.Split(r.opts.Advertise, ":") addr, portStr, err := net.SplitHostPort(r.opts.Advertise)
if err != nil {
return nil, fmt.Errorf("could not parse router address: %v", err)
}
// try to parse network port into integer // try to parse network port into integer
port, err := strconv.Atoi(addr[1]) port, err := strconv.Atoi(portStr)
if err != nil { if err != nil {
return nil, fmt.Errorf("could not parse router network address: %v", err) return nil, fmt.Errorf("could not parse router network address: %v", err)
} }
node := &registry.Node{ node := &registry.Node{
Id: r.opts.ID, Id: r.opts.ID,
Address: addr[0], Address: addr,
Port: port, Port: port,
} }

View File

@ -7,16 +7,16 @@ type Router interface {
Init(...Option) error Init(...Option) error
// Options returns the router options // Options returns the router options
Options() Options Options() Options
// ID returns id of the router // ID returns the id of the router
ID() string ID() string
// Table returns the router routing table // Table returns the routing table
Table() Table Table() Table
// Address returns the router adddress // Address returns the router adddress
Address() string Address() string
// Network returns the router network address // Network returns the network address of the router
Network() string Network() string
// Start starts the router // Advertise starts advertising the routes to the network
Start() error Advertise() error
// Stop stops the router // Stop stops the router
Stop() error Stop() error
// String returns debug info // String returns debug info