Skip to content

Commit

Permalink
Exponential backoff on HTTP 429
Browse files Browse the repository at this point in the history
  • Loading branch information
Minibrams committed Aug 8, 2024
1 parent 58a3b50 commit 9d38e1d
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions http.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package hjem

import "net/http"
import (
"math"
"net/http"
"time"
)

var DefaultClient http.Client

func init() {
DefaultClient = http.Client{
Transport: &DefaultHeadersTripper{
next: http.DefaultTransport,
headers: map[string]string{
"User-Agent": "tpanum/hjem (github.com/tpanum/hjem)",
Transport: &RetryRoundTripper{
next: &DefaultHeadersTripper{
next: http.DefaultTransport,
headers: map[string]string{
"User-Agent": "tpanum/hjem (github.com/tpanum/hjem)",
},
},
maxRetries: 5,
},
}
}
Expand All @@ -27,3 +34,33 @@ func (t *DefaultHeadersTripper) RoundTrip(req *http.Request) (*http.Response, er

return t.next.RoundTrip(req)
}

type RetryRoundTripper struct {
next http.RoundTripper
maxRetries int
}

func (r *RetryRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
var resp *http.Response
var err error

for i := 0; i <= r.maxRetries; i++ {
resp, err = r.next.RoundTrip(req)

if err != nil {
return nil, err
}

if resp.StatusCode != 429 {
return resp, nil
}

baseWait := 9 * time.Second
backoff := time.Duration(math.Pow(2, float64(i))) * time.Second
wait := baseWait + backoff

time.Sleep(wait)
}

return resp, err
}

0 comments on commit 9d38e1d

Please sign in to comment.