/
predict_client.go
417 lines (363 loc) · 10.7 KB
/
predict_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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package eas
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"sync/atomic"
"time"
)
const (
// Default endpoint is the gateway mode
EndpointTypeDefault = "DEFAULT"
// Vipserver endpoint is only used for services which registered
// vipsever domains in alibaba internal clusters
EndpointTypeVipserver = "VIPSERVER"
// Direct endpoint is used for direct accessing to the services' instances
// both inside a eas service and in user's client ecs
EndpointTypeDirect = "DIRECT"
)
const (
CompressTypeGzip = "Gzip"
CompressTypeZlib = "Zlib"
)
const (
ErrorCodeServiceDiscovery = 510
ErrorCodeCreateRequest = 511
ErrorCodePerformRequest = 512
ErrorCodeReadResponse = 513
)
// PredictError is a custom err type
type PredictError struct {
Code int
Message string
RequestURL string
}
// Error for error interface
func (err *PredictError) Error() string {
return fmt.Sprintf("Url: [%v] Code: [%d], Message: [%s]", err.RequestURL, err.Code, err.Message)
}
// NewPredictError constructs an error
func NewPredictError(code int, url string, msg string) *PredictError {
return &PredictError{
Code: code,
Message: msg,
RequestURL: url,
}
}
// PredictClient for accessing prediction service by creating a fixed size connection pool
// to perform the request through established persistent connections.
type PredictClient struct {
retryCount int
maxConnectionCount int
token string
headers map[string]string
host string
endpoint Endpoint
endpointType string
endpointName string
serviceName string
compressType string
stop int32
client http.Client
}
type HttpOption func(client *http.Client)
func DisableHttpRedirect() func(client *http.Client) {
return func(client *http.Client) {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
return
}
}
// NewPredictClient returns an instance of PredictClient
func NewPredictClient(endpointName string, serviceName string, options ...HttpOption) *PredictClient {
client := &PredictClient{
endpointName: endpointName,
serviceName: serviceName,
retryCount: 5,
stop: 0,
headers: map[string]string{},
client: http.Client{
Timeout: 5000 * time.Millisecond,
Transport: &http.Transport{
MaxConnsPerHost: 100,
},
},
}
for _, option := range options {
option(&client.client)
}
return client
}
// Init initializes the predict client to create and enable endpoint discovery
func (p *PredictClient) Init() error {
switch p.endpointType {
case "":
p.endpoint = newGatewayEndpoint(p.endpointName)
case EndpointTypeDefault:
p.endpoint = newGatewayEndpoint(p.endpointName)
case EndpointTypeVipserver:
p.endpoint = newVipServerEndpoint(p.endpointName)
go p.syncHandler()
case EndpointTypeDirect:
p.endpoint = newCacheServerEndpoint(p.endpointName, p.serviceName)
go p.syncHandler()
default:
return NewPredictError(http.StatusBadRequest, "", "Unsupported endpoint type: "+p.endpointType)
}
return nil
}
// Shutdown after called this client instance should not be used again
func (p *PredictClient) Shutdown() {
atomic.StoreInt32(&(p.stop), 1)
}
// syncHandler synchronizes the services's endpoints from the upstream discovery server periodically
func (p *PredictClient) syncHandler() {
p.endpoint.Sync()
for {
select {
// Sync endpoints from upstream every 3 seconds
case <-time.NewTimer(time.Second * 3).C:
if 1 == atomic.LoadInt32(&(p.stop)) {
break
}
p.endpoint.Sync()
}
}
}
// SetEndpoint sets service's endpoint for client
func (p *PredictClient) SetEndpoint(endpointName string) {
p.endpointName = endpointName
}
// SetEndpointType sets endpoint type for client
func (p *PredictClient) SetEndpointType(endpointType string) {
p.endpointType = endpointType
}
// SetToken function sets service's access token for client
func (p *PredictClient) SetToken(token string) {
p.token = token
}
func (p *PredictClient) AddHeader(headerName, headerValue string) {
p.headers[headerName] = headerValue
}
func (p *PredictClient) SetHost(host string) {
p.host = host
}
// SetRetryCount sets max retry count for client
func (p *PredictClient) SetRetryCount(cnt int) {
p.retryCount = cnt
}
// SetHttpTransport sets http transport argument for go http client
func (p *PredictClient) SetHttpTransport(transport *http.Transport) {
p.client.Transport = transport
}
// SetTimeout set the request timeout for client, 5000ms by default
func (p *PredictClient) SetTimeout(timeout int) {
p.client.Timeout = time.Duration(timeout) * time.Millisecond
}
// SetServiceName sets target service name for client
func (p *PredictClient) SetServiceName(serviceName string) {
p.serviceName = serviceName
}
// SetCompressType sets compressor type for client
func (p *PredictClient) SetCompressType(compressType string) {
p.compressType = compressType
}
func (p *PredictClient) tryNext(host string) string {
return p.endpoint.TryNext(host)
}
func (p *PredictClient) createUrl(host string) string {
if len(p.serviceName) != 0 {
if p.serviceName[len(p.serviceName)-1] == '/' {
p.serviceName = p.serviceName[:len(p.serviceName)-1]
}
}
return fmt.Sprintf("http://%s/api/predict/%s", host, p.serviceName)
}
// generateSignature computes the signature header using the access token with hmac sha1 algorithm.
// returns the headers including signature header for authentication.
func (p *PredictClient) generateSignature(requestData []byte) map[string]string {
canonicalizedResource := fmt.Sprintf("/api/predict/%s", p.serviceName)
contentMd5 := md5sum(requestData)
contentType := "application/octet-stream"
currentTime := time.Now().Format("Mon, 02 Jan 2006 15:04:05 GMT")
verb := "POST"
auth := fmt.Sprintf("%s\n%s\n%s\n%s\n%s", verb, contentMd5, contentType, currentTime, canonicalizedResource)
authorization := fmt.Sprintf("EAS %s", hmacSha256(auth, p.token))
return map[string]string{
"Content-MD5": contentMd5,
"Date": currentTime,
"Content-Type": contentType,
"Content-Length": fmt.Sprintf("%d", len(requestData)),
"Authorization": authorization,
}
}
// BytesPredict send the raw request data in byte array through http connections,
// retry the request automatically when an error occurs
func (p *PredictClient) BytesPredict(requestData []byte) ([]byte, error) {
var err error
if len(p.compressType) > 0 {
requestData, err = compress(requestData, p.compressType)
if err != nil {
return nil, NewPredictError(http.StatusInternalServerError, "", err.Error())
}
}
host := p.tryNext("")
headers := p.generateSignature(requestData)
for i := 0; i <= p.retryCount; i++ {
if i != 0 {
host = p.tryNext(host)
}
if len(host) == 0 {
return nil, NewPredictError(ErrorCodeServiceDiscovery, host,
fmt.Sprintf("No available endpoint found for service: %v", p.serviceName))
}
url := p.createUrl(host)
req, err := http.NewRequest("POST", url, bytes.NewReader(requestData))
if err != nil {
// retry
if i != p.retryCount {
continue
}
return nil, NewPredictError(ErrorCodeCreateRequest, url, err.Error())
}
if p.token != "" {
for headerName, headerValue := range headers {
req.Header.Set(headerName, headerValue)
}
}
for headerName, headerValue := range p.headers {
req.Header.Set(headerName, headerValue)
}
if p.host != "" {
req.Host = p.host
}
resp, err := p.client.Do(req)
if err != nil {
// retry
if i != p.retryCount {
continue
}
return nil, NewPredictError(ErrorCodePerformRequest, url, err.Error())
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// retry
if i != p.retryCount {
continue
}
return nil, NewPredictError(ErrorCodeReadResponse, url, err.Error())
}
resp.Body.Close()
if resp.StatusCode != 200 {
// retry
if i != p.retryCount {
continue
}
return body, NewPredictError(resp.StatusCode, url, string(body))
}
return body, nil
}
return []byte{}, nil
}
type Request interface {
ToString() (string, error)
}
type Response interface {
unmarshal(body []byte) error
}
// Predict for request
func (p *PredictClient) Predict(request Request) (Response, error) {
if rawRequest, ok := request.(RawRequest); ok {
return p.RawRequest(rawRequest)
}
req, err2 := request.ToString()
if err2 != nil {
return nil, err2
}
body, err := p.BytesPredict([]byte(req))
if err != nil {
return nil, err
}
switch request.(type) {
case TFRequest:
resp := TFResponse{}
unmarshalErr := resp.unmarshal(body)
return &resp, unmarshalErr
case TorchRequest:
resp := TorchResponse{}
unmarshalErr := resp.unmarshal(body)
return &resp, unmarshalErr
default:
return nil, NewPredictError(-1, "", "Unknown request type, currently support StringRequest, TFRequest and TorchRequest.")
}
}
// StringPredict function send input data and return predicted result
func (p *PredictClient) StringPredict(str string) (string, error) {
body, err := p.BytesPredict([]byte(str))
return string(body), err
}
// TorchPredict function send input data and return PyTorch predicted result
func (p *PredictClient) TorchPredict(request TorchRequest) (*TorchResponse, error) {
resp, err := p.Predict(request)
if err != nil {
return nil, err
}
return resp.(*TorchResponse), err
}
// TFPredict function send input data and return TensorFlow predicted result
func (p *PredictClient) TFPredict(request TFRequest) (*TFResponse, error) {
resp, err := p.Predict(request)
if err != nil {
return nil, err
}
return resp.(*TFResponse), err
}
// RawRequest sends the raw request in raw http Request, retry the request automatically when an error occurs
func (p *PredictClient) RawRequest(req RawRequest) (*RawResponse, error) {
host := p.tryNext("")
var predictError *PredictError
for i := 0; i <= p.retryCount; i++ {
if i != 0 {
host = p.tryNext(host)
}
if len(host) == 0 {
predictError = NewPredictError(ErrorCodeServiceDiscovery, host,
fmt.Sprintf("No available endpoint found for service: %v", p.serviceName))
return nil, predictError
}
req.URL.Host = host
url := p.createUrl(host)
resp, err := p.client.Do(&req.Request)
if err != nil {
predictError = NewPredictError(ErrorCodePerformRequest, url, err.Error())
// retry
if i != p.retryCount {
continue
}
return nil, predictError
}
if resp.StatusCode != 200 {
predictError = NewPredictError(resp.StatusCode, url, "")
// retry
if i != p.retryCount {
continue
}
return nil, predictError
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
predictError = NewPredictError(ErrorCodeReadResponse, url, err.Error())
// retry
if i != p.retryCount {
continue
}
return nil, err
}
resp.Body.Close()
rawResp := RawResponse{data: body}
return &rawResp, nil
}
return nil, predictError
}