From 7a14738300f07a2fe0d75e09c545de5c0bb8805c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandor=20Sz=C3=BCcs?= Date: Thu, 26 Oct 2023 21:52:31 +0200 Subject: [PATCH] feature: shutdown supportServer gracfully and test shutdown behavior for it too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sandor Szücs --- routesrv/polling.go | 1 - routesrv/routesrv.go | 5 ++++ routesrv/shutdown_test.go | 57 +++++++++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/routesrv/polling.go b/routesrv/polling.go index f142d273d7..5e811c5497 100644 --- a/routesrv/polling.go +++ b/routesrv/polling.go @@ -92,7 +92,6 @@ func (p *poller) poll(wg *sync.WaitGroup) { p.setGaugeToCurrentTime("routes.updated_timestamp") p.metrics.UpdateGauge("routes.total", float64(routesCount)) p.metrics.UpdateGauge("routes.byte", float64(routesBytes)) - } span.SetTag("routes.count", routesCount) span.SetTag("routes.bytes", routesBytes) diff --git a/routesrv/routesrv.go b/routesrv/routesrv.go index 02498e8d80..bb88d4fd17 100644 --- a/routesrv/routesrv.go +++ b/routesrv/routesrv.go @@ -178,6 +178,11 @@ func newShutdownFunc(rs *RouteServer) func(delay time.Duration) { log.Infof("shutting down the server in %s...", delay) time.Sleep(delay) + if rs.supportServer != nil { + if err := rs.supportServer.Shutdown(context.Background()); err != nil { + log.Error("unable to shut down the support server: ", err) + } + } if err := rs.server.Shutdown(context.Background()); err != nil { log.Error("unable to shut down the server: ", err) } diff --git a/routesrv/shutdown_test.go b/routesrv/shutdown_test.go index e27b5a9479..928cc1b230 100644 --- a/routesrv/shutdown_test.go +++ b/routesrv/shutdown_test.go @@ -25,6 +25,7 @@ func findAddress() (string, error) { func TestServerShutdownHTTP(t *testing.T) { o := skipper.Options{ SourcePollTimeout: 500 * time.Millisecond, + SupportListener: ":9911", } const shutdownDelay = 1 * time.Second @@ -34,7 +35,15 @@ func TestServerShutdownHTTP(t *testing.T) { } o.Address, o.WaitForHealthcheckInterval = address, shutdownDelay - baseUrl := "http://" + address + baseURL := "http://" + address + // test support listener + host, _, err := net.SplitHostPort(address) + if err != nil { + t.Fatal(err) + } + supportBaseURL := "http://" + host + o.SupportListener + testEndpoints := []string{baseURL + "/routes", supportBaseURL + "/metrics"} + rs, err := New(o) if err != nil { t.Fatalf("Failed to create a routesrv: %v", err) @@ -50,12 +59,15 @@ func TestServerShutdownHTTP(t *testing.T) { } }() - rsp, err := http.DefaultClient.Get(baseUrl + "/metrics") - if err != nil { - t.Fatalf("Failed to get routes: %v", err) - } - if rsp.StatusCode != 200 { - t.Fatalf("Failed to get expected status code 200, got: %d", rsp.StatusCode) + time.Sleep(o.SourcePollTimeout * 2) + for _, u := range testEndpoints { + rsp, err := http.DefaultClient.Get(u) + if err != nil { + t.Fatalf("Failed to get %q: %v", u, err) + } + if rsp.StatusCode != 200 { + t.Fatalf("Failed to get expected status code 200 for %q, got: %d", u, rsp.StatusCode) + } } // initiate shutdown @@ -64,23 +76,28 @@ func TestServerShutdownHTTP(t *testing.T) { // test that we can fetch even within termination time.Sleep(shutdownDelay / 2) - rsp, err = http.DefaultClient.Get(baseUrl + "/metrics") - if err != nil { - t.Fatalf("Failed to get routes after SIGTERM: %v", err) - } - if rsp.StatusCode != 200 { - t.Fatalf("Failed to get expected status code 200 after SIGTERM, got: %d", rsp.StatusCode) + for _, u := range testEndpoints { + rsp, err := http.DefaultClient.Get(u) + if err != nil { + t.Fatalf("Failed to get %q after SIGTERM: %v", u, err) + } + if rsp.StatusCode != 200 { + t.Fatalf("Failed to get expected status code 200 for %q after SIGTERM, got: %d", u, rsp.StatusCode) + } } // test that we get connection refused after shutdown time.Sleep(shutdownDelay / 2) - _, err = http.DefaultClient.Get(baseUrl + "/metrics") - switch err { - case nil: - t.Fatal("Failed to get error as expected") - default: - if e := err.Error(); !strings.Contains(e, "refused") { - t.Fatalf("Failed to get connection refused, got: %s", e) + + for _, u := range testEndpoints { + _, err = http.DefaultClient.Get(u) + switch err { + case nil: + t.Fatalf("Failed to get error as expected: %q", u) + default: + if e := err.Error(); !strings.Contains(e, "refused") { + t.Fatalf("Failed to get connection refused, got: %s", e) + } } }