Skip to content

Commit

Permalink
Re-create HTTP connection to Graylog 60 update iterations (#483)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mpfz0r authored Oct 16, 2023
1 parent 4becbce commit f87982d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
6 changes: 6 additions & 0 deletions changelog/issue-479.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type = "fixed"
message = "Re-create HTTP connection to Graylog every 60 request loops to detect DNS changes."

issues = ["479"]
pulls = [""]

19 changes: 12 additions & 7 deletions services/periodicals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f87982d

Please sign in to comment.