-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.go
53 lines (47 loc) · 1.34 KB
/
utils.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
package main
import (
"bytes"
"errors"
"net"
"strings"
"time"
"github.com/valyala/fasthttp"
)
const httpClientTimeout = 15 * time.Second
const dialTimeout = 7 * time.Second
var httpClient = &fasthttp.Client{
ReadTimeout: 30 * time.Second,
MaxConnsPerHost: 233,
MaxIdleConnDuration: 15 * time.Minute,
ReadBufferSize: 1024 * 8,
Dial: func(addr string) (net.Conn, error) {
// no suitable address found => ipv6 can not dial to ipv4,..
hostname, port, err := net.SplitHostPort(addr)
if err != nil {
if err1, ok := err.(*net.AddrError); ok && strings.Index(err1.Err, "missing port") != -1 {
hostname, port, err = net.SplitHostPort(strings.TrimRight(addr, ":") + ":80")
}
if err != nil {
return nil, err
}
}
if port == "" || port == ":" {
port = "80"
}
return fasthttp.DialDualStackTimeout("["+hostname+"]:"+port, dialTimeout)
},
}
var errEncodingNotSupported = errors.New("response content encoding not supported")
func getResponseBody(resp *fasthttp.Response) ([]byte, error) {
var contentEncoding = resp.Header.Peek("Content-Encoding")
if len(contentEncoding) < 1 {
return resp.Body(), nil
}
if bytes.Equal(contentEncoding, []byte("gzip")) {
return resp.BodyGunzip()
}
if bytes.Equal(contentEncoding, []byte("deflate")) {
return resp.BodyInflate()
}
return nil, errEncodingNotSupported
}