Skip to content

Commit 4153460

Browse files
comtalystclaude
andcommitted
fix: close HTTP response body and thread context in pricing API
The pricing API pagination loop had two issues: 1. res.Body was never closed, leaking an HTTP connection and its file descriptor on every page fetch. Over dozens of pages during each 12-hour pricing update, this accumulates leaked connections. 2. http.Get was used instead of http.NewRequestWithContext, meaning the context parameter was ignored (_ context.Context). This prevented cancellation during operator shutdown, potentially blocking the goroutine indefinitely. Fix: use http.NewRequestWithContext to propagate the context, and explicitly close res.Body after reading (not defer, since we're in a loop). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e51328d commit 4153460

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

pkg/providers/pricing/client/pricingapi.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func New(cloud cloud.Configuration) PricingAPI {
4646
return &pricingAPI{cloud: cloud}
4747
}
4848

49-
func (papi *pricingAPI) GetProductsPricePages(_ context.Context, filters []*Filter, pageHandler func(output *ProductsPricePage)) error {
49+
func (papi *pricingAPI) GetProductsPricePages(ctx context.Context, filters []*Filter, pageHandler func(output *ProductsPricePage)) error {
5050
nextURL := pricingURL
5151

5252
if !auth.IsPublic(papi.cloud) {
@@ -66,16 +66,22 @@ func (papi *pricingAPI) GetProductsPricePages(_ context.Context, filters []*Filt
6666
}
6767

6868
for nextURL != "" {
69-
res, err := http.Get(nextURL)
69+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, nextURL, nil)
70+
if err != nil {
71+
return fmt.Errorf("creating pricing request: %w", err)
72+
}
73+
res, err := http.DefaultClient.Do(req)
7074
if err != nil {
7175
return err
7276
}
7377

7478
if res.StatusCode != 200 {
79+
res.Body.Close()
7580
return fmt.Errorf("got a non-200 status code: %d", res.StatusCode)
7681
}
7782

7883
resBody, err := io.ReadAll(res.Body)
84+
res.Body.Close()
7985
if err != nil {
8086
return err
8187
}

0 commit comments

Comments
 (0)