-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretry.go
More file actions
43 lines (39 loc) · 1019 Bytes
/
retry.go
File metadata and controls
43 lines (39 loc) · 1019 Bytes
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
42
43
package restyclient
import (
"time"
"github.com/admpub/resty/v2"
"github.com/webx-top/com"
)
func RetryAfter(c *resty.Client, response *resty.Response) (time.Duration, error) {
retryAfter := response.Header().Get(`Retry-After`)
if len(retryAfter) > 0 {
if com.StrIsNumeric(retryAfter) {
delaySeconds := com.Int64(retryAfter)
return time.Second * time.Duration(delaySeconds), nil
}
t, err := time.Parse(time.RFC1123, retryAfter)
if err != nil {
return 0, err
}
return time.Until(t.Local()), nil
}
remaing := response.Header().Get(`X-Ratelimit-Remaining`)
if len(remaing) > 0 {
if com.Int64(remaing) > 0 {
return 0, nil
}
}
retryAfter = response.Header().Get(`X-Ratelimit-Reset`)
if len(retryAfter) > 0 {
if com.StrIsNumeric(retryAfter) {
timestamp := com.Int64(retryAfter)
return time.Until(time.Unix(timestamp, 0)), nil
}
t, err := time.Parse(time.RFC1123, retryAfter)
if err != nil {
return 0, err
}
return time.Until(t.Local()), nil
}
return 0, nil
}