From abef9c03a0deb9b93e5ea94d35dde03844683b30 Mon Sep 17 00:00:00 2001 From: Marco Pfatschbacher Date: Mon, 16 Oct 2023 10:17:40 +0100 Subject: [PATCH] Re-create HTTP connection to Graylog 60 update iterations We are using HTTP keep-alive, which otherwise might never pick up any changes to the DNS name of Graylog. Since there is no parameter in golang's http.Client to do this, simply re-create a new HTTP connection every 60 calls. --- changelog/issue-479.toml | 6 ++++++ services/periodicals.go | 19 ++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 changelog/issue-479.toml diff --git a/changelog/issue-479.toml b/changelog/issue-479.toml new file mode 100644 index 0000000..b2b592f --- /dev/null +++ b/changelog/issue-479.toml @@ -0,0 +1,6 @@ +type = "fixed" +message = "Re-create HTTP connection to Graylog every 60 request loops to detect DNS changes." + +issues = ["479"] +pulls = [""] + diff --git a/services/periodicals.go b/services/periodicals.go index 8efa7fb..26d4225 100644 --- a/services/periodicals.go +++ b/services/periodicals.go @@ -29,25 +29,30 @@ import ( "github.com/Graylog2/collector-sidecar/logger" ) +const reCreateHttpConnEvery = 60 + var log = logger.Log() -var httpClient *http.Client func StartPeriodicals(context *context.Ctx) { - if httpClient == nil { - httpClient = rest.NewHTTPClient(api.GetTlsConfig(context)) - } go func() { + var httpClient *http.Client + configChecksums := make(map[string]string) var lastBackendResponse graylog.ResponseBackendList var lastRegResponse graylog.ResponseCollectorRegistration logOnce := true - firstRun := true + iteration := 0 + for { - if !firstRun { + if iteration > 0 { time.Sleep(time.Duration(context.UserConfig.UpdateInterval) * time.Second) } - firstRun = false + // Re-create HTTP connection every X loops: https://github.com/Graylog2/collector-sidecar/issues/479 + if iteration%reCreateHttpConnEvery == 0 { + httpClient = rest.NewHTTPClient(api.GetTlsConfig(context)) + } + iteration++ serverVersion, err := api.GetServerVersion(httpClient, context) if err != nil {