From bdb874a6a87c8bbfd494bd8b26f08d3fe529a1ee Mon Sep 17 00:00:00 2001 From: Waldemar Quevedo Date: Mon, 21 Aug 2023 11:06:07 -0700 Subject: [PATCH] Update LastActivity on connect for routes Signed-off-by: Waldemar Quevedo --- server/route.go | 8 +++++++- server/routes_test.go | 13 ++++++++++++- test/route_discovery_test.go | 18 +++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/server/route.go b/server/route.go index 6f6adbe057..3883f3a565 100644 --- a/server/route.go +++ b/server/route.go @@ -1905,6 +1905,9 @@ func (s *Server) addRoute(c *client, didSolicit bool, info *Info, accName string c.mu.Lock() idHash := c.route.idHash cid := c.cid + if c.last.IsZero() { + c.last = time.Now() + } c.mu.Unlock() // Store this route with key being the route id hash + account name @@ -1980,10 +1983,13 @@ func (s *Server) addRoute(c *client, didSolicit bool, info *Info, accName string rHash := c.route.hash rn := c.route.remoteName url := c.route.url - // For solicited routes, we need now to send the INFO protocol + // For solicited routes, we need now to send the INFO protocol. if didSolicit { c.enqueueProto(s.generateRouteInitialInfoJSON(_EMPTY_, c.route.compression, idx)) } + if c.last.IsZero() { + c.last = time.Now() + } c.mu.Unlock() // Add to the slice and bump the count of connections for this remote diff --git a/server/routes_test.go b/server/routes_test.go index 73218a5d71..b3d8bfb160 100644 --- a/server/routes_test.go +++ b/server/routes_test.go @@ -1153,7 +1153,18 @@ func TestRouteNoCrashOnAddingSubToRoute(t *testing.T) { defer rs.Shutdown() servers = append(servers, rs) - // Create a sub on each routed server + // Confirm routes are active before clients connect. + for _, srv := range servers { + rz, err := srv.Routez(nil) + require_NoError(t, err) + for i, route := range rz.Routes { + if route.LastActivity.IsZero() { + t.Errorf("Expected LastActivity to be valid (%d)", i) + } + } + } + + // Create a sub on each routed server. nc := natsConnect(t, fmt.Sprintf("nats://%s:%d", ropts.Host, ropts.Port)) defer nc.Close() natsSub(t, nc, "foo", cb) diff --git a/test/route_discovery_test.go b/test/route_discovery_test.go index d51d3c234f..3c7a87da25 100644 --- a/test/route_discovery_test.go +++ b/test/route_discovery_test.go @@ -183,9 +183,14 @@ func TestSeedSolicitWorks(t *testing.T) { // Wait for a bit for graph to connect time.Sleep(500 * time.Millisecond) - // Grab Routez from monitor ports, make sure we are fully connected + // Grab Routez from monitor ports, make sure we are fully connected. url := fmt.Sprintf("http://%s:%d/", opts.Host, opts.HTTPPort) rz := readHTTPRoutez(t, url) + for _, route := range rz.Routes { + if route.LastActivity.IsZero() { + t.Error("Expected LastActivity to be valid\n") + } + } ris := expectRids(t, rz, []string{s2.ID(), s3.ID()}) if ris[s2.ID()].IsConfigured { t.Fatalf("Expected server not to be configured\n") @@ -194,8 +199,14 @@ func TestSeedSolicitWorks(t *testing.T) { t.Fatalf("Expected server not to be configured\n") } + // Server 2 did solicit routes to Server 1. url = fmt.Sprintf("http://%s:%d/", s2Opts.Host, s2Opts.HTTPPort) rz = readHTTPRoutez(t, url) + for _, route := range rz.Routes { + if route.LastActivity.IsZero() { + t.Error("Expected LastActivity to be valid") + } + } ris = expectRids(t, rz, []string{s1.ID(), s3.ID()}) if !ris[s1.ID()].IsConfigured { t.Fatalf("Expected seed server to be configured\n") @@ -206,6 +217,11 @@ func TestSeedSolicitWorks(t *testing.T) { url = fmt.Sprintf("http://%s:%d/", s3Opts.Host, s3Opts.HTTPPort) rz = readHTTPRoutez(t, url) + for _, route := range rz.Routes { + if route.LastActivity.IsZero() { + t.Error("Expected LastActivity to be valid") + } + } ris = expectRids(t, rz, []string{s1.ID(), s2.ID()}) if !ris[s1.ID()].IsConfigured { t.Fatalf("Expected seed server to be configured\n")