Dong.Ge

左手代码 右手江湖

golang http请求使用pool

东哥's Avatar 2019-12-14

3种方式,其中原生的那个是我们项目使用的,有问题,这个程序跑之后,那个goroutine 一直增长.
别的两个一直稳定不变.

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
package main

import (
"fmt"
"io/ioutil"
"net"
"net/http"
_ "net/http/pprof"
"strings"
"time"

"github.com/levigross/grequests"
"github.com/valyala/fasthttp"
)

type HttpTimeout struct {
DialTimeout int
DialKeepAlive int
TLSHandshakeTimeout int
ResponseHeaderTimeout int
ExpectContinueTimeout int
Timeout int
}

func SetHttpTimeout(dialTimeout, dialKeepAlive, tlsHandshakeTimeout, responseHeaderTimeout, expectContinueTimeout, timeout int) HttpTimeout {
var tt HttpTimeout

tt.DialTimeout = dialTimeout
tt.DialKeepAlive = dialKeepAlive
tt.TLSHandshakeTimeout = tlsHandshakeTimeout
tt.ResponseHeaderTimeout = responseHeaderTimeout
tt.ExpectContinueTimeout = expectContinueTimeout
tt.Timeout = timeout

return tt
}

func DefaultHttpTimeout() HttpTimeout {
return SetHttpTimeout(30, 60, 60, 90, 30, 90)
}

func ShortHttpTimeout() HttpTimeout {
return SetHttpTimeout(1, 1, 1, 1, 1, 1)
}

func LongHttpTimeout() HttpTimeout {
return SetHttpTimeout(60, 60, 60, 180, 90, 180)
}

func HttClientWithTimeout(timeoutConf HttpTimeout) (client *http.Client) {
var netTransport = &http.Transport{
Dial: (&net.Dialer{
Timeout: time.Second * time.Duration(timeoutConf.DialTimeout),
KeepAlive: time.Second * time.Duration(timeoutConf.DialKeepAlive),
}).Dial,
TLSHandshakeTimeout: time.Second * time.Duration(timeoutConf.TLSHandshakeTimeout),
ResponseHeaderTimeout: time.Second * time.Duration(timeoutConf.ResponseHeaderTimeout),
ExpectContinueTimeout: time.Second * time.Duration(timeoutConf.ExpectContinueTimeout),
}
client = &http.Client{
Timeout: time.Second * time.Duration(timeoutConf.Timeout),
Transport: netTransport,
}

return
}

func SimpleHttpClient(reqMethod string, reqUrl string, reqHeaders map[string]string, reqBody string, timeoutConf HttpTimeout) ([]byte, int, error) {
var httpStatusCode int
var emptyBody []byte

req, err := http.NewRequest(reqMethod, reqUrl, strings.NewReader(reqBody))
if err != nil {
return emptyBody, httpStatusCode, err
}

for k, v := range reqHeaders {
req.Header.Set(k, v)
}

client := HttClientWithTimeout(timeoutConf)
resp, err := client.Do(req)
if resp != nil {
httpStatusCode = resp.StatusCode
return emptyBody, httpStatusCode, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)

if err != nil {
fmt.Println("[SimpleHttpClient] read request body fail, reqUrl:", reqUrl, ", err:", err)
}

if resp.StatusCode != http.StatusOK {
fmt.Println("[SimpleHttpClient] status code")
}

return body, httpStatusCode, err
}

func grequestsPost(url string, postData map[string]string) (error, string) {
resp, err := grequests.Post(url,
&grequests.RequestOptions{Data: postData})
return err, resp.String()
}

func fasthttpPost(url string) {
args := &fasthttp.Args{}
fasthttp.Post(nil, url, args)
}

func main() {

go func() {
for {
//grequestsPost()("http://www.baidu.com/", map[string]string{})
//SimpleHttpClient("POST", "http://www.baidu.com/", map[string]string{}, "", DefaultHttpTimeout())
fasthttpPost("http://www.baidu.com/")

}
}()
fmt.Println("pprof start...")
http.ListenAndServe(":9876", nil)
}

本文最后更新于 天前,文中所描述的信息可能已发生改变