Skip to content

Commit

Permalink
Merge pull request #5 from Minibrams/main
Browse files Browse the repository at this point in the history
Exponential backoff on HTTP 429
  • Loading branch information
tpanum authored Aug 9, 2024
2 parents 58a3b50 + 5c1cd48 commit 991c63f
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 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,30 @@ 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
}

backoff := time.Duration(math.Pow(1.5, float64(i))) * time.Second
time.Sleep(backoff)
}

return resp, err
}

0 comments on commit 991c63f

Please sign in to comment.