-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasthttp_client.go
64 lines (56 loc) · 1.4 KB
/
fasthttp_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
/*
* // Copyright 2020 Insolar Network Ltd.
* // All rights reserved.
* // This material is licensed under the Insolar License version 1.0,
* // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md.
*/
package loaderbot
import (
"log"
"reflect"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/valyala/fasthttp"
)
type FastHTTPClient struct {
dump bool
fasthttp.Client
}
// NewLoggingFastHTTPClient creates new client with debug http
func NewLoggingFastHTTPClient(debug bool) *FastHTTPClient {
return &FastHTTPClient{
debug,
fasthttp.Client{
// Dial: func(addr string) (net.Conn, error) {
// return fasthttp.DialTimeout(addr, 1*time.Second)
// },
MaxConnsPerHost: 65535,
MaxIdleConnDuration: 90 * time.Second,
MaxIdemponentCallAttempts: 0,
},
}
}
func (m *FastHTTPClient) Do(req *fasthttp.Request, resp *fasthttp.Response) error {
if m.dump {
log.Printf(RequestHeader, req.String())
}
if err := m.Client.DoRedirects(req, resp, 5); err != nil {
return err
}
if m.dump {
log.Printf(ResponseHeader, resp.String())
}
return nil
}
func UnmarshalAnyJson(d []byte, typ interface{}) (interface{}, error) {
if typ == nil || d == nil {
return nil, nil
}
t := reflect.TypeOf(typ).Elem()
v := reflect.New(t)
newP := v.Interface()
if err := jsoniter.Unmarshal(d, newP); err != nil {
return nil, err
}
return newP, nil
}