-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathcore.go
More file actions
321 lines (273 loc) · 8.52 KB
/
core.go
File metadata and controls
321 lines (273 loc) · 8.52 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Core pipeline scaffolds request execution for Fiber's HTTP client, including
// hook invocation, retry orchestration, and timeout management around fasthttp
// transports.
package client
import (
"context"
"errors"
"fmt"
"net"
"slices"
"strconv"
"strings"
"sync"
"github.com/valyala/fasthttp"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/addon/retry"
)
const boundary = "FiberFormBoundary"
// RequestHook is a function invoked before the request is sent.
// It receives a Client and a Request, allowing you to modify the Request or Client data.
type RequestHook func(*Client, *Request) error
// ResponseHook is a function invoked after a response is received.
// It receives a Client, Response, and Request, allowing you to modify the Response data
// or perform actions based on the response.
type ResponseHook func(*Client, *Response, *Request) error
// RetryConfig is an alias for the `retry.Config` type from the `addon/retry` package.
type RetryConfig = retry.Config
// addMissingPort appends the appropriate port number to the given address if it doesn't have one.
// If isTLS is true, it uses port 443; otherwise, it uses port 80.
func addMissingPort(addr string, isTLS bool) string { //revive:disable-line:flag-parameter
if strings.IndexByte(addr, ':') != -1 {
return addr
}
port := 80
if isTLS {
port = 443
}
return net.JoinHostPort(addr, strconv.Itoa(port))
}
// core stores middleware and plugin definitions and defines the request execution process.
type core struct {
client *Client
req *Request
ctx context.Context //nolint:containedctx // Context is needed here.
}
// getRetryConfig returns a copy of the client's retry configuration.
func (c *core) getRetryConfig() *RetryConfig {
c.client.mu.RLock()
defer c.client.mu.RUnlock()
cfg := c.client.RetryConfig()
if cfg == nil {
return nil
}
return &RetryConfig{
InitialInterval: cfg.InitialInterval,
MaxBackoffTime: cfg.MaxBackoffTime,
Multiplier: cfg.Multiplier,
MaxRetryCount: cfg.MaxRetryCount,
}
}
// execFunc is the core logic to send the request and receive the response.
// It leverages the fasthttp client, optionally with retries or redirects.
func (c *core) execFunc() (*Response, error) {
// do not close, these will be returned to the pool
errChan := acquireErrChan()
respChan := acquireResponseChan()
cfg := c.getRetryConfig()
go func() {
// retain both channels until they are drained
defer releaseErrChan(errChan)
defer releaseResponseChan(respChan)
reqv := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(reqv)
respv := fasthttp.AcquireResponse()
defer func() {
if respv != nil {
fasthttp.ReleaseResponse(respv)
}
}()
var resp *Response
defer func() {
if r := recover(); r != nil {
if resp != nil {
ReleaseResponse(resp)
}
errChan <- fmt.Errorf("client panic: %v", r)
}
}()
c.req.RawRequest.CopyTo(reqv)
if bodyStream := c.req.RawRequest.BodyStream(); bodyStream != nil {
reqv.SetBodyStream(bodyStream, c.req.RawRequest.Header.ContentLength())
}
var err error
if cfg != nil {
// Use an exponential backoff retry strategy.
err = retry.NewExponentialBackoff(*cfg).Retry(func() error {
if c.req.maxRedirects > 0 && (string(reqv.Header.Method()) == fiber.MethodGet || string(reqv.Header.Method()) == fiber.MethodHead) {
return c.client.DoRedirects(reqv, respv, c.req.maxRedirects)
}
return c.client.Do(reqv, respv)
})
} else {
if c.req.maxRedirects > 0 && (string(reqv.Header.Method()) == fiber.MethodGet || string(reqv.Header.Method()) == fiber.MethodHead) {
err = c.client.DoRedirects(reqv, respv, c.req.maxRedirects)
} else {
err = c.client.Do(reqv, respv)
}
}
if err != nil {
errChan <- err
return
}
resp = AcquireResponse()
resp.setClient(c.client)
resp.setRequest(c.req)
// Swap the fasthttp response with the Fiber response's RawResponse field.
// This is required, as (*fasthttp.Response).CopyTo() explicitly does not
// copy body streams.
//
// See: https://github.com/valyala/fasthttp/blob/v1.69.0/http.go#L909-L923
//
// The defer statement above ensures that the original RawResponse
// (now stored in respv) will be properly released.
resp.RawResponse, respv = respv, resp.RawResponse
respChan <- resp
}()
select {
case err := <-errChan:
return nil, err
case resp := <-respChan:
return resp, nil
case <-c.ctx.Done():
go func() { // drain the channels and release the response
select {
case resp := <-respChan:
ReleaseResponse(resp)
case <-errChan:
}
}()
return nil, ErrTimeoutOrCancel
}
}
// preHooks runs all request hooks before sending the request.
func (c *core) preHooks() error {
c.client.mu.RLock()
userHooks := slices.Clone(c.client.userRequestHooks)
c.client.mu.RUnlock()
for _, f := range userHooks {
if err := f(c.client, c.req); err != nil {
return err
}
}
c.client.mu.Lock()
defer c.client.mu.Unlock()
for _, f := range c.client.builtinRequestHooks {
if err := f(c.client, c.req); err != nil {
return err
}
}
return nil
}
// afterHooks runs all response hooks after receiving the response.
func (c *core) afterHooks(resp *Response) error {
c.client.mu.Lock()
userHooks := slices.Clone(c.client.userResponseHooks)
for _, f := range c.client.builtinResponseHooks {
if err := f(c.client, resp, c.req); err != nil {
c.client.mu.Unlock()
return err
}
}
c.client.mu.Unlock()
for _, f := range userHooks {
if err := f(c.client, resp, c.req); err != nil {
return err
}
}
return nil
}
// timeout applies the configured timeout to the request, if any.
func (c *core) timeout() context.CancelFunc {
var cancel context.CancelFunc
if c.req.timeout > 0 {
c.ctx, cancel = context.WithTimeout(c.ctx, c.req.timeout)
} else if c.client.timeout > 0 {
c.ctx, cancel = context.WithTimeout(c.ctx, c.client.timeout)
}
return cancel
}
// execute runs all hooks, applies timeouts, sends the request, and runs response hooks.
func (c *core) execute(ctx context.Context, client *Client, req *Request) (*Response, error) {
// Store references locally.
c.ctx = ctx
c.client = client
c.req = req
// Execute pre request hooks (user-defined and built-in).
if err := c.preHooks(); err != nil {
return nil, err
}
// Apply timeout if specified.
cancel := c.timeout()
if cancel != nil {
defer cancel()
}
// Perform the actual HTTP request.
resp, err := c.execFunc()
if err != nil {
return nil, err
}
// Execute after response hooks (built-in and then user-defined).
if err := c.afterHooks(resp); err != nil {
resp.Close()
return nil, err
}
return resp, nil
}
var responseChanPool = &sync.Pool{
New: func() any {
return make(chan *Response)
},
}
// acquireResponseChan returns an empty, non-closed *Response channel from the pool.
// The returned channel may be returned to the pool with releaseResponseChan
func acquireResponseChan() chan *Response {
ch, ok := responseChanPool.Get().(chan *Response)
if !ok {
panic(errResponseChanTypeAssertion)
}
return ch
}
// releaseResponseChan returns the *Response channel to the pool.
// It's the caller's responsibility to ensure that:
// - the channel is not closed
// - the channel is drained before returning it
// - the channel is not reused after returning it
func releaseResponseChan(ch chan *Response) {
responseChanPool.Put(ch)
}
var errChanPool = &sync.Pool{
New: func() any {
return make(chan error)
},
}
// acquireErrChan returns an empty, non-closed error channel from the pool.
// The returned channel may be returned to the pool with releaseErrChan
func acquireErrChan() chan error {
ch, ok := errChanPool.Get().(chan error)
if !ok {
panic(errChanErrorTypeAssertion)
}
return ch
}
// releaseErrChan returns the error channel to the pool.
// It's caller's responsibility to ensure that:
// - the channel is not closed
// - the channel is drained before returning it
// - the channel is not reused after returning it
func releaseErrChan(ch chan error) {
errChanPool.Put(ch)
}
// newCore returns a new core object.
func newCore() *core {
return &core{}
}
var (
ErrTimeoutOrCancel = errors.New("timeout or cancel")
ErrURLFormat = errors.New("the URL is incorrect")
ErrNotSupportSchema = errors.New("protocol not supported; only http or https are allowed")
ErrFileNoName = errors.New("the file should have a name")
ErrBodyType = errors.New("the body type should be []byte")
ErrNotSupportSaveMethod = errors.New("only file paths and io.Writer are supported")
ErrBodyTypeNotSupported = errors.New("the body type is not supported")
)