Skip to content

Commit

Permalink
fix: nil check against the embedded http.Response (#469)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
jooola authored Jun 24, 2024
1 parent 03226d1 commit 46e489a
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion hcloud/client_handler_rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type rateLimitHandler struct {
func (h *rateLimitHandler) Do(req *http.Request, v any) (resp *Response, err error) {
resp, err = h.handler.Do(req, v)

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

0 comments on commit 46e489a

Please sign in to comment.