/
fasthttp_client.go
225 lines (200 loc) · 6.26 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* rpc/fasthttp/fasthttp_client.go *
* *
* hprose fasthttp client for Go. *
* *
* LastModified: Jan 7, 2017 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package fasthttp
import (
"crypto/tls"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/hprose/hprose-golang/rpc"
"github.com/hprose/hprose-golang/util"
"github.com/valyala/fasthttp"
)
var httpSchemes = []string{"http", "https"}
type cookieManager struct {
store map[string]*fasthttp.Cookie
locker sync.Mutex
}
func newCookieManager() (cm *cookieManager) {
cm = &cookieManager{store: make(map[string]*fasthttp.Cookie)}
return
}
func (cm *cookieManager) saveCookie(resp *fasthttp.Response) {
cm.locker.Lock()
defer cm.locker.Unlock()
resp.Header.VisitAllCookie(func(key, value []byte) {
k := util.ByteString(key)
cookie := fasthttp.AcquireCookie()
cookie.SetKeyBytes(key)
resp.Header.Cookie(cookie)
if c := cm.store[k]; c != nil {
delete(cm.store, k)
fasthttp.ReleaseCookie(c)
}
cm.store[k] = cookie
})
}
func (cm *cookieManager) loadCookie(req *fasthttp.Request, url *url.URL) {
cm.locker.Lock()
for k, v := range cm.store {
if strings.Index(url.Host, util.ByteString(v.Domain())) < 0 {
continue
}
if strings.Index(url.Path, util.ByteString(v.Path())) != 0 {
continue
}
if (url.Scheme == "https" && v.Secure()) || !v.Secure() {
req.Header.SetCookie(k, util.ByteString(v.Value()))
}
if v.Expire().After(time.Now()) {
delete(cm.store, k)
fasthttp.ReleaseCookie(v)
}
}
cm.locker.Unlock()
}
var globalCookieManager = newCookieManager()
// FastHTTPClient is hprose fasthttp client
type FastHTTPClient struct {
rpc.BaseClient
*cookieManager
fasthttp.Client
Header fasthttp.RequestHeader
compression bool
keepAlive bool
limiter rpc.Limiter
}
// NewFastHTTPClient is the constructor of FastHTTPClient
func NewFastHTTPClient(uri ...string) (client *FastHTTPClient) {
client = new(FastHTTPClient)
client.InitBaseClient()
client.limiter.InitLimiter()
client.cookieManager = globalCookieManager
if rpc.DisableGlobalCookie {
client.cookieManager = newCookieManager()
}
client.compression = false
client.keepAlive = true
client.SetURIList(uri)
client.SendAndReceive = client.sendAndReceive
return
}
func newFastHTTPClient(uri ...string) rpc.Client {
return NewFastHTTPClient(uri...)
}
// SetURIList sets a list of server addresses
func (client *FastHTTPClient) SetURIList(uriList []string) {
if rpc.CheckAddresses(uriList, httpSchemes) == "https" {
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
}
client.BaseClient.SetURIList(uriList)
}
// TLSClientConfig returns the tls.Config in hprose client
func (client *FastHTTPClient) TLSClientConfig() *tls.Config {
return client.TLSConfig
}
// SetTLSClientConfig sets the tls.Config
func (client *FastHTTPClient) SetTLSClientConfig(config *tls.Config) {
client.TLSConfig = config
}
// MaxConcurrentRequests returns max concurrent request count
func (client *FastHTTPClient) MaxConcurrentRequests() int {
return client.limiter.MaxConcurrentRequests
}
// SetMaxConcurrentRequests sets max concurrent request count
func (client *FastHTTPClient) SetMaxConcurrentRequests(value int) {
client.limiter.MaxConcurrentRequests = value
}
// KeepAlive returns the keepalive status of hprose client
func (client *FastHTTPClient) KeepAlive() bool {
return client.keepAlive
}
// SetKeepAlive sets the keepalive status of hprose client
func (client *FastHTTPClient) SetKeepAlive(enable bool) {
client.keepAlive = enable
}
// Compression returns the compression status of hprose client
func (client *FastHTTPClient) Compression() bool {
return client.compression
}
// SetCompression sets the compression status of hprose client
func (client *FastHTTPClient) SetCompression(enable bool) {
client.compression = enable
}
func (client *FastHTTPClient) limit() {
client.limiter.L.Lock()
client.limiter.Limit()
client.limiter.L.Unlock()
}
func (client *FastHTTPClient) unlimit() {
client.limiter.L.Lock()
client.limiter.Unlimit()
client.limiter.L.Unlock()
}
func (client *FastHTTPClient) sendAndReceive(
data []byte, context *rpc.ClientContext) ([]byte, error) {
client.limit()
defer client.unlimit()
req := fasthttp.AcquireRequest()
client.Header.CopyTo(&req.Header)
header, ok := context.Get("httpHeader").(http.Header)
if ok && header != nil {
for key, values := range header {
for _, value := range values {
req.Header.Add(key, value)
}
}
}
req.Header.SetMethod("POST")
client.loadCookie(req, client.URL())
req.SetRequestURI(client.URI())
req.SetBody(data)
req.Header.SetContentLength(len(data))
req.Header.SetContentType("application/hprose")
if client.keepAlive {
req.Header.Set("Connection", "keep-alive")
} else {
req.Header.Set("Connection", "close")
}
if client.compression {
req.Header.Set("Accept-Encoding", "gzip")
}
resp := fasthttp.AcquireResponse()
err := client.Client.DoTimeout(req, resp, context.Timeout)
if err != nil {
data = nil
} else {
data = resp.Body()
client.saveCookie(resp)
}
header = make(http.Header)
resp.Header.VisitAll(func(key, value []byte) {
header.Add(util.ByteString(key), util.ByteString(value))
})
context.Set("httpHeader", header)
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
return data, err
}
func init() {
rpc.RegisterClientFactory("http", newFastHTTPClient)
rpc.RegisterClientFactory("https", newFastHTTPClient)
}