/
rest_client.go
executable file
·181 lines (154 loc) · 4.45 KB
/
rest_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
package rest
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"github.com/go-chassis/go-chassis/core/client"
"github.com/go-chassis/go-chassis/core/common"
"github.com/go-chassis/go-chassis/core/invocation"
"net"
"time"
)
const (
// Name is a constant of type string
Name = "rest"
// FailureTypePrefix is a constant of type string
FailureTypePrefix = "http_"
//DefaultTimeoutBySecond defines the default timeout for http connections
DefaultTimeoutBySecond = 60 * time.Second
//DefaultKeepAliveSecond defines the connection time
DefaultKeepAliveSecond = 60 * time.Second
//DefaultMaxConnsPerHost defines the maximum number of concurrent connections
DefaultMaxConnsPerHost = 512
//SchemaHTTP represents the http schema
SchemaHTTP = "http"
//SchemaHTTPS represents the https schema
SchemaHTTPS = "https"
)
var (
//ErrCanceled means Request is canceled by context management
ErrCanceled = errors.New("request cancelled")
//ErrInvalidReq invalid input
ErrInvalidReq = errors.New("rest consumer call arg is not *rest.Request type")
//ErrInvalidResp invalid input
ErrInvalidResp = errors.New("rest consumer response arg is not *rest.Response type")
)
//HTTPFailureTypeMap is a variable of type map
var HTTPFailureTypeMap = map[string]bool{
FailureTypePrefix + strconv.Itoa(http.StatusInternalServerError): true, //http_500
FailureTypePrefix + strconv.Itoa(http.StatusBadGateway): true, //http_502
FailureTypePrefix + strconv.Itoa(http.StatusServiceUnavailable): true, //http_503
FailureTypePrefix + strconv.Itoa(http.StatusGatewayTimeout): true, //http_504
FailureTypePrefix + strconv.Itoa(http.StatusTooManyRequests): true, //http_429
}
func init() {
client.InstallPlugin(Name, NewRestClient)
}
//NewRestClient is a function
func NewRestClient(opts client.Options) client.ProtocolClient {
if opts.Failure == nil || len(opts.Failure) == 0 {
opts.Failure = HTTPFailureTypeMap
} else {
tmpFailureMap := make(map[string]bool)
for k := range opts.Failure {
if HTTPFailureTypeMap[k] {
tmpFailureMap[k] = true
}
}
opts.Failure = tmpFailureMap
}
poolSize := DefaultMaxConnsPerHost
if opts.PoolSize != 0 {
poolSize = opts.PoolSize
}
tp := &http.Transport{
MaxIdleConns: poolSize,
MaxIdleConnsPerHost: poolSize,
DialContext: (&net.Dialer{
KeepAlive: DefaultKeepAliveSecond,
Timeout: DefaultTimeoutBySecond,
}).DialContext}
if opts.TLSConfig != nil {
tp.TLSClientConfig = opts.TLSConfig
}
rc := &Client{
opts: opts,
c: &http.Client{
Transport: tp,
},
}
return rc
}
//Init is a method
// If a request fails, we generate an error.
func (c *Client) failure2Error(e error, r *Response, addr string) error {
if e != nil {
return e
}
if r == nil {
return nil
}
codeStr := strconv.Itoa(r.GetStatusCode())
// The Failure map defines whether or not a request fail.
if c.opts.Failure["http_"+codeStr] {
return fmt.Errorf("http error status %d, server addr: %s", r.GetStatusCode(), addr)
}
return nil
}
func invocation2HttpRequest(inv *invocation.Invocation) (*Request, error) {
reqSend, ok := inv.Args.(*Request)
if !ok {
return nil, ErrInvalidReq
}
m := common.FromContext(inv.Ctx)
for k, v := range m {
reqSend.SetHeader(k, v)
}
return reqSend, nil
}
//Call is a method which uses client struct object
func (c *Client) Call(ctx context.Context, addr string, inv *invocation.Invocation, rsp interface{}) error {
var err error
reqSend, err := invocation2HttpRequest(inv)
if err != nil {
return err
}
resp, ok := rsp.(*Response)
if !ok {
return ErrInvalidResp
}
c.contextToHeader(ctx, reqSend)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if c.opts.TLSConfig != nil {
reqSend.Req.URL.Scheme = SchemaHTTPS
} else {
reqSend.Req.URL.Scheme = SchemaHTTP
}
if addr != "" {
reqSend.Req.URL.Host = addr
}
//increase the max connection per host to prevent error "no free connection available" error while sending more requests.
c.c.Transport.(*http.Transport).MaxIdleConnsPerHost = 512 * 20
errChan := make(chan error, 1)
go func() { errChan <- c.Do(reqSend, resp) }()
select {
case <-ctx.Done():
err = ErrCanceled
case err = <-errChan:
}
return c.failure2Error(err, resp, addr)
}
func (c *Client) String() string {
return "rest_client"
}
func (c *Client) contextToHeader(ctx context.Context, req *Request) {
for k, v := range common.FromContext(ctx) {
req.Req.Header.Set(k, v)
}
if len(req.GetContentType()) == 0 {
req.SetContentType(common.JSON)
}
}