From 2b79910ad90fa0dbcbad790238f31d3e0caf331f Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Thu, 6 Aug 2020 11:32:06 +0100 Subject: [PATCH] add logging and don't get nodes where they exist in router (#1898) * add logging and don't get nodes where they exist in router * add more logging --- registry/etcd/watcher.go | 2 +- router/options.go | 9 +++++++++ router/registry/registry.go | 32 +++++++++++++++++++++++++++----- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/registry/etcd/watcher.go b/registry/etcd/watcher.go index 8a812a93..6b5d7224 100644 --- a/registry/etcd/watcher.go +++ b/registry/etcd/watcher.go @@ -36,7 +36,7 @@ func newEtcdWatcher(r *etcdRegistry, timeout time.Duration, opts ...registry.Wat watchPath := prefix if wo.Domain == registry.WildcardDomain { if len(wo.Service) > 0 { - return nil, errors.New("Cannot watch a service accross domains") + return nil, errors.New("Cannot watch a service across domains") } watchPath = prefix } else if len(wo.Service) > 0 { diff --git a/router/options.go b/router/options.go index f2ac4e91..0b82c673 100644 --- a/router/options.go +++ b/router/options.go @@ -24,6 +24,8 @@ type Options struct { Advertise Strategy // Context for additional options Context context.Context + // Precache routes + Precache bool } // Id sets Router Id @@ -68,6 +70,13 @@ func Advertise(a Strategy) Option { } } +// Precache the routes +func Precache() Option { + return func(o *Options) { + o.Precache = true + } +} + // DefaultOptions returns router default options func DefaultOptions() Options { return Options{ diff --git a/router/registry/registry.go b/router/registry/registry.go index ac192857..1eaa540c 100644 --- a/router/registry/registry.go +++ b/router/registry/registry.go @@ -157,21 +157,37 @@ func (r *rtr) manageRegistryRoutes(reg registry.Registry, action string) error { for _, service := range services { // get the services domain from metadata. Fallback to wildcard. var domain string + if service.Metadata != nil && len(service.Metadata["domain"]) > 0 { domain = service.Metadata["domain"] } else { domain = registry.WildcardDomain } + // we already have nodes + if len(service.Nodes) > 0 { + logger.Tracef("Creating route %v domain: %v", service, domain) + if err := r.manageRoutes(service, action, domain); err != nil { + logger.Tracef("Failed to manage route for %v domain: %v", service, domain) + } + continue + } + + // otherwise get all the service info + // get the service to retrieve all its info srvs, err := reg.GetService(service.Name, registry.GetDomain(domain)) if err != nil { + logger.Tracef("Failed to get service %s domain: %s", service.Name, domain) continue } + // manage the routes for all returned services for _, srv := range srvs { + logger.Tracef("Creating route %v domain: %v", srv, domain) if err := r.manageRoutes(srv, action, domain); err != nil { - return err + logger.Tracef("Failed to manage route for %v domain: %v", srv, domain) + continue } } } @@ -183,8 +199,10 @@ func (r *rtr) manageRegistryRoutes(reg registry.Registry, action string) error { func (r *rtr) fetchRoutes(service string) error { services, err := r.options.Registry.GetService(service, registry.GetDomain(registry.WildcardDomain)) if err == registry.ErrNotFound { + logger.Tracef("Failed to find route for %s", service) return nil } else if err != nil { + logger.Tracef("Failed to find route for %s: %v", service, err) return fmt.Errorf("failed getting services: %v", err) } @@ -202,8 +220,10 @@ func (r *rtr) fetchRoutes(service string) error { domain = registry.WildcardDomain } + logger.Tracef("Creating route %v domain: %v", srv, domain) if err := r.manageRoutes(srv, "create", domain); err != nil { - return err + logger.Tracef("Failed to create route for %v domain %v: %v", err) + continue } } @@ -473,9 +493,11 @@ func (r *rtr) start() error { return nil } - // add all local service routes into the routing table - if err := r.manageRegistryRoutes(r.options.Registry, "create"); err != nil { - return fmt.Errorf("failed adding registry routes: %s", err) + if r.options.Precache { + // add all local service routes into the routing table + if err := r.manageRegistryRoutes(r.options.Registry, "create"); err != nil { + return fmt.Errorf("failed adding registry routes: %s", err) + } } // add default gateway into routing table