Skip to content

Commit

Permalink
Merge pull request #144 from movio/remove-polling-client-keep-alive
Browse files Browse the repository at this point in the history
Remove service polling client keep alive
  • Loading branch information
Lucian Jones authored Apr 11, 2022
2 parents cc52747 + 3bb4c9d commit 03264a7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
18 changes: 18 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ func NewClient(opts ...ClientOpt) *GraphQLClient {
return c
}

func NewClientWithoutKeepAlive(opts ...ClientOpt) *GraphQLClient {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.DisableKeepAlives = true
c := &GraphQLClient{
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
Transport: transport,
},
MaxResponseSize: 1024 * 1024,
}

for _, opt := range opts {
opt(c)
}

return c
}

// WithHTTPClient sets a custom HTTP client to be used when making downstream queries.
func WithHTTPClient(client *http.Client) ClientOpt {
return func(s *GraphQLClient) {
Expand Down
17 changes: 17 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ func TestGraphqlClient(t *testing.T) {
assert.Equal(t, "value", res.Root.Test)
})

t.Run("without keep-alive", func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "close", r.Header.Get("Connection"))
w.Write([]byte(`{
"data": {
"root": {
"test": "value"
}
}
}`))
}))

c := NewClientWithoutKeepAlive()
err := c.Request(context.Background(), srv.URL, &Request{}, nil)
assert.NoError(t, err)
})

t.Run("with http client", func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("test_cookie")
Expand Down
2 changes: 1 addition & 1 deletion introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Service struct {
func NewService(serviceURL string) *Service {
s := &Service{
ServiceURL: serviceURL,
client: NewClient(WithUserAgent(GenerateUserAgent("update"))),
client: NewClientWithoutKeepAlive(WithUserAgent(GenerateUserAgent("update"))),
}
return s
}
Expand Down

0 comments on commit 03264a7

Please sign in to comment.