-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
82 lines (73 loc) · 2.17 KB
/
client.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package httpify
import (
"net/http"
"time"
)
// Client wraps an HTTP client with retry and retryStrategy logic.
type Client struct {
HTTPClient *http.Client
RequestLogHook RequestLogHook
ResponseLogHook ResponseLogHook
ErrorHandler ErrorHandler
CheckRetry CheckRetry
RetryStrategy RetryStrategy
options Options
}
// Options defines retryable settings for the HTTP client.
type Options struct {
RetryWaitMin time.Duration
RetryWaitMax time.Duration
Timeout time.Duration
RetryMax int
RespReadLimit int64
KillIdleConn bool
}
// Default options for spraying multiple hosts.
var DefaultOptionsSpraying = Options{
RetryWaitMin: 1 * time.Second,
RetryWaitMax: 30 * time.Second,
Timeout: 30 * time.Second,
RetryMax: 5,
RespReadLimit: 4096,
KillIdleConn: true,
}
// Default options for targeting a single host.
var DefaultOptionsSingle = Options{
RetryWaitMin: 1 * time.Second,
RetryWaitMax: 30 * time.Second,
Timeout: 30 * time.Second,
RetryMax: 5,
RespReadLimit: 4096,
KillIdleConn: false,
}
// NewClient initializes a Client with specified options.
func NewClient(options Options) *Client {
httpClient := DefaultHTTPClient(options.Timeout)
return &Client{
HTTPClient: httpClient,
CheckRetry: DefaultRetryPolicy(),
RetryStrategy: DefaultRetryStrategy(),
options: options,
}
}
// NewWithHTTPClient initializes a Client with a custom HTTP client.
func NewWithHTTPClient(client *http.Client, options Options) *Client {
return &Client{
HTTPClient: client,
CheckRetry: DefaultRetryPolicy(),
RetryStrategy: DefaultRetryStrategy(),
options: options,
}
}
// DefaultHTTPClient creates an HTTP client with a default timeout.
func DefaultHTTPClient(timeout time.Duration) *http.Client {
return &http.Client{Timeout: timeout, Transport: NoKeepAliveTransport()}
}
// setKillIdleConnections configures connection keep-alive behavior based on options.
func (c *Client) setKillIdleConnections() {
if c.HTTPClient != nil || !c.options.KillIdleConn {
if b, ok := c.HTTPClient.Transport.(*http.Transport); ok {
c.options.KillIdleConn = b.DisableKeepAlives || b.MaxConnsPerHost < 0
}
}
}