-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
138 lines (116 loc) · 4.19 KB
/
builder.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package webs
import (
"net"
"net/http"
"time"
)
const (
// defaultResponseTimeout is the default duration to wait for a response before timing out in HTTP client requests.
defaultResponseTimeout = 0
// defaultConnectionTimeout specifies the default timeout duration for establishing a connection to an HTTP server.
defaultConnectionTimeout = 0
// defaultMaxIdleConnsPerHost specifies the default maximum number of idle connections to keep per host in the HTTP client.
defaultMaxIdleConnsPerHost = 1
)
// Builder defines an interface for constructing RequestHandler objects with configurable HTTP client settings.
type Builder interface {
Build() *Client
}
// Transporter defines an interface for getting an *http.Transport instance.
type Transporter interface {
getTransport() *http.Transport
}
// DefaultsRetriever defines methods for retrieving default configuration values like connection timeout, response timeout, and max idle connections.
type DefaultsRetriever interface {
getConnectionTimeout() time.Duration
getResponseTimeout() time.Duration
getMaxIdleConnsPerHost() int
}
// ClientBuilder assists in creating customized HTTP clients by configuring headers, timeouts, and connection limits.
type ClientBuilder struct {
headers http.Header
transport http.Transport
connectTimeout time.Duration
responseTimeout time.Duration
disableTimeouts bool
maxIdleConnsPerHost int
}
// NewClientBuilder creates a new instance of ClientBuilder for configuring customized HTTP clients.
func NewClientBuilder() *ClientBuilder {
return &ClientBuilder{}
}
// Build finalizes the ClientBuilder configuration and returns a newly constructed Client instance.
func (cb *ClientBuilder) Build() *Client {
transport := cb.getTransport()
baseClient := &http.Client{
Transport: transport,
Timeout: cb.getConnectionTimeout(),
}
client := &Client{
client: baseClient,
headers: cb.headers,
}
return client
}
// SetHeaders sets custom HTTP headers to be used in the requests.
func (cb *ClientBuilder) SetHeaders(headers http.Header) *ClientBuilder {
cb.headers = headers
return cb
}
// DisableTimeouts configures the ClientBuilder to enable or disable timeout settings.
func (cb *ClientBuilder) DisableTimeouts(disable bool) *ClientBuilder {
cb.disableTimeouts = disable
return cb
}
// SetConnectTimeout sets the connection timeout duration for the client.
func (cb *ClientBuilder) SetConnectTimeout(timeout time.Duration) *ClientBuilder {
cb.connectTimeout = timeout
return cb
}
// SetResponseTimeout sets the response timeout duration for the client.
func (cb *ClientBuilder) SetResponseTimeout(timeout time.Duration) *ClientBuilder {
cb.responseTimeout = timeout
return cb
}
// SetMaxIdleConnectionsPerHost sets the maximum number of idle connections to keep per-host for the HTTP client.
func (cb *ClientBuilder) SetMaxIdleConnectionsPerHost(maxIdleConnsPerHost int) *ClientBuilder {
cb.maxIdleConnsPerHost = maxIdleConnsPerHost
return cb
}
// getResponseTimeout calculates and returns the appropriate response timeout duration for the HTTP client.
func (cb *ClientBuilder) getResponseTimeout() time.Duration {
if cb.responseTimeout > 0 {
return cb.responseTimeout
}
if cb.disableTimeouts {
return 0
}
return defaultResponseTimeout
}
// getConnectionTimeout returns the configured connection timeout duration or the default value if not set.
func (cb *ClientBuilder) getConnectionTimeout() time.Duration {
if cb.connectTimeout > 0 {
return cb.connectTimeout
}
if cb.disableTimeouts {
return 0
}
return defaultConnectionTimeout
}
// getMaxIdleConnsPerHost returns the maximum number of idle connections per host. If not set, defaults to 1.
func (cb *ClientBuilder) getMaxIdleConnsPerHost() int {
if cb.maxIdleConnsPerHost > 0 {
return cb.maxIdleConnsPerHost
}
return defaultMaxIdleConnsPerHost
}
// getTransport configures and returns an *http.Transport with custom timeout settings and connection limits.
func (cb *ClientBuilder) getTransport() *http.Transport {
return &http.Transport{
MaxIdleConnsPerHost: cb.getMaxIdleConnsPerHost(),
ResponseHeaderTimeout: cb.getResponseTimeout(),
DialContext: (&net.Dialer{
Timeout: cb.getConnectionTimeout(),
}).DialContext,
}
}