|
8 | 8 | "net"
|
9 | 9 | "net/http"
|
10 | 10 | "net/url"
|
| 11 | + "strconv" |
| 12 | + "time" |
11 | 13 |
|
12 | 14 | resty "github.com/go-resty/resty/v2"
|
13 | 15 | "github.com/shellhub-io/shellhub/pkg/models"
|
@@ -55,27 +57,76 @@ func NewClient(address string, opts ...Opt) (Client, error) {
|
55 | 57 | return nil, errors.Join(ErrParseAddress, err)
|
56 | 58 | }
|
57 | 59 |
|
| 60 | + const RetryAfterHeader string = "Retry-After" |
| 61 | + |
| 62 | + // DefaultMaxRetryWaitTime is the default value for wait time between retries. |
| 63 | + const DefaultMaxRetryWaitTime time.Duration = 1 * time.Hour |
| 64 | + // DefaultRetryAfterTime is the retry default time when the header [RetryAfterHeader] isn't defined on the response. |
| 65 | + const DefaultRetryAfterTime time.Duration = 5 * time.Second |
| 66 | + |
58 | 67 | client := new(client)
|
59 | 68 | client.http = resty.New()
|
60 | 69 | client.http.SetRetryCount(math.MaxInt32)
|
61 | 70 | client.http.SetRedirectPolicy(SameDomainRedirectPolicy())
|
62 | 71 | client.http.SetBaseURL(uri.String())
|
63 | 72 | client.http.AddRetryCondition(func(r *resty.Response, err error) bool {
|
64 | 73 | if _, ok := err.(net.Error); ok {
|
| 74 | + log.WithFields(log.Fields{ |
| 75 | + "url": r.Request.URL, |
| 76 | + }).WithError(err).Error("network error") |
| 77 | + |
65 | 78 | return true
|
66 | 79 | }
|
67 | 80 |
|
68 |
| - if r.StatusCode() >= http.StatusInternalServerError && r.StatusCode() != http.StatusNotImplemented { |
| 81 | + switch { |
| 82 | + case r.StatusCode() == http.StatusTooManyRequests: |
69 | 83 | log.WithFields(log.Fields{
|
70 | 84 | "status_code": r.StatusCode(),
|
71 | 85 | "url": r.Request.URL,
|
| 86 | + "data": r.String(), |
| 87 | + }).Warn("too many requests") |
| 88 | + |
| 89 | + return true |
| 90 | + case r.StatusCode() >= http.StatusInternalServerError && r.StatusCode() != http.StatusNotImplemented: |
| 91 | + log.WithFields(log.Fields{ |
| 92 | + "status_code": r.StatusCode(), |
| 93 | + "url": r.Request.URL, |
| 94 | + "data": r.String(), |
72 | 95 | }).Warn("failed to achieve the server")
|
73 | 96 |
|
74 | 97 | return true
|
75 | 98 | }
|
76 | 99 |
|
77 | 100 | return false
|
78 | 101 | })
|
| 102 | + client.http.SetRetryAfter(func(c *resty.Client, r *resty.Response) (time.Duration, error) { |
| 103 | + switch r.StatusCode() { |
| 104 | + case http.StatusTooManyRequests, http.StatusServiceUnavailable: |
| 105 | + retryAfterHeader := r.Header().Get(RetryAfterHeader) |
| 106 | + if retryAfterHeader == "" { |
| 107 | + return DefaultRetryAfterTime, nil |
| 108 | + } |
| 109 | + |
| 110 | + // NOTE: The `Retry-After` supports delay in seconds and and a date time, but currently we will support only |
| 111 | + // one of them. |
| 112 | + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Retry-After |
| 113 | + retryAfterSeconds, err := strconv.Atoi(retryAfterHeader) |
| 114 | + if err != nil { |
| 115 | + return DefaultRetryAfterTime, err |
| 116 | + } |
| 117 | + |
| 118 | + log.WithFields(log.Fields{ |
| 119 | + "status": r.StatusCode(), |
| 120 | + "retry_after": retryAfterSeconds, |
| 121 | + "url": r.Request.URL, |
| 122 | + }).Debug("retrying request after a defined time period") |
| 123 | + |
| 124 | + return time.Duration(retryAfterSeconds) * time.Second, nil |
| 125 | + default: |
| 126 | + return DefaultRetryAfterTime, nil |
| 127 | + } |
| 128 | + }) |
| 129 | + client.http.SetRetryMaxWaitTime(DefaultMaxRetryWaitTime) |
79 | 130 |
|
80 | 131 | if client.logger != nil {
|
81 | 132 | client.http.SetLogger(&LeveledLogger{client.logger})
|
|
0 commit comments