1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00

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 (
"fmt"
"net"
"strconv"
"strings"
"sync"
@ -76,8 +77,11 @@ func (r *router) Network() string {
return r.opts.Advertise
}
// Start starts the router
func (r *router) Start() error {
// Advertise advertises the router routes to the network.
// 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
if err := r.addServiceRoutes(r.opts.Registry, DefaultLocalMetric); err != nil {
return fmt.Errorf("failed adding routes for local services: %v", err)
@ -130,7 +134,7 @@ func (r *router) Start() error {
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.
// 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 {
@ -156,19 +160,22 @@ func (r *router) addServiceRoutes(reg registry.Registry, metric int) error {
// 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.
// NOTE: We use ":" as the delimiter when we splitting the router network address.
func (r *router) parseToNode() (*registry.Node, error) {
// split on ":" as a standard host/port delimiter
addr := strings.Split(r.opts.Advertise, ":")
// split router address to host and port part
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
port, err := strconv.Atoi(addr[1])
port, err := strconv.Atoi(portStr)
if err != nil {
return nil, fmt.Errorf("could not parse router network address: %v", err)
}
node := &registry.Node{
Id: r.opts.ID,
Address: addr[0],
Address: addr,
Port: port,
}

View File

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