-
Notifications
You must be signed in to change notification settings - Fork 1
/
gonet.go
83 lines (71 loc) · 1.41 KB
/
gonet.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
package net
import (
"os"
"sync"
"github.com/3JoB/resty-ilo"
"github.com/3JoB/telebot/v2/pkg/json"
)
type GoNet struct {
client *resty.Client
json json.Json
requestPool *sync.Pool
responsePool *sync.Pool
}
func NewHTTPClient() NetFrame {
g := &GoNet{
client: resty.New(),
requestPool: &sync.Pool{},
responsePool: &sync.Pool{},
}
g.lookProxyEnv()
return g
}
func (g *GoNet) SetJsonHandle(v json.Json) {
g.client.JSONMarshal = v.Marshal
g.client.JSONUnmarshal = v.Unmarshal
g.json = v
}
func (g *GoNet) Acquire() (NetRequest, NetResponse) {
var r *GoNetRequest
if v := g.requestPool.Get(); v == nil {
r = &GoNetRequest{}
r.json = g.json
} else {
r = v.(*GoNetRequest)
}
r.r = g.client.R()
r.resp = g.acquireResponse()
return r, r.resp
}
func (g *GoNet) acquireResponse() *Response {
v := g.responsePool.Get()
if v == nil {
return &Response{}
}
return v.(*Response)
}
func (g *GoNet) Release(req NetRequest, resp NetResponse) {
g.ReleaseRequest(req)
g.ReleaseResponse(resp)
}
func (g *GoNet) ReleaseRequest(r NetRequest) {
v, ok := r.(*GoNetRequest)
if !ok {
return
}
v.Reset()
g.requestPool.Put(v)
}
func (g *GoNet) ReleaseResponse(r NetResponse) {
v, ok := r.(*Response)
if !ok {
return
}
v.Reset()
g.responsePool.Put(v)
}
func (g *GoNet) lookProxyEnv() {
if http_proxy, ok := os.LookupEnv("http_proxy"); ok {
g.client = g.client.SetProxy(http_proxy)
}
}