Skip to content

Commit 46e489a

Browse files
authored
fix: nil check against the embedded http.Response (#469)
The local `*Response` should never be nil, but the embedded `*http.Response` might be nil, for example when the context is cancelled. Can be reproduced with: ``` go test -run ^TestWaitFor/fail_with_canceled_context$ -count 100 github.com/hetznercloud/hcloud-go/v2/hcloud ```
1 parent 03226d1 commit 46e489a

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

hcloud/client_handler_rate_limit.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ type rateLimitHandler struct {
1717
func (h *rateLimitHandler) Do(req *http.Request, v any) (resp *Response, err error) {
1818
resp, err = h.handler.Do(req, v)
1919

20-
if resp != nil && resp.Header != nil {
20+
// Ensure the embedded [*http.Response] is not nil, e.g. on canceled context
21+
if resp != nil && resp.Response != nil && resp.Response.Header != nil {
2122
if h := resp.Header.Get("RateLimit-Limit"); h != "" {
2223
resp.Meta.Ratelimit.Limit, _ = strconv.Atoi(h)
2324
}

0 commit comments

Comments
 (0)