Skip to content

Commit 5546ef7

Browse files
FFM-11006 Add Connection close header (#141)
* FFM-11006 Add Connection close header **What** - Adds a connection close header to the http requests to prevent connections being re-used **Why** - We were seeing every other POST /metrics request fail with an EOF error but when we ran with the debugger this didn't happen. After some research it seems like this can happen due to the way the http client tries to re-use connections. Adding this header means we'll close the connection once the request has completed **Testing** - Tested this fix out locally Co-authored-by: Erdi Rowlands <[email protected]>
1 parent 7d99c9a commit 5546ef7

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

.harness/ffgolangserversdk.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pipeline:
3636
identifier: Submodule_Init
3737
spec:
3838
connectorRef: DockerHub
39-
image: golang:1.18.6
39+
image: golang:1.19.9
4040
shell: Sh
4141
command: "mkdir -p ~/.ssh\nssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts\n\ncat <<EOF >> .gitmodules\n[submodule \"tests/ff-test-cases\"]\n\tpath = tests/ff-test-cases\n\turl = https://github.com/drone/ff-test-cases.git\nEOF\n\ngit submodule update --init --recursive"
4242
- parallel:
@@ -46,7 +46,7 @@ pipeline:
4646
identifier: Build_and_Test
4747
spec:
4848
connectorRef: DockerHub
49-
image: golang:1.18.6
49+
image: golang:1.19.9
5050
shell: Sh
5151
command: |-
5252
go install github.com/jstemmer/go-junit-report@latest

client/client.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8-
"github.com/cenkalti/backoff/v4"
9-
"github.com/harness/ff-golang-server-sdk/sdk_codes"
10-
"golang.org/x/sync/errgroup"
118
"log"
129
"net/http"
1310
"strings"
1411
"sync"
1512
"sync/atomic"
1613
"time"
1714

15+
"github.com/cenkalti/backoff/v4"
16+
"github.com/harness/ff-golang-server-sdk/sdk_codes"
17+
"golang.org/x/sync/errgroup"
18+
1819
"github.com/harness/ff-golang-server-sdk/evaluation"
1920
"github.com/harness/ff-golang-server-sdk/logger"
2021

@@ -393,14 +394,20 @@ func (c *CfClient) authenticate(ctx context.Context) error {
393394
// Use a custom transport which adds headers for tracking usage
394395
// The `WithRequestEditorFn` cannot be used for SSE requests, so we need to provide a custom transport to the
395396
// http client so that these headers can be added to all requests.
396-
getHeadersFn := func() (map[string]string, error) {
397-
return map[string]string{
397+
getHeadersFn := func(r *http.Request) (map[string]string, error) {
398+
headers := map[string]string{
398399
"User-Agent": "GoSDK/" + analyticsservice.SdkVersion,
399400
"Harness-SDK-Info": fmt.Sprintf("Go %s Server", analyticsservice.SdkVersion),
400401
"Harness-EnvironmentID": c.environmentID,
401-
}, nil
402+
}
403+
404+
if strings.Contains(r.URL.Path, "/metrics") && r.Method == http.MethodPost {
405+
headers["Connection"] = "close"
406+
}
407+
408+
return headers, nil
402409
}
403-
410+
404411
// Wrap the httpClient's transport with our own custom transport, which currently just adds extra headers
405412
// for analytics purposes.
406413
// If the httpClient doesn't have a Transport we can honour, then just use a default transport.

client/transport.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package client
33
import "net/http"
44

55
// HeadersFn is a function type that provides headers dynamically.
6-
type HeadersFn func() (map[string]string, error)
6+
type HeadersFn func(r *http.Request) (map[string]string, error)
77

88
// customTransport wraps an http.RoundTripper and allows adding headers dynamically. This means we can still use
99
// the goretryable-http client's transport, or a user's transport if they've provided their own http client.
@@ -22,7 +22,7 @@ func NewCustomTransport(baseTransport http.RoundTripper, getHeaderFn HeadersFn)
2222

2323
func (t *customTransport) RoundTrip(req *http.Request) (*http.Response, error) {
2424
// Retrieve the headers using the provided function.
25-
headers, err := t.getHeaders()
25+
headers, err := t.getHeaders(req)
2626
if err != nil {
2727
return nil, err
2828
}

0 commit comments

Comments
 (0)