-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
35 lines (30 loc) · 1010 Bytes
/
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
package httputils
import (
"net/url"
"time"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpproxy"
)
// DefaultClient is a default client
var DefaultClient = fasthttp.Client{
MaxConnsPerHost: 50000,
MaxIdleConnDuration: time.Second * 15, // 空闲 TCP 连接的最大存活时间
MaxConnDuration: time.Second * 10, // 空闲 Handler 连接的最大存活时间
ReadTimeout: time.Second * 90, // 读取超时
MaxConnWaitTimeout: time.Second * 5, // 等待连接的时间
}
func (h *HTTP) SetClient(client *fasthttp.Client) *HTTP { h.client = client; return h }
func (h *HTTP) SetProxy(addr string) *HTTP {
if h.client == nil {
h.client = &fasthttp.Client{}
}
_url, _ := url.Parse(addr)
switch {
case _url == nil:
case _url.Scheme == "http", _url.Scheme == "https":
h.client.Dial = fasthttpproxy.FasthttpHTTPDialer(addr)
case _url.Scheme == "socks5", _url.Scheme == "socks5h":
h.client.Dial = fasthttpproxy.FasthttpSocksDialer(addr)
}
return h
}