-
Notifications
You must be signed in to change notification settings - Fork 100
/
outbound.go
551 lines (485 loc) · 17.3 KB
/
outbound.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package http
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
opentracinglog "github.com/opentracing/opentracing-go/log"
"go.uber.org/yarpc"
"go.uber.org/yarpc/api/peer"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/internal/introspection"
intyarpcerrors "go.uber.org/yarpc/internal/yarpcerrors"
peerchooser "go.uber.org/yarpc/peer"
"go.uber.org/yarpc/peer/hostport"
"go.uber.org/yarpc/pkg/lifecycle"
"go.uber.org/yarpc/yarpcerrors"
)
// this ensures the HTTP outbound implements both transport.Outbound interfaces
var (
_ transport.UnaryOutbound = (*Outbound)(nil)
_ transport.OnewayOutbound = (*Outbound)(nil)
_ introspection.IntrospectableOutbound = (*Outbound)(nil)
)
var defaultURLTemplate, _ = url.Parse("http://localhost")
// OutboundOption customizes an HTTP Outbound.
type OutboundOption func(*Outbound)
func (OutboundOption) httpOption() {}
// URLTemplate specifies the URL this outbound makes requests to. For
// peer.Chooser-based outbounds, the peer (host:port) spection of the URL may
// vary from call to call but the rest will remain unchanged. For single-peer
// outbounds, the URL will be used as-is.
func URLTemplate(template string) OutboundOption {
return func(o *Outbound) {
o.setURLTemplate(template)
}
}
// AddHeader specifies that an HTTP outbound should always include the given
// header in outgoung requests.
//
// httpTransport.NewOutbound(chooser, http.AddHeader("X-Token", "TOKEN"))
//
// Note that headers starting with "Rpc-" are reserved by YARPC. This function
// will panic if the header starts with "Rpc-".
func AddHeader(key, value string) OutboundOption {
if strings.HasPrefix(strings.ToLower(key), "rpc-") {
panic(fmt.Errorf(
"invalid header name %q: "+
`headers starting with "Rpc-" are reserved by YARPC`, key))
}
return func(o *Outbound) {
if o.headers == nil {
o.headers = make(http.Header)
}
o.headers.Add(key, value)
}
}
// NewOutbound builds an HTTP outbound that sends requests to peers supplied
// by the given peer.Chooser. The URL template for used for the different
// peers may be customized using the URLTemplate option.
//
// The peer chooser and outbound must share the same transport, in this case
// the HTTP transport.
// The peer chooser must use the transport's RetainPeer to obtain peer
// instances and return those peers to the outbound when it calls Choose.
// The concrete peer type is private and intrinsic to the HTTP transport.
func (t *Transport) NewOutbound(chooser peer.Chooser, opts ...OutboundOption) *Outbound {
o := &Outbound{
once: lifecycle.NewOnce(),
chooser: chooser,
urlTemplate: defaultURLTemplate,
tracer: t.tracer,
transport: t,
bothResponseError: true,
}
for _, opt := range opts {
opt(o)
}
return o
}
// NewOutbound builds an HTTP outbound that sends requests to peers supplied
// by the given peer.Chooser. The URL template for used for the different
// peers may be customized using the URLTemplate option.
//
// The peer chooser and outbound must share the same transport, in this case
// the HTTP transport.
// The peer chooser must use the transport's RetainPeer to obtain peer
// instances and return those peers to the outbound when it calls Choose.
// The concrete peer type is private and intrinsic to the HTTP transport.
func NewOutbound(chooser peer.Chooser, opts ...OutboundOption) *Outbound {
return NewTransport().NewOutbound(chooser, opts...)
}
// NewSingleOutbound builds an outbound that sends YARPC requests over HTTP
// to the specified URL.
//
// The URLTemplate option has no effect in this form.
func (t *Transport) NewSingleOutbound(uri string, opts ...OutboundOption) *Outbound {
parsedURL, err := url.Parse(uri)
if err != nil {
panic(err.Error())
}
chooser := peerchooser.NewSingle(hostport.PeerIdentifier(parsedURL.Host), t)
o := t.NewOutbound(chooser)
for _, opt := range opts {
opt(o)
}
o.setURLTemplate(uri)
return o
}
// Outbound sends YARPC requests over HTTP. It may be constructed using the
// NewOutbound function or the NewOutbound or NewSingleOutbound methods on the
// HTTP Transport. It is recommended that services use a single HTTP transport
// to construct all HTTP outbounds, ensuring efficient sharing of resources
// across the different outbounds.
type Outbound struct {
chooser peer.Chooser
urlTemplate *url.URL
tracer opentracing.Tracer
transport *Transport
// Headers to add to all outgoing requests.
headers http.Header
once *lifecycle.Once
// should only be false in testing
bothResponseError bool
}
// setURLTemplate configures an alternate URL template.
// The host:port portion of the URL template gets replaced by the chosen peer's
// identifier for each outbound request.
func (o *Outbound) setURLTemplate(URL string) {
parsedURL, err := url.Parse(URL)
if err != nil {
log.Fatalf("failed to configure HTTP outbound: invalid URL template %q: %s", URL, err)
}
o.urlTemplate = parsedURL
}
// Transports returns the outbound's HTTP transport.
func (o *Outbound) Transports() []transport.Transport {
return []transport.Transport{o.transport}
}
// Chooser returns the outbound's peer chooser.
func (o *Outbound) Chooser() peer.Chooser {
return o.chooser
}
// Start the HTTP outbound
func (o *Outbound) Start() error {
return o.once.Start(o.chooser.Start)
}
// Stop the HTTP outbound
func (o *Outbound) Stop() error {
return o.once.Stop(o.chooser.Stop)
}
// IsRunning returns whether the Outbound is running.
func (o *Outbound) IsRunning() bool {
return o.once.IsRunning()
}
// Call makes a HTTP request
func (o *Outbound) Call(ctx context.Context, treq *transport.Request) (*transport.Response, error) {
if treq == nil {
return nil, yarpcerrors.InvalidArgumentErrorf("request for http unary outbound was nil")
}
return o.call(ctx, treq)
}
// CallOneway makes a oneway request
func (o *Outbound) CallOneway(ctx context.Context, treq *transport.Request) (transport.Ack, error) {
if treq == nil {
return nil, yarpcerrors.InvalidArgumentErrorf("request for http oneway outbound was nil")
}
_, err := o.call(ctx, treq)
if err != nil {
return nil, err
}
return time.Now(), nil
}
func (o *Outbound) call(ctx context.Context, treq *transport.Request) (*transport.Response, error) {
start := time.Now()
deadline, ok := ctx.Deadline()
if !ok {
return nil, yarpcerrors.Newf(yarpcerrors.CodeInvalidArgument, "missing context deadline")
}
ttl := deadline.Sub(start)
hreq, err := o.createRequest(treq)
if err != nil {
return nil, err
}
hreq.Header = applicationHeaders.ToHTTPHeaders(treq.Headers, nil)
ctx, hreq, span, err := o.withOpentracingSpan(ctx, hreq, treq, start)
if err != nil {
return nil, err
}
defer span.Finish()
hreq = o.withCoreHeaders(hreq, treq, ttl)
hreq = hreq.WithContext(ctx)
response, err := o.roundTrip(hreq, treq, start)
if err != nil {
span.SetTag("error", true)
span.LogFields(opentracinglog.String("event", err.Error()))
return nil, err
}
span.SetTag("http.status_code", response.StatusCode)
// Service name match validation, return yarpcerrors.CodeInternal error if not match
if match, resSvcName := checkServiceMatch(treq.Service, response.Header); !match {
return nil, transport.UpdateSpanWithErr(span,
yarpcerrors.InternalErrorf("service name sent from the request "+
"does not match the service name received in the response, sent %q, got: %q", treq.Service, resSvcName))
}
tres := &transport.Response{
Headers: applicationHeaders.FromHTTPHeaders(response.Header, transport.NewHeaders()),
Body: response.Body,
ApplicationError: response.Header.Get(ApplicationStatusHeader) == ApplicationErrorStatus,
}
bothResponseError := response.Header.Get(BothResponseErrorHeader) == AcceptTrue
if bothResponseError && o.bothResponseError {
if response.StatusCode >= 300 {
return tres, getYARPCErrorFromResponse(response, true)
}
return tres, nil
}
if response.StatusCode >= 200 && response.StatusCode < 300 {
return tres, nil
}
return nil, getYARPCErrorFromResponse(response, false)
}
func (o *Outbound) getPeerForRequest(ctx context.Context, treq *transport.Request) (*httpPeer, func(error), error) {
p, onFinish, err := o.chooser.Choose(ctx, treq)
if err != nil {
return nil, nil, err
}
hpPeer, ok := p.(*httpPeer)
if !ok {
return nil, nil, peer.ErrInvalidPeerConversion{
Peer: p,
ExpectedType: "*httpPeer",
}
}
return hpPeer, onFinish, nil
}
func (o *Outbound) createRequest(treq *transport.Request) (*http.Request, error) {
newURL := *o.urlTemplate
return http.NewRequest("POST", newURL.String(), treq.Body)
}
func (o *Outbound) withOpentracingSpan(ctx context.Context, req *http.Request, treq *transport.Request, start time.Time) (context.Context, *http.Request, opentracing.Span, error) {
// Apply HTTP Context headers for tracing and baggage carried by tracing.
tracer := o.tracer
var parent opentracing.SpanContext // ok to be nil
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
parent = parentSpan.Context()
}
tags := opentracing.Tags{
"rpc.caller": treq.Caller,
"rpc.service": treq.Service,
"rpc.encoding": treq.Encoding,
"rpc.transport": "http",
}
for k, v := range yarpc.OpentracingTags {
tags[k] = v
}
span := tracer.StartSpan(
treq.Procedure,
opentracing.StartTime(start),
opentracing.ChildOf(parent),
tags,
)
ext.PeerService.Set(span, treq.Service)
ext.SpanKindRPCClient.Set(span)
ext.HTTPUrl.Set(span, req.URL.String())
ctx = opentracing.ContextWithSpan(ctx, span)
err := tracer.Inject(
span.Context(),
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(req.Header),
)
return ctx, req, span, err
}
func (o *Outbound) withCoreHeaders(req *http.Request, treq *transport.Request, ttl time.Duration) *http.Request {
// Add default headers to all requests.
for k, vs := range o.headers {
for _, v := range vs {
req.Header.Add(k, v)
}
}
req.Header.Set(CallerHeader, treq.Caller)
req.Header.Set(ServiceHeader, treq.Service)
req.Header.Set(ProcedureHeader, treq.Procedure)
if ttl != 0 {
req.Header.Set(TTLMSHeader, fmt.Sprintf("%d", ttl/time.Millisecond))
}
if treq.ShardKey != "" {
req.Header.Set(ShardKeyHeader, treq.ShardKey)
}
if treq.RoutingKey != "" {
req.Header.Set(RoutingKeyHeader, treq.RoutingKey)
}
if treq.RoutingDelegate != "" {
req.Header.Set(RoutingDelegateHeader, treq.RoutingDelegate)
}
encoding := string(treq.Encoding)
if encoding != "" {
req.Header.Set(EncodingHeader, encoding)
}
if o.bothResponseError {
req.Header.Set(AcceptsBothResponseErrorHeader, AcceptTrue)
}
return req
}
func getYARPCErrorFromResponse(response *http.Response, bothResponseError bool) error {
var contents string
if bothResponseError {
contents = response.Header.Get(ErrorMessageHeader)
} else {
contentsBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return yarpcerrors.Newf(yarpcerrors.CodeInternal, err.Error())
}
contents = string(contentsBytes)
if err := response.Body.Close(); err != nil {
return yarpcerrors.Newf(yarpcerrors.CodeInternal, err.Error())
}
}
// use the status code if we can't get a code from the headers
code := statusCodeToBestCode(response.StatusCode)
if errorCodeText := response.Header.Get(ErrorCodeHeader); errorCodeText != "" {
var errorCode yarpcerrors.Code
// TODO: what to do with error?
if err := errorCode.UnmarshalText([]byte(errorCodeText)); err == nil {
code = errorCode
}
}
return intyarpcerrors.NewWithNamef(
code,
response.Header.Get(ErrorNameHeader),
strings.TrimSuffix(contents, "\n"),
)
}
// Only does verification if there is a response header
func checkServiceMatch(reqSvcName string, resHeaders http.Header) (bool, string) {
serviceName := resHeaders.Get(ServiceHeader)
return serviceName == "" || serviceName == reqSvcName, serviceName
}
// RoundTrip implements the http.RoundTripper interface, making a YARPC HTTP outbound suitable as a
// Transport when constructing an HTTP Client. An HTTP client is suitable only for relative paths to
// a single outbound service. The HTTP outbound overrides the host:port portion of the URL of the
// provided request.
//
// Sample usage:
//
// client := http.Client{Transport: outbound}
//
// Thereafter use the Golang standard library HTTP to send requests with this client.
//
// ctx, cancel := context.WithTimeout(context.Background(), time.Second)
// defer cancel()
// req, err := http.NewRequest("GET", "http://example.com/", nil /* body */)
// req = req.WithContext(ctx)
// res, err := client.Do(req)
//
// All requests must have a deadline on the context.
// The peer chooser for raw HTTP requests will receive a YARPC transport.Request with no body.
//
// OpenTracing information must be added manually, before this call, to support context propagation.
func (o *Outbound) RoundTrip(hreq *http.Request) (*http.Response, error) {
return o.roundTrip(hreq, nil /* treq */, time.Now())
}
func (o *Outbound) roundTrip(hreq *http.Request, treq *transport.Request, start time.Time) (*http.Response, error) {
ctx := hreq.Context()
deadline, ok := ctx.Deadline()
if !ok {
return nil, yarpcerrors.Newf(
yarpcerrors.CodeInvalidArgument,
"missing context deadline")
}
ttl := deadline.Sub(start)
// When sending requests through the RoundTrip method, we construct the
// transport request from the HTTP headers as if it were an inbound
// request.
// The API for setting transport metadata for an outbound request when
// using the go stdlib HTTP client is to use headers as the YAPRC HTTP
// transport header conventions.
if treq == nil {
treq = &transport.Request{
Caller: hreq.Header.Get(CallerHeader),
Service: hreq.Header.Get(ServiceHeader),
Encoding: transport.Encoding(hreq.Header.Get(EncodingHeader)),
Procedure: hreq.Header.Get(ProcedureHeader),
ShardKey: hreq.Header.Get(ShardKeyHeader),
RoutingKey: hreq.Header.Get(RoutingKeyHeader),
RoutingDelegate: hreq.Header.Get(RoutingDelegateHeader),
Headers: applicationHeaders.FromHTTPHeaders(hreq.Header, transport.Headers{}),
}
}
if err := o.once.WaitUntilRunning(ctx); err != nil {
return nil, intyarpcerrors.AnnotateWithInfo(
yarpcerrors.FromError(err),
"error waiting for HTTP outbound to start for service: %s",
treq.Service)
}
p, onFinish, err := o.getPeerForRequest(ctx, treq)
if err != nil {
return nil, err
}
hres, err := o.doWithPeer(ctx, hreq, treq, start, ttl, p)
// Call the onFinish method before returning (with the error from call with peer)
onFinish(err)
return hres, err
}
func (o *Outbound) doWithPeer(
ctx context.Context,
hreq *http.Request,
treq *transport.Request,
start time.Time,
ttl time.Duration,
p *httpPeer,
) (*http.Response, error) {
hreq.URL.Host = p.HostPort()
response, err := o.transport.client.Do(hreq.WithContext(ctx))
if err != nil {
// Workaround borrowed from ctxhttp until
// https://github.com/golang/go/issues/17711 is resolved.
select {
case <-ctx.Done():
err = ctx.Err()
default:
}
if err == context.DeadlineExceeded {
// Note that the connection experienced a time out, which may
// indicate that the connection is half-open, that the destination
// died without sending a TCP FIN packet.
p.OnSuspect()
end := time.Now()
return nil, yarpcerrors.Newf(
yarpcerrors.CodeDeadlineExceeded,
"client timeout for procedure %q of service %q after %v",
treq.Procedure, treq.Service, end.Sub(start))
}
// Note that the connection may have been lost so the peer connection
// maintenance loop resumes probing for availability.
p.OnDisconnected()
return nil, yarpcerrors.Newf(yarpcerrors.CodeUnknown, "unknown error from http client: %s", err.Error())
}
return response, nil
}
// Introspect returns basic status about this outbound.
func (o *Outbound) Introspect() introspection.OutboundStatus {
state := "Stopped"
if o.IsRunning() {
state = "Running"
}
var chooser introspection.ChooserStatus
if i, ok := o.chooser.(introspection.IntrospectableChooser); ok {
chooser = i.Introspect()
} else {
chooser = introspection.ChooserStatus{
Name: "Introspection not available",
}
}
return introspection.OutboundStatus{
Transport: "http",
Endpoint: o.urlTemplate.String(),
State: state,
Chooser: chooser,
}
}