-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
fasthttp_http_client_factory.go
58 lines (50 loc) · 1.33 KB
/
fasthttp_http_client_factory.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
package main
import (
"context"
"crypto/tls"
"net"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpproxy"
)
type fasthttpHttpClientFactory struct {
}
func newFasthttpHttpClientFactory() *fasthttpHttpClientFactory {
return &fasthttpHttpClientFactory{}
}
func (*fasthttpHttpClientFactory) Create(o httpClientOptions) httpClient {
d := func(address string) (net.Conn, error) {
return fasthttp.DialTimeout(address, tcpTimeout)
}
if o.Proxy != "" {
d = fasthttpproxy.FasthttpHTTPDialerTimeout(o.Proxy, tcpTimeout)
} else if o.DnsResolver != "" {
nd := &net.Dialer{}
td := &fasthttp.TCPDialer{
Concurrency: concurrency,
Resolver: &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
return nd.DialContext(ctx, "udp", o.DnsResolver)
},
},
}
d = func(address string) (net.Conn, error) {
return td.DialTimeout(address, tcpTimeout)
}
}
return newFasthttpHttpClient(
&fasthttp.Client{
MaxConnsPerHost: o.MaxConnectionsPerHost,
ReadBufferSize: o.BufferSize,
TLSConfig: &tls.Config{
InsecureSkipVerify: o.SkipTLSVerification,
},
Dial: d,
DisablePathNormalizing: true,
NoDefaultUserAgentHeader: true,
MaxResponseBodySize: o.MaxResponseBodySize,
},
o.Timeout,
o.Header,
)
}