-
Notifications
You must be signed in to change notification settings - Fork 107
/
backoff.go
41 lines (35 loc) · 874 Bytes
/
backoff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package resource
import (
"errors"
"net/http"
"os"
"time"
"github.com/cenkalti/backoff"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
"github.com/sirupsen/logrus"
)
func RetryOnRateLimit(op func() error) error {
bo := backoff.NewExponentialBackOff()
if os.Getenv("TEST") == "true" {
bo.InitialInterval = 5 * time.Millisecond
} else {
bo.InitialInterval = 5 * time.Second
}
bo.MaxInterval = 5 * time.Minute
bo.MaxElapsedTime = 1 * time.Hour
return backoff.RetryNotify(func() error {
err := op()
if err == nil {
return nil
}
var transportErr *transport.Error
if errors.As(err, &transportErr) {
if transportErr.StatusCode == http.StatusTooManyRequests {
return err
}
}
return backoff.Permanent(err)
}, bo, func(err error, dur time.Duration) {
logrus.Warnf("too many requests; retrying in %s", dur)
})
}