-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeout.go
200 lines (187 loc) · 6.48 KB
/
timeout.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package go_requests
import (
"time"
)
const (
// defaultMaxIdleConnectionsPerHost is the default value for MaxIdleConnectionsPerHost
defaultMaxIdleConnectionsPerHost = 10
// defaultResponseTimeout is the default value for ResponseTimeout
defaultResponseTimeout = 5 * time.Second
// defaultRequestTimeout is the default value for RequestTimeout
defaultRequestTimeout = 5 * time.Second
)
// timeoutImpl is a struct that holds the configuration for the http client
//
// RequestTimeout: the Timeout for the request
// ResponseTimeout: the Timeout for the response
// MaxIdleConnections: the maximum number of idle connections
type timeoutImpl struct {
// RequestTimeout is the Timeout for the request
ResponseTimeout time.Duration
// ResponseTimeout is the Timeout for the response
RequestTimeout time.Duration
// MaxIdleConnections is the maximum number of idle connections
MaxIdleConnections int
// DisableTimeouts disables the timeouts for the client
DisableTimeouts bool
}
// newTimeouts returns a new instance of timeoutImpl
// with the default values
//
// Example:
// client := newTimeouts()
func newTimeouts() *timeoutImpl {
return &timeoutImpl{
ResponseTimeout: defaultResponseTimeout,
RequestTimeout: defaultRequestTimeout,
MaxIdleConnections: defaultMaxIdleConnectionsPerHost,
DisableTimeouts: false,
}
}
// Timeout is an interface that holds the configuration for the http client.
type Timeout interface {
// SetRequestTimeout sets the request Timeout
// if the value is 0, the request will not Timeout.
// Example:
// client.SetRequestTimeout(10 * time.Second)
// client.SetRequestTimeout(0)
SetRequestTimeout(time.Duration) Timeout
// SetResponseTimeout sets the response Timeout
// if the value is 0, the response will not Timeout.
// Example:
// client.SetResponseTimeout(10 * time.Second)
// client.SetResponseTimeout(0)
SetResponseTimeout(time.Duration) Timeout
// SetMaxIdleConnections sets the maximum number of idle connections
// if the value is 0, the maximum number of idle connections will be set to 10
// Example:
// client.SetMaxIdleConnections(10)
// client.SetMaxIdleConnections(0)
SetMaxIdleConnections(int) Timeout
// GetMaxIdleConnections returns the maximum number of idle connections
// if the value is not set, it returns the default value.
// default value is 10
// Example:
// client.GetMaxIdleConnections()
GetMaxIdleConnections() int
// GetRequestTimeout returns the request Timeout
// if the request Timeout is not set, it returns the default request Timeout.
// Example:
// client.GetRequestTimeout()
GetRequestTimeout() time.Duration
// GetResponseTimeout returns the response Timeout
// if the request Timeout is not set, it returns the default response Timeout
// Example:
// client.GetResponseTimeout()
GetResponseTimeout() time.Duration
// Disable disables the Timeout
// Example:
// client.Disable()
Disable() Timeout
// Enable enables the Timeout
// Example:
// client.Enable()
Enable() Timeout
}
// GetRequestTimeout returns the request Timeout
// if the request Timeout is not set, it returns the default request Timeout.
func (c timeoutImpl) GetRequestTimeout() time.Duration {
if c.RequestTimeout != defaultRequestTimeout {
return c.RequestTimeout
}
if c.DisableTimeouts {
return 0
}
return defaultRequestTimeout
}
// GetResponseTimeout returns the response Timeout
// if the request Timeout is not set, it returns the default response Timeout
func (c timeoutImpl) GetResponseTimeout() time.Duration {
if c.ResponseTimeout != defaultResponseTimeout {
return c.ResponseTimeout
}
if c.DisableTimeouts {
return 0
}
return defaultResponseTimeout
}
// GetMaxIdleConnections
//
// Returns the maximum number of idle connections
// if the value is not set, it returns the default value.
//
// default value is 10
func (c timeoutImpl) GetMaxIdleConnections() int {
if c.MaxIdleConnections != defaultMaxIdleConnectionsPerHost {
return c.MaxIdleConnections
}
return defaultMaxIdleConnectionsPerHost
}
// Disable disables the timeouts for the client
// if the value is true, the timeouts will be disabled
// and the client will not time out.
//
// Example:
// client.DisableTimeouts(true)
func (c timeoutImpl) Disable() Timeout {
c.DisableTimeouts = true
return c
}
// Enable enables the timeouts for the client.
// if the value is false, the timeouts will be enabled
// and the client will time out.
// if the value is not set, the timeouts will be enabled
// and the client will time out.
//
// Example:
// client.DisableTimeouts(false)
func (c timeoutImpl) Enable() Timeout {
c.DisableTimeouts = false
return c
}
// SetRequestTimeout sets the request Timeout
//
// If the value is 0, the request will not Timeout.
// If the value is negative, the request will time out immediately.
// If the value is positive, the request will time out after the specified duration.
// If the value is not set, the request will time out after 5 seconds.
// If the value is not set and the DisableTimeouts is set to true, the request will not Timeout.
//
// Example:
// client.SetRequestTimeout(10 * time.Second)
func (c timeoutImpl) SetRequestTimeout(timeout time.Duration) Timeout {
c.RequestTimeout = timeout
return c
}
// SetResponseTimeout sets the response Timeout.
//
// If the value is 0, the response will not Timeout.
// If the value is negative, the response will time out immediately.
// If the value is positive, the response will time out after the specified duration.
// If the value is not set, the response will time out after 5 seconds.
// If the value is not set and the DisableTimeouts is set to true, the response will not Timeout.
//
// Example:
// client.SetResponseTimeout(10 * time.Second)
// client.SetResponseTimeout(0)
// client.SetResponseTimeout(-1)
// client.SetResponseTimeout(10 * time.Second)
func (c timeoutImpl) SetResponseTimeout(timeout time.Duration) Timeout {
c.ResponseTimeout = timeout
return c
}
// SetMaxIdleConnections sets the maximum number of idle connections.
//
// If the value is not set, the default value is 10.
// If the value is 0, the maximum number of idle connections is unlimited.
// If the value is negative, the maximum number of idle connections is unlimited.
// If the value is positive, the maximum number of idle connections is the specified value.
//
// Example:
// client.SetMaxIdleConnections(10)
// client.SetMaxIdleConnections(0)
// client.SetMaxIdleConnections(-1)
func (c timeoutImpl) SetMaxIdleConnections(maxConnections int) Timeout {
c.MaxIdleConnections = maxConnections
return c
}