From 9f8e9aa8d3c1d641d608ca8d1f7b5dea8f2d1688 Mon Sep 17 00:00:00 2001 From: claw Date: Wed, 13 May 2026 06:22:30 -0700 Subject: [PATCH] fix: timer leak and http field shadowing in github client - Add timer.Stop() to the timer.C branch to prevent timer leak on the normal path (previously only called in ctx.Done branch) - Rename struct field 'http' to 'httpClient' to avoid shadowing the net/http import Addresses self-review findings on PR #110. --- github/client.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/github/client.go b/github/client.go index c131d3a..2dc27ca 100644 --- a/github/client.go +++ b/github/client.go @@ -95,7 +95,7 @@ type Client struct { // accepting full URLs instead. baseURL string token string - http *http.Client + httpClient *http.Client // retryBackoff defines the delays between retry attempts for 429 responses. // retryBackoff[i] is the delay before attempt i+1 (after attempt i fails). @@ -117,7 +117,7 @@ func NewClient(token, baseURL string) *Client { return &Client{ baseURL: strings.TrimRight(baseURL, "/"), token: token, - http: &http.Client{Timeout: 30 * time.Second}, + httpClient: &http.Client{Timeout: 30 * time.Second}, now: time.Now, } } @@ -125,7 +125,7 @@ func NewClient(token, baseURL string) *Client { // SetHTTPClient sets the underlying HTTP client used for requests. // This is intended for testing to inject mock transports. func (c *Client) SetHTTPClient(hc *http.Client) { - c.http = hc + c.httpClient = hc } // SetRetryBackoff sets the delays between retry attempts. @@ -192,6 +192,7 @@ func (c *Client) doRequest(ctx context.Context, method, reqURL string, accept st timer := time.NewTimer(delay) select { case <-timer.C: + timer.Stop() case <-ctx.Done(): timer.Stop() return nil, ctx.Err() @@ -210,7 +211,7 @@ func (c *Client) doRequest(ctx context.Context, method, reqURL string, accept st req.Header.Set("Accept", "application/vnd.github+json") } - resp, err := c.http.Do(req) + resp, err := c.httpClient.Do(req) if err != nil { return nil, fmt.Errorf("do request: %w", err) }