-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpclient.go
154 lines (124 loc) · 4.08 KB
/
httpclient.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
// Package ruixuego httpclient HTTP 客户端
package ruixuego
import (
"crypto/tls"
"time"
"github.com/valyala/fasthttp"
)
const defaultContentType = "application/json"
// NewHTTPClient create http client
func NewHTTPClient(timeout time.Duration, concurrency int) *HTTPClient {
return &HTTPClient{
client: fasthttp.Client{
TLSConfig: &tls.Config{InsecureSkipVerify: true},
ReadTimeout: timeout,
WriteTimeout: timeout,
MaxConnWaitTimeout: timeout,
},
timeout: timeout,
concurrency: make(chan struct{}, concurrency),
}
}
// HTTPClient a fasthttp client
type HTTPClient struct {
client fasthttp.Client
timeout time.Duration
concurrency chan struct{}
}
func (c *HTTPClient) prepareDo(
url string, req *fasthttp.Request) *fasthttp.Response {
c.concurrency <- struct{}{}
req.SetRequestURI(url)
return fasthttp.AcquireResponse()
}
func (c *HTTPClient) getRequestWithArgs(args *fasthttp.Args) *fasthttp.Request {
req := GetRequest()
req.SetBody(args.QueryString())
// args need to be get by fashhttp.AcquireArgs()
fasthttp.ReleaseArgs(args)
return req
}
func (c *HTTPClient) afterDo(req *fasthttp.Request) {
fasthttp.ReleaseRequest(req)
<-c.concurrency
}
// Do 发起接口请求
func (c *HTTPClient) Do(url string, args *fasthttp.Args) (*fasthttp.Response, error) {
return c.DoWithTimeout(url, args, c.timeout)
}
// DoWithTimeout 发起一个带有超时时间的请求
func (c *HTTPClient) DoWithTimeout(
url string,
args *fasthttp.Args,
timeout time.Duration) (*fasthttp.Response, error) {
req := c.getRequestWithArgs(args)
req.Header.SetContentType(defaultContentType)
return c.DoRequestWithTimeout(url, req, timeout)
}
// DoWithoutTimeout 发起一个没有超时时间的请求
func (c *HTTPClient) DoWithoutTimeout(
url string, args *fasthttp.Args) (*fasthttp.Response, error) {
req := c.getRequestWithArgs(args)
req.Header.SetContentType(defaultContentType)
return c.DoRequestWithoutTimeout(url, req)
}
// DoContentTypeWithTimeout 发起一个带有超时时间的请求
func (c *HTTPClient) DoContentTypeWithTimeout(
contentType, url string,
args *fasthttp.Args, timeout time.Duration) (*fasthttp.Response, error) {
req := c.getRequestWithArgs(args)
req.Header.SetContentType(contentType)
return c.DoRequestWithTimeout(url, req, timeout)
}
// DoContentTypeWithoutTimeout 发起一个没有超时时间的请求
func (c *HTTPClient) DoContentTypeWithoutTimeout(
contentType, url string, args *fasthttp.Args) (*fasthttp.Response, error) {
req := c.getRequestWithArgs(args)
req.Header.SetContentType(contentType)
return c.DoRequestWithoutTimeout(url, req)
}
// DoRequest 指定请求头内容类型发起一个带有超时时间的请求
func (c *HTTPClient) DoRequest(
url string,
req *fasthttp.Request) (*fasthttp.Response, error) {
resp := c.prepareDo(url, req)
defer c.afterDo(req)
if err := c.client.DoTimeout(req, resp, c.timeout); err != nil {
return nil, err
}
return resp, nil
}
// DoRequestWithTimeout 指定请求头内容类型发起一个带有超时时间的请求直接返回 *fasthttp.Response
func (c *HTTPClient) DoRequestWithTimeout(
url string,
req *fasthttp.Request,
timeout time.Duration) (*fasthttp.Response, error) {
resp := c.prepareDo(url, req)
defer c.afterDo(req)
if err := c.client.DoTimeout(req, resp, timeout); err != nil {
return nil, err
}
return resp, nil
}
// DoRequestWithoutTimeout 指定请求头内容类型发起一个没有超时时间的请求
func (c *HTTPClient) DoRequestWithoutTimeout(
url string, req *fasthttp.Request) (*fasthttp.Response, error) {
resp := c.prepareDo(url, req)
defer c.afterDo(req)
if err := c.client.Do(req, resp); err != nil {
return nil, err
}
return resp, nil
}
// GetArgs 获取 fasthttp 参数对象
func GetArgs() *fasthttp.Args {
return fasthttp.AcquireArgs()
}
// GetRequest 获取 fasthttp 请求参数, 用于需要自定义请求头的场景
func GetRequest() *fasthttp.Request {
return fasthttp.AcquireRequest()
}
// PutResponse 将 *fasthttp.Response 返回对象池
func PutResponse(resp *fasthttp.Response) {
fasthttp.ReleaseResponse(resp)
}