-
Notifications
You must be signed in to change notification settings - Fork 1
/
fasthttp.go
81 lines (68 loc) · 1.45 KB
/
fasthttp.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
package net
import (
"sync"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpproxy"
"github.com/3JoB/telebot/v2/pkg/json"
)
type FastHTTP struct {
client *fasthttp.Client
json json.Json
requestPool *sync.Pool
responsePool *sync.Pool
}
func NewFastHTTPClient() NetFrame {
f := &FastHTTP{
client: &fasthttp.Client{
NoDefaultUserAgentHeader: true,
DisableHeaderNamesNormalizing: false,
Dial: fasthttpproxy.FasthttpProxyHTTPDialer(),
},
requestPool: &sync.Pool{},
responsePool: &sync.Pool{},
}
return f
}
func (f *FastHTTP) SetJsonHandle(v json.Json) {
f.json = v
}
func (f *FastHTTP) Acquire() (NetRequest, NetResponse) {
var r *FastHTTPRequest
if v := f.requestPool.Get(); v == nil {
r = &FastHTTPRequest{}
r.json = f.json
} else {
r = v.(*FastHTTPRequest)
}
r.resp = f.acquireResponse()
r.client = f.client
r.acquire()
return r, r.resp
}
func (f *FastHTTP) acquireResponse() *Response {
r := f.responsePool.Get()
if r == nil {
return &Response{}
}
return r.(*Response)
}
func (f *FastHTTP) Release(req NetRequest, resp NetResponse) {
f.ReleaseRequest(req)
f.ReleaseResponse(resp)
}
func (f *FastHTTP) ReleaseRequest(r NetRequest) {
v, ok := r.(*FastHTTPRequest)
if !ok {
return
}
v.Reset()
f.requestPool.Put(v)
}
func (f *FastHTTP) ReleaseResponse(r NetResponse) {
v, ok := r.(*Response)
if !ok {
return
}
v.Reset()
f.responsePool.Put(v)
}