Skip to content

Commit f142a50

Browse files
committed
Add ctx to http request
1 parent ee84be3 commit f142a50

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

client/nginx.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package client
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"net/http"
@@ -43,7 +44,14 @@ func NewNginxClient(httpClient *http.Client, apiEndpoint string) (*NginxClient,
4344

4445
// GetStubStats fetches the stub_status metrics.
4546
func (client *NginxClient) GetStubStats() (*StubStats, error) {
46-
resp, err := client.httpClient.Get(client.apiEndpoint)
47+
ctx, cancel := context.WithCancel(context.Background())
48+
defer cancel()
49+
50+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, client.apiEndpoint, nil)
51+
if err != nil {
52+
return nil, fmt.Errorf("failed to create a get request: %w", err)
53+
}
54+
resp, err := client.httpClient.Do(req)
4755
if err != nil {
4856
return nil, fmt.Errorf("failed to get %v: %w", client.apiEndpoint, err)
4957
}

0 commit comments

Comments
 (0)