-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoptions_retry.go
More file actions
252 lines (236 loc) · 7.63 KB
/
Copy pathoptions_retry.go
File metadata and controls
252 lines (236 loc) · 7.63 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package httpx
import (
"time"
"github.com/imroc/req/v3"
)
// RetryCount enables retry for a request and sets the maximum retry count.
// Default behavior from req: retries are disabled (count = 0). When enabled,
// retries happen only on request errors unless RetryCondition is set, and the
// default interval is a fixed 100ms between attempts.
// @group Retry
//
// Applies to both client defaults and individual requests.
// Example: retry count
//
// // Apply to all requests
// c := httpx.New(httpx.RetryCount(2))
// res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/uuid")
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // uuid => "<uuid>" #string
// // }
//
// // Apply to a single request
// res, _ = httpx.Get[map[string]any](c, "https://httpbin.org/uuid", httpx.RetryCount(2))
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // uuid => "<uuid>" #string
// // }
func RetryCount(count int) OptionBuilder {
return OptionBuilder{}.RetryCount(count)
}
func (b OptionBuilder) RetryCount(count int) OptionBuilder {
return b.add(bothOption(
func(c *Client) {
c.req.SetCommonRetryCount(count)
},
func(r *req.Request) {
r.SetRetryCount(count)
},
))
}
// RetryFixedInterval sets a fixed retry interval for a request.
// Overrides the req default interval (fixed 100ms).
// @group Retry
//
// Applies to both client defaults and individual requests.
// Example: retry interval
//
// // Apply to all requests
// c := httpx.New(httpx.RetryFixedInterval(200 * time.Millisecond))
// res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/uuid")
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // uuid => "<uuid>" #string
// // }
//
// // Apply to a single request
// res, _ = httpx.Get[map[string]any](c, "https://httpbin.org/uuid", httpx.RetryFixedInterval(200*time.Millisecond))
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // uuid => "<uuid>" #string
// // }
func RetryFixedInterval(interval time.Duration) OptionBuilder {
return OptionBuilder{}.RetryFixedInterval(interval)
}
func (b OptionBuilder) RetryFixedInterval(interval time.Duration) OptionBuilder {
return b.add(bothOption(
func(c *Client) {
c.req.SetCommonRetryFixedInterval(interval)
},
func(r *req.Request) {
r.SetRetryFixedInterval(interval)
},
))
}
// RetryBackoff sets a capped exponential backoff retry interval for a request.
// Overrides the req default interval (fixed 100ms) with jittered backoff.
// @group Retry
//
// Applies to both client defaults and individual requests.
// Example: retry backoff
//
// // Apply to all requests
// c := httpx.New(httpx.RetryBackoff(100*time.Millisecond, 2*time.Second))
// res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/uuid")
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // uuid => "<uuid>" #string
// // }
//
// // Apply to a single request
// res, _ = httpx.Get[map[string]any](c, "https://httpbin.org/uuid", httpx.RetryBackoff(100*time.Millisecond, 2*time.Second))
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // uuid => "<uuid>" #string
// // }
func RetryBackoff(min, max time.Duration) OptionBuilder {
return OptionBuilder{}.RetryBackoff(min, max)
}
func (b OptionBuilder) RetryBackoff(min, max time.Duration) OptionBuilder {
return b.add(bothOption(
func(c *Client) {
c.req.SetCommonRetryBackoffInterval(min, max)
},
func(r *req.Request) {
r.SetRetryBackoffInterval(min, max)
},
))
}
// RetryInterval sets a custom retry interval function for a request.
// Overrides the req default interval (fixed 100ms).
// @group Retry
//
// Applies to both client defaults and individual requests.
// Example: custom retry interval
//
// // Apply to all requests
// c := httpx.New(httpx.RetryInterval(func(_ *req.Response, attempt int) time.Duration {
// return time.Duration(attempt) * 100 * time.Millisecond
// }))
// res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/uuid")
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // uuid => "<uuid>" #string
// // }
//
// // Apply to a single request
// res, _ = httpx.Get[map[string]any](c, "https://httpbin.org/uuid", httpx.RetryInterval(func(_ *req.Response, attempt int) time.Duration {
// return time.Duration(attempt) * 100 * time.Millisecond
// }))
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // uuid => "<uuid>" #string
// // }
func RetryInterval(fn req.GetRetryIntervalFunc) OptionBuilder {
return OptionBuilder{}.RetryInterval(fn)
}
func (b OptionBuilder) RetryInterval(fn req.GetRetryIntervalFunc) OptionBuilder {
return b.add(bothOption(
func(c *Client) {
c.req.SetCommonRetryInterval(fn)
},
func(r *req.Request) {
r.SetRetryInterval(fn)
},
))
}
// RetryCondition sets the retry condition for a request.
// Overrides the default behavior (retry only when a request error occurs).
// @group Retry
//
// Applies to both client defaults and individual requests.
// Example: retry on 503
//
// // Apply to all requests
// c := httpx.New(httpx.RetryCondition(func(resp *req.Response, _ error) bool {
// return resp != nil && resp.StatusCode == 503
// }))
// res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/status/503")
// httpx.Dump(res) // dumps map[string]any
// // map[string]interface {}(nil)
//
// // Apply to a single request
// res, _ = httpx.Get[map[string]any](c, "https://httpbin.org/status/503", httpx.RetryCondition(func(resp *req.Response, _ error) bool {
// return resp != nil && resp.StatusCode == 503
// }))
// httpx.Dump(res) // dumps map[string]any
// // map[string]interface {}(nil)
func RetryCondition(condition req.RetryConditionFunc) OptionBuilder {
return OptionBuilder{}.RetryCondition(condition)
}
func (b OptionBuilder) RetryCondition(condition req.RetryConditionFunc) OptionBuilder {
return b.add(bothOption(
func(c *Client) {
c.req.SetCommonRetryCondition(condition)
},
func(r *req.Request) {
r.SetRetryCondition(condition)
},
))
}
// RetryHook registers a retry hook for a request.
// Runs before each retry attempt; no hooks are configured by default.
// @group Retry
//
// Applies to both client defaults and individual requests.
// Example: hook on retry
//
// // Apply to all requests
// c := httpx.New(httpx.RetryHook(func(_ *req.Response, _ error) {}))
// res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/uuid")
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // uuid => "<uuid>" #string
// // }
//
// // Apply to a single request
// res, _ = httpx.Get[map[string]any](c, "https://httpbin.org/uuid", httpx.RetryHook(func(_ *req.Response, _ error) {}))
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // uuid => "<uuid>" #string
// // }
func RetryHook(hook req.RetryHookFunc) OptionBuilder {
return OptionBuilder{}.RetryHook(hook)
}
func (b OptionBuilder) RetryHook(hook req.RetryHookFunc) OptionBuilder {
return b.add(bothOption(
func(c *Client) {
c.req.SetCommonRetryHook(hook)
},
func(r *req.Request) {
r.SetRetryHook(hook)
},
))
}
// Retry applies a custom retry configuration to the client.
// Defaults remain in effect unless the callback modifies them.
// @group Retry (Client)
//
// Applies only to client configuration.
// Example: configure client retry
//
// _ = httpx.New(httpx.Retry(func(rc *req.Client) {
// rc.SetCommonRetryCount(2)
// }))
func Retry(fn func(*req.Client)) OptionBuilder {
return OptionBuilder{}.Retry(fn)
}
func (b OptionBuilder) Retry(fn func(*req.Client)) OptionBuilder {
return b.add(clientOnly(func(c *Client) {
if fn == nil {
return
}
fn(c.req)
}))
}