2021-01-29 15:08:17 +03:00
|
|
|
package register
|
2020-06-24 13:46:51 +03:00
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2020-06-24 13:46:51 +03:00
|
|
|
|
2021-01-16 01:12:18 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2021-01-29 15:08:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2021-01-16 01:12:18 +03:00
|
|
|
"github.com/unistack-org/micro/v3/router"
|
2020-07-27 15:22:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-08-07 22:53:38 +03:00
|
|
|
// RefreshInterval is the time at which we completely refresh the table
|
|
|
|
RefreshInterval = time.Second * 120
|
|
|
|
// PruneInterval is how often we prune the routing table
|
|
|
|
PruneInterval = time.Second * 10
|
2020-07-27 15:22:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// rtr implements router interface
|
|
|
|
type rtr struct {
|
|
|
|
sync.RWMutex
|
|
|
|
|
2020-08-15 01:51:52 +03:00
|
|
|
running bool
|
|
|
|
table *table
|
2021-01-16 01:12:18 +03:00
|
|
|
opts router.Options
|
2020-08-15 01:51:52 +03:00
|
|
|
exit chan bool
|
|
|
|
initChan chan bool
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRouter creates new router and returns it
|
2020-06-24 13:46:51 +03:00
|
|
|
func NewRouter(opts ...router.Option) router.Router {
|
2021-01-16 01:12:18 +03:00
|
|
|
options := router.NewOptions(opts...)
|
2020-07-27 15:22:00 +03:00
|
|
|
|
|
|
|
// construct the router
|
|
|
|
r := &rtr{
|
2021-01-16 01:12:18 +03:00
|
|
|
opts: options,
|
2020-08-15 01:51:52 +03:00
|
|
|
initChan: make(chan bool),
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// create the new table, passing the fetchRoute method in as a fallback if
|
|
|
|
// the table doesn't contain the result for a query.
|
2021-01-16 01:12:18 +03:00
|
|
|
r.table = newTable(r.lookup)
|
2020-07-28 11:01:08 +03:00
|
|
|
|
|
|
|
// start the router
|
|
|
|
r.start()
|
2020-07-27 15:22:00 +03:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes router with given options
|
|
|
|
func (r *rtr) Init(opts ...router.Option) error {
|
|
|
|
r.Lock()
|
|
|
|
for _, o := range opts {
|
2021-01-16 01:12:18 +03:00
|
|
|
o(&r.opts)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
2020-08-04 13:43:01 +03:00
|
|
|
r.Unlock()
|
|
|
|
|
2021-01-29 15:08:17 +03:00
|
|
|
if r.opts.Register == nil {
|
|
|
|
return fmt.Errorf("register not set")
|
2021-01-16 01:12:18 +03:00
|
|
|
}
|
|
|
|
|
2020-08-04 13:43:01 +03:00
|
|
|
// push a message to the init chan so the watchers
|
2021-01-29 15:08:17 +03:00
|
|
|
// can reset in the case the register was changed
|
2020-08-04 13:43:01 +03:00
|
|
|
go func() {
|
|
|
|
r.initChan <- true
|
|
|
|
}()
|
2020-07-27 15:22:00 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options returns router options
|
|
|
|
func (r *rtr) Options() router.Options {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
|
2021-01-16 01:12:18 +03:00
|
|
|
options := r.opts
|
2020-07-27 15:22:00 +03:00
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
|
|
|
// Table returns routing table
|
|
|
|
func (r *rtr) Table() router.Table {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
return r.table
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:08:17 +03:00
|
|
|
func getDomain(srv *register.Service) string {
|
2020-08-07 22:53:38 +03:00
|
|
|
// check the service metadata for domain
|
2021-01-29 15:08:17 +03:00
|
|
|
// TODO: domain as Domain field in register?
|
2020-08-07 22:53:38 +03:00
|
|
|
if srv.Metadata != nil && len(srv.Metadata["domain"]) > 0 {
|
|
|
|
return srv.Metadata["domain"]
|
|
|
|
} else if len(srv.Nodes) > 0 && srv.Nodes[0].Metadata != nil {
|
|
|
|
return srv.Nodes[0].Metadata["domain"]
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise return wildcard
|
|
|
|
// TODO: return GlobalDomain or PublicDomain
|
2021-01-29 15:08:17 +03:00
|
|
|
return register.DefaultDomain
|
2020-08-07 22:53:38 +03:00
|
|
|
}
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
// manageRoute applies action on a given route
|
|
|
|
func (r *rtr) manageRoute(route router.Route, action string) error {
|
|
|
|
switch action {
|
|
|
|
case "create":
|
|
|
|
if err := r.table.Create(route); err != nil && err != router.ErrDuplicateRoute {
|
|
|
|
return fmt.Errorf("failed adding route for service %s: %s", route.Service, err)
|
|
|
|
}
|
|
|
|
case "delete":
|
|
|
|
if err := r.table.Delete(route); err != nil && err != router.ErrRouteNotFound {
|
|
|
|
return fmt.Errorf("failed deleting route for service %s: %s", route.Service, err)
|
|
|
|
}
|
|
|
|
case "update":
|
|
|
|
if err := r.table.Update(route); err != nil {
|
|
|
|
return fmt.Errorf("failed updating route for service %s: %s", route.Service, err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("failed to manage route for service %s: unknown action %s", route.Service, action)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-07 22:53:38 +03:00
|
|
|
// createRoutes turns a service into a list routes basically converting nodes to routes
|
2021-01-29 15:08:17 +03:00
|
|
|
func (r *rtr) createRoutes(service *register.Service, network string) []router.Route {
|
2021-01-16 01:12:18 +03:00
|
|
|
routes := make([]router.Route, 0, len(service.Nodes))
|
2020-07-27 15:22:00 +03:00
|
|
|
|
|
|
|
for _, node := range service.Nodes {
|
2020-08-07 22:53:38 +03:00
|
|
|
routes = append(routes, router.Route{
|
2020-07-27 15:22:00 +03:00
|
|
|
Service: service.Name,
|
|
|
|
Address: node.Address,
|
|
|
|
Gateway: "",
|
|
|
|
Network: network,
|
2021-01-16 01:12:18 +03:00
|
|
|
Router: r.opts.Id,
|
2020-07-27 15:22:00 +03:00
|
|
|
Link: router.DefaultLink,
|
2021-01-16 01:12:18 +03:00
|
|
|
Metric: router.DefaultLocalMetric,
|
2020-07-27 15:22:00 +03:00
|
|
|
Metadata: node.Metadata,
|
2020-08-07 22:53:38 +03:00
|
|
|
})
|
|
|
|
}
|
2020-07-27 15:22:00 +03:00
|
|
|
|
2020-08-07 22:53:38 +03:00
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
|
|
|
// manageServiceRoutes applies action to all routes of the service.
|
|
|
|
// It returns error of the action fails with error.
|
2021-01-29 15:08:17 +03:00
|
|
|
func (r *rtr) manageRoutes(service *register.Service, action, network string) error {
|
2020-08-07 22:53:38 +03:00
|
|
|
// create a set of routes from the service
|
|
|
|
routes := r.createRoutes(service, network)
|
|
|
|
|
|
|
|
// if its a delete action and there's no nodes
|
|
|
|
// it means we need to wipe out all the routes
|
|
|
|
// for that service
|
|
|
|
if action == "delete" && len(routes) == 0 {
|
|
|
|
// delete the service entirely
|
|
|
|
r.table.deleteService(service.Name, network)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the routes in the table
|
|
|
|
for _, route := range routes {
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
r.opts.Logger.Tracef(r.opts.Context, "Creating route %v domain: %v", route, network)
|
|
|
|
}
|
2020-07-27 15:22:00 +03:00
|
|
|
if err := r.manageRoute(route, action); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:08:17 +03:00
|
|
|
// manageRegisterRoutes applies action to all routes of each service found in the register.
|
2020-07-27 15:22:00 +03:00
|
|
|
// It returns error if either the services failed to be listed or the routing table action fails.
|
2021-01-29 15:08:17 +03:00
|
|
|
func (r *rtr) loadRoutes(reg register.Register) error {
|
|
|
|
services, err := reg.ListServices(r.opts.Context, register.ListDomain(register.WildcardDomain))
|
2020-07-27 15:22:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed listing services: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add each service node as a separate route
|
|
|
|
for _, service := range services {
|
|
|
|
// get the services domain from metadata. Fallback to wildcard.
|
2020-08-07 22:53:38 +03:00
|
|
|
domain := getDomain(service)
|
2020-08-06 13:32:06 +03:00
|
|
|
|
2020-08-07 22:53:38 +03:00
|
|
|
// create the routes
|
|
|
|
routes := r.createRoutes(service, domain)
|
2020-07-27 15:22:00 +03:00
|
|
|
|
2020-08-07 22:53:38 +03:00
|
|
|
// if the routes exist save them
|
|
|
|
if len(routes) > 0 {
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
r.opts.Logger.Tracef(r.opts.Context, "Creating routes for service %v domain: %v", service, domain)
|
|
|
|
}
|
2020-08-11 14:42:22 +03:00
|
|
|
for _, rt := range routes {
|
2020-08-11 18:57:04 +03:00
|
|
|
err := r.table.Create(rt)
|
|
|
|
|
|
|
|
// update the route to prevent it from expiring
|
|
|
|
if err == router.ErrDuplicateRoute {
|
|
|
|
err = r.table.Update(rt)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Logger.V(logger.ErrorLevel) {
|
|
|
|
r.opts.Logger.Errorf(r.opts.Context, "Error creating route for service %v in domain %v: %v", service, domain, err)
|
|
|
|
}
|
2020-08-11 14:42:22 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-06 13:32:06 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise get all the service info
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
// get the service to retrieve all its info
|
2021-01-29 15:08:17 +03:00
|
|
|
srvs, err := reg.LookupService(r.opts.Context, service.Name, register.LookupDomain(domain))
|
2020-07-27 15:22:00 +03:00
|
|
|
if err != nil {
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
r.opts.Logger.Tracef(r.opts.Context, "Failed to get service %s domain: %s", service.Name, domain)
|
|
|
|
}
|
2020-07-27 15:22:00 +03:00
|
|
|
continue
|
|
|
|
}
|
2020-08-06 13:32:06 +03:00
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
// manage the routes for all returned services
|
|
|
|
for _, srv := range srvs {
|
2020-08-07 22:53:38 +03:00
|
|
|
routes := r.createRoutes(srv, domain)
|
|
|
|
|
|
|
|
if len(routes) > 0 {
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
r.opts.Logger.Tracef(r.opts.Context, "Creating routes for service %v domain: %v", srv, domain)
|
|
|
|
}
|
2020-08-11 14:42:22 +03:00
|
|
|
for _, rt := range routes {
|
2020-08-11 18:57:04 +03:00
|
|
|
err := r.table.Create(rt)
|
|
|
|
|
|
|
|
// update the route to prevent it from expiring
|
|
|
|
if err == router.ErrDuplicateRoute {
|
|
|
|
err = r.table.Update(rt)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Logger.V(logger.ErrorLevel) {
|
|
|
|
r.opts.Logger.Errorf(r.opts.Context, "Error creating route for service %v in domain %v: %v", service, domain, err)
|
|
|
|
}
|
2020-08-11 14:42:22 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-07 22:53:38 +03:00
|
|
|
// lookup retrieves all the routes for a given service and creates them in the routing table
|
2021-01-16 01:12:18 +03:00
|
|
|
func (r *rtr) lookup(service string) ([]router.Route, error) {
|
|
|
|
if r.opts.Logger.V(logger.TraceLevel) {
|
2021-01-29 15:08:17 +03:00
|
|
|
r.opts.Logger.Tracef(r.opts.Context, "Fetching route for %s domain: %v", service, register.WildcardDomain)
|
2020-08-21 11:23:01 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 15:08:17 +03:00
|
|
|
services, err := r.opts.Register.LookupService(r.opts.Context, service, register.LookupDomain(register.WildcardDomain))
|
|
|
|
if err == register.ErrNotFound {
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
r.opts.Logger.Tracef(r.opts.Context, "Failed to find route for %s", service)
|
|
|
|
}
|
2020-08-07 22:53:38 +03:00
|
|
|
return nil, router.ErrRouteNotFound
|
2020-07-27 15:22:00 +03:00
|
|
|
} else if err != nil {
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
r.opts.Logger.Tracef(r.opts.Context, "Failed to find route for %s: %v", service, err)
|
|
|
|
}
|
2020-08-07 22:53:38 +03:00
|
|
|
return nil, fmt.Errorf("failed getting services: %v", err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
2021-01-16 01:12:18 +03:00
|
|
|
var routes []router.Route
|
|
|
|
|
2020-08-07 22:53:38 +03:00
|
|
|
for _, srv := range services {
|
|
|
|
domain := getDomain(srv)
|
|
|
|
// TODO: should we continue to send the event indicating we created a route?
|
|
|
|
// lookup is only called in the query path so probably not
|
|
|
|
routes = append(routes, r.createRoutes(srv, domain)...)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
2020-08-07 22:53:38 +03:00
|
|
|
return routes, nil
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 15:08:17 +03:00
|
|
|
// watchRegister watches register and updates routing table based on the received events.
|
|
|
|
// It returns error if either the register watcher fails with error or if the routing table update fails.
|
|
|
|
func (r *rtr) watchRegister(w register.Watcher) error {
|
2020-07-27 15:22:00 +03:00
|
|
|
exit := make(chan bool)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
close(exit)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer w.Stop()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-exit:
|
|
|
|
return
|
2020-08-08 01:44:43 +03:00
|
|
|
case <-r.initChan:
|
|
|
|
return
|
2020-07-27 15:22:00 +03:00
|
|
|
case <-r.exit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
2020-08-07 22:53:38 +03:00
|
|
|
// get the next service
|
2020-07-27 15:22:00 +03:00
|
|
|
res, err := w.Next()
|
|
|
|
if err != nil {
|
2021-01-29 15:08:17 +03:00
|
|
|
if err != register.ErrWatcherStopped {
|
2020-07-27 15:22:00 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-08-07 22:53:38 +03:00
|
|
|
// don't process nil entries
|
2020-07-27 15:22:00 +03:00
|
|
|
if res.Service == nil {
|
2021-01-16 01:12:18 +03:00
|
|
|
if logger.V(logger.TraceLevel) {
|
|
|
|
logger.Trace(r.opts.Context, "Received a nil service")
|
|
|
|
}
|
2020-07-27 15:22:00 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
r.opts.Logger.Tracef(r.opts.Context, "Router dealing with next route %s %+v\n", res.Action, res.Service)
|
|
|
|
}
|
2020-08-18 16:00:51 +03:00
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
// get the services domain from metadata. Fallback to wildcard.
|
2020-08-07 22:53:38 +03:00
|
|
|
domain := getDomain(res.Service)
|
2020-07-27 15:22:00 +03:00
|
|
|
|
2020-08-07 22:53:38 +03:00
|
|
|
// create/update or delete the route
|
2020-07-27 15:22:00 +03:00
|
|
|
if err := r.manageRoutes(res.Service, res.Action, domain); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// start the router. Should be called under lock.
|
|
|
|
func (r *rtr) start() error {
|
|
|
|
if r.running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Precache {
|
|
|
|
// add all local service routes into the routing table
|
2021-01-29 15:08:17 +03:00
|
|
|
if err := r.loadRoutes(r.opts.Register); err != nil {
|
|
|
|
return fmt.Errorf("failed loading register routes: %s", err)
|
2021-01-16 01:12:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
// add default gateway into routing table
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Gateway != "" {
|
2020-07-27 15:22:00 +03:00
|
|
|
// note, the only non-default value is the gateway
|
|
|
|
route := router.Route{
|
|
|
|
Service: "*",
|
|
|
|
Address: "*",
|
2021-01-16 01:12:18 +03:00
|
|
|
Gateway: r.opts.Gateway,
|
2020-07-27 15:22:00 +03:00
|
|
|
Network: "*",
|
2021-01-16 01:12:18 +03:00
|
|
|
Router: r.opts.Id,
|
2020-07-27 15:22:00 +03:00
|
|
|
Link: router.DefaultLink,
|
2021-01-16 01:12:18 +03:00
|
|
|
Metric: router.DefaultLocalMetric,
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
if err := r.table.Create(route); err != nil {
|
|
|
|
return fmt.Errorf("failed adding default gateway route: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create error and exit channels
|
|
|
|
r.exit = make(chan bool)
|
2020-08-21 11:23:01 +03:00
|
|
|
|
2021-01-16 01:12:18 +03:00
|
|
|
// periodically refresh all the routes
|
2020-08-07 22:53:38 +03:00
|
|
|
go func() {
|
2021-01-16 01:12:18 +03:00
|
|
|
t1 := time.NewTicker(RefreshInterval)
|
|
|
|
defer t1.Stop()
|
2020-08-07 22:53:38 +03:00
|
|
|
|
2021-01-16 01:12:18 +03:00
|
|
|
t2 := time.NewTicker(PruneInterval)
|
|
|
|
defer t2.Stop()
|
2020-08-07 22:53:38 +03:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
return
|
2021-01-16 01:12:18 +03:00
|
|
|
case <-t2.C:
|
|
|
|
r.table.pruneRoutes(RefreshInterval)
|
|
|
|
case <-t1.C:
|
2021-01-29 15:08:17 +03:00
|
|
|
if err := r.loadRoutes(r.opts.Register); err != nil {
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Logger.V(logger.DebugLevel) {
|
2021-01-29 15:08:17 +03:00
|
|
|
r.opts.Logger.Debugf(r.opts.Context, "failed refreshing register routes: %s", err)
|
2021-01-16 01:12:18 +03:00
|
|
|
}
|
2020-08-21 11:23:01 +03:00
|
|
|
}
|
2020-08-07 22:53:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
return
|
|
|
|
default:
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Logger.V(logger.TraceLevel) {
|
2021-01-29 15:08:17 +03:00
|
|
|
r.opts.Logger.Tracef(r.opts.Context, "Router starting register watch")
|
2021-01-16 01:12:18 +03:00
|
|
|
}
|
2021-01-29 15:08:17 +03:00
|
|
|
w, err := r.opts.Register.Watch(r.opts.Context, register.WatchDomain(register.WildcardDomain))
|
2020-08-08 03:04:38 +03:00
|
|
|
if err != nil {
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Logger.V(logger.DebugLevel) {
|
2021-01-29 15:08:17 +03:00
|
|
|
r.opts.Logger.Debug(r.opts.Context, "failed creating register watcher: %v", err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
2020-08-08 03:04:38 +03:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 15:08:17 +03:00
|
|
|
// watchRegister calls stop when it's done
|
|
|
|
if err := r.watchRegister(w); err != nil {
|
2021-01-16 01:12:18 +03:00
|
|
|
if r.opts.Logger.V(logger.DebugLevel) {
|
2021-01-29 15:08:17 +03:00
|
|
|
r.opts.Logger.Debugf(r.opts.Context, "Error watching the register: %v", err)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-16 01:12:18 +03:00
|
|
|
r.running = true
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-16 01:12:18 +03:00
|
|
|
// Lookup routes in the routing table
|
|
|
|
func (r *rtr) Lookup(q ...router.QueryOption) ([]router.Route, error) {
|
|
|
|
return r.Table().Query(q...)
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
// Watch routes
|
|
|
|
func (r *rtr) Watch(opts ...router.WatchOption) (router.Watcher, error) {
|
|
|
|
return r.table.Watch(opts...)
|
|
|
|
}
|
|
|
|
|
2021-01-16 01:12:18 +03:00
|
|
|
// Close the router
|
|
|
|
func (r *rtr) Close() error {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
if !r.running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
close(r.exit)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
r.running = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
// String prints debugging information about router
|
|
|
|
func (r *rtr) String() string {
|
2021-01-29 15:08:17 +03:00
|
|
|
return "register"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rtr) Name() string {
|
|
|
|
return r.opts.Name
|
2020-06-24 13:46:51 +03:00
|
|
|
}
|