-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathretry_http_client.go
More file actions
44 lines (35 loc) · 823 Bytes
/
Copy pathretry_http_client.go
File metadata and controls
44 lines (35 loc) · 823 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
44
package main
import (
"fmt"
"net"
"net/http"
"net/url"
"time"
)
type retryHttpClient struct {
client httpClient
maxCount uint
initialDelay time.Duration
}
func newRetryHttpClient(c httpClient, maxCount uint, initialDelay time.Duration) httpClient {
return &retryHttpClient{c, maxCount, initialDelay}
}
func (c *retryHttpClient) Get(u *url.URL, header http.Header) (httpResponse, error) {
d := c.initialDelay
e := error(nil)
for range c.maxCount + 1 {
r, err := c.client.Get(u, header)
if err == nil {
return r, nil
} else if e, ok := err.(net.Error); !ok || !e.Timeout() {
return nil, err
}
time.Sleep(d)
d = min(retryBackoff*d, maxRetryDelay)
e = err
}
if c.maxCount == 0 {
return nil, e
}
return nil, fmt.Errorf("max retry count %d exceeded: %w", c.maxCount, e)
}