forked from yarpc/yarpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outbound.go
222 lines (194 loc) · 6.74 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
// Copyright (c) 2017 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 tchannel
import (
"context"
"github.com/uber/tchannel-go"
"go.uber.org/yarpc/api/peer"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/internal/introspection"
peerchooser "go.uber.org/yarpc/peer"
"go.uber.org/yarpc/peer/hostport"
"go.uber.org/yarpc/pkg/errors"
"go.uber.org/yarpc/pkg/lifecycle"
"go.uber.org/yarpc/yarpcerrors"
)
var (
errDoNotUseContextWithHeaders = yarpcerrors.Newf(yarpcerrors.CodeInvalidArgument, "tchannel.ContextWithHeaders is not compatible with YARPC, use yarpc.CallOption instead")
_ transport.UnaryOutbound = (*Outbound)(nil)
_ introspection.IntrospectableOutbound = (*Outbound)(nil)
)
// Outbound sends YARPC requests over TChannel.
// It may be constructed using the NewOutbound or NewSingleOutbound methods on
// the TChannel Transport.
type Outbound struct {
transport *Transport
chooser peer.Chooser
once *lifecycle.Once
}
// NewOutbound builds a new TChannel outbound that selects a peer for each
// request using the given peer chooser.
func (t *Transport) NewOutbound(chooser peer.Chooser) *Outbound {
return &Outbound{
once: lifecycle.NewOnce(),
transport: t,
chooser: chooser,
}
}
// NewSingleOutbound builds a new TChannel outbound always using the peer with
// the given address.
func (t *Transport) NewSingleOutbound(addr string) *Outbound {
chooser := peerchooser.NewSingle(hostport.PeerIdentifier(addr), t)
return t.NewOutbound(chooser)
}
// Chooser returns the outbound's peer chooser.
func (o *Outbound) Chooser() peer.Chooser {
return o.chooser
}
// Call sends an RPC over this TChannel outbound.
func (o *Outbound) Call(ctx context.Context, req *transport.Request) (*transport.Response, error) {
if err := o.transport.once.WaitUntilRunning(ctx); err != nil {
return nil, err
}
if _, ok := ctx.(tchannel.ContextWithHeaders); ok {
return nil, errDoNotUseContextWithHeaders
}
root := o.transport.ch.RootPeers()
p, onFinish, err := o.getPeerForRequest(ctx, req)
if err != nil {
return nil, err
}
tp := root.GetOrAdd(p.HostPort())
res, err := o.callWithPeer(ctx, req, tp)
onFinish(err)
return res, err
}
// callWithPeer sends a request with the chosen peer.
func (o *Outbound) callWithPeer(ctx context.Context, req *transport.Request, peer *tchannel.Peer) (*transport.Response, error) {
// NB(abg): Under the current API, the local service's name is required
// twice: once when constructing the TChannel and then again when
// constructing the RPC.
var call *tchannel.OutboundCall
var err error
format := tchannel.Format(req.Encoding)
callOptions := tchannel.CallOptions{
Format: format,
ShardKey: req.ShardKey,
RoutingKey: req.RoutingKey,
RoutingDelegate: req.RoutingDelegate,
}
// If the hostport is given, we use the BeginCall on the channel
// instead of the subchannel.
call, err = peer.BeginCall(
// TODO(abg): Set TimeoutPerAttempt in the context's retry options if
// TTL is set.
// (kris): Consider instead moving TimeoutPerAttempt to an outer
// layer, just clamp the context on outbound call.
ctx,
req.Service,
req.Procedure,
&callOptions,
)
if err != nil {
return nil, err
}
// Inject tracing system baggage
reqHeaders := tchannel.InjectOutboundSpan(call.Response(), req.Headers.Items())
if err := writeRequestHeaders(ctx, format, reqHeaders, call.Arg2Writer); err != nil {
// TODO(abg): This will wrap IO errors while writing headers as encode
// errors. We should fix that.
return nil, errors.RequestHeadersEncodeError(req, err)
}
if err := writeBody(req.Body, call); err != nil {
return nil, err
}
res := call.Response()
headers, err := readHeaders(format, res.Arg2Reader)
if err != nil {
if err, ok := err.(tchannel.SystemError); ok {
return nil, fromSystemError(err)
}
// TODO(abg): This will wrap IO errors while reading headers as decode
// errors. We should fix that.
return nil, errors.ResponseHeadersDecodeError(req, err)
}
resBody, err := res.Arg3Reader()
if err != nil {
if err, ok := err.(tchannel.SystemError); ok {
return nil, fromSystemError(err)
}
return nil, err
}
return &transport.Response{
Headers: headers,
Body: resBody,
ApplicationError: res.ApplicationError(),
}, getResponseErrorAndDeleteHeaderKeys(headers)
}
func (o *Outbound) getPeerForRequest(ctx context.Context, treq *transport.Request) (*tchannelPeer, func(error), error) {
p, onFinish, err := o.chooser.Choose(ctx, treq)
if err != nil {
return nil, nil, err
}
tp, ok := p.(*tchannelPeer)
if !ok {
return nil, nil, peer.ErrInvalidPeerConversion{
Peer: p,
ExpectedType: "*tchannelPeer",
}
}
return tp, onFinish, nil
}
// Transports returns the underlying TChannel Transport for this outbound.
func (o *Outbound) Transports() []transport.Transport {
return []transport.Transport{o.transport}
}
// Start starts the TChannel outbound.
func (o *Outbound) Start() error {
return o.once.Start(o.chooser.Start)
}
// Stop stops the TChannel outbound.
func (o *Outbound) Stop() error {
return o.once.Stop(o.chooser.Stop)
}
// IsRunning returns whether the ChannelOutbound is running.
func (o *Outbound) IsRunning() bool {
return o.once.IsRunning()
}
// 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: "tchannel",
State: state,
Chooser: chooser,
}
}