-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
159 lines (129 loc) · 3.57 KB
/
client.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
package gos
import (
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/tech-engine/goscrapy/internal/types"
)
type clientOpts struct {
timeout time.Duration
transportOpts
}
type transportOpts struct {
proxyFn func(*http.Request) (*url.URL, error)
maxIdleConns, maxConnsPerHost, maxIdleConnsPerHost int
}
func defaultClientOpts() clientOpts {
opts := clientOpts{
timeout: MIDDLEWARE_DEFAULT_HTTP_TIMEOUT_MS * time.Millisecond,
transportOpts: transportOpts{
proxyFn: nil,
maxIdleConns: MIDDLEWARE_DEFAULT_HTTP_MAX_IDLE_CONN,
maxConnsPerHost: MIDDLEWARE_DEFAULT_HTTP_MAX_CONN_PER_HOST,
maxIdleConnsPerHost: MIDDLEWARE_DEFAULT_HTTP_MAX_IDLE_CONN_PER_HOST,
},
}
value, ok := os.LookupEnv("MIDDLEWARE_HTTP_MAX_IDLE_CONN")
if ok {
maxIdleConn, err := strconv.Atoi(value)
if err == nil {
opts.maxIdleConns = maxIdleConn
}
}
value, ok = os.LookupEnv("MIDDLEWARE_HTTP_MAX_CONN_PER_HOST")
if ok {
maxConnPerHost, err := strconv.Atoi(value)
if err == nil {
opts.maxConnsPerHost = maxConnPerHost
}
}
value, ok = os.LookupEnv("MIDDLEWARE_HTTP_MAX_IDLE_CONN_PER_HOST")
if ok {
maxIdleConnPerHost, err := strconv.Atoi(value)
if err == nil {
opts.maxConnsPerHost = maxIdleConnPerHost
}
}
value, ok = os.LookupEnv("MIDDLEWARE_HTTP_TIMEOUT_MS")
if ok {
timeoutMs, err := strconv.Atoi(value)
if err == nil {
opts.timeout = time.Duration(timeoutMs) * time.Millisecond
}
}
return opts
}
func WithTimeout(t time.Duration) types.OptFunc[clientOpts] {
return func(opts *clientOpts) {
opts.timeout = t
}
}
func WithMaxIdleConns(maxIdleConns int) types.OptFunc[clientOpts] {
return func(opts *clientOpts) {
opts.maxIdleConns = maxIdleConns
}
}
func WithMaxConnsPerHost(maxConnsPerHost int) types.OptFunc[clientOpts] {
return func(opts *clientOpts) {
opts.maxConnsPerHost = maxConnsPerHost
}
}
func WithMaxIdleConnsPerHost(maxIdleConnsPerHost int) types.OptFunc[clientOpts] {
return func(opts *clientOpts) {
opts.maxIdleConnsPerHost = maxIdleConnsPerHost
}
}
func WithProxyFn(fn func(*http.Request) (*url.URL, error)) types.OptFunc[clientOpts] {
return func(opts *clientOpts) {
opts.proxyFn = fn
}
}
func WithProxies(proxies ...string) types.OptFunc[clientOpts] {
return func(opts *clientOpts) {
proxyUrls := make([]*url.URL, 0, len(proxies))
for _, proxy := range proxies {
u, err := url.Parse(strings.TrimSpace(proxy))
if err != nil {
log.Panic(err)
return
}
proxyUrls = append(proxyUrls, u)
}
opts.proxyFn = roundRobin(proxyUrls)
}
}
// round robin algo for proxy rotation
func roundRobin(urls []*url.URL) func(*http.Request) (*url.URL, error) {
var index uint32
len := uint32(len(urls))
return func(*http.Request) (*url.URL, error) {
index := atomic.AddUint32(&index, 1)
u := urls[(index-1)%len]
return u, nil
}
}
// createDefaultHTTPClient creates a default http client with defaults.
// If default values are set in the env it will pick the defaults from the env.
func DefaultClient(opts ...types.OptFunc[clientOpts]) *http.Client {
cli := &http.Client{}
// load in default options
cliOpts := defaultClientOpts()
for _, opt := range opts {
opt(&cliOpts)
}
t := http.DefaultTransport.(*http.Transport).Clone()
// set all value from transport options
t.MaxIdleConns = cliOpts.maxIdleConns
t.MaxConnsPerHost = cliOpts.maxConnsPerHost
t.MaxIdleConnsPerHost = cliOpts.maxIdleConnsPerHost
t.Proxy = cliOpts.proxyFn
// set client options
cli.Timeout = cliOpts.timeout
cli.Transport = t
return cli
}