forked from uber/tchannel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inbound.go
421 lines (364 loc) · 14 KB
/
inbound.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
// Copyright (c) 2015 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 (
"errors"
"fmt"
"time"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"golang.org/x/net/context"
)
var errInboundRequestAlreadyActive = errors.New("inbound request is already active; possible duplicate client id")
// handleCallReq handles an incoming call request, registering a message
// exchange to receive further fragments for that call, and dispatching it in
// another goroutine
func (c *Connection) handleCallReq(frame *Frame) bool {
now := c.timeNow()
switch state := c.readState(); state {
case connectionActive:
break
case connectionStartClose, connectionInboundClosed, connectionClosed:
c.SendSystemError(frame.Header.ID, callReqSpan(frame), ErrChannelClosed)
return true
default:
panic(fmt.Errorf("unknown connection state for call req: %v", state))
}
callReq := new(callReq)
callReq.id = frame.Header.ID
initialFragment, err := parseInboundFragment(c.opts.FramePool, frame, callReq)
if err != nil {
// TODO(mmihic): Probably want to treat this as a protocol error
c.log.WithFields(
LogField{"header", frame.Header},
ErrField(err),
).Error("Couldn't decode initial fragment.")
return true
}
call := new(InboundCall)
call.conn = c
ctx, cancel := newIncomingContext(c.baseContext, call, callReq.TimeToLive)
mex, err := c.inbound.newExchange(ctx, c.opts.FramePool, callReq.messageType(), frame.Header.ID, mexChannelBufferSize)
if err != nil {
if err == errDuplicateMex {
err = errInboundRequestAlreadyActive
}
c.log.WithFields(LogField{"header", frame.Header}).Error("Couldn't register exchange.")
c.protocolError(frame.Header.ID, errInboundRequestAlreadyActive)
return true
}
// Close may have been called between the time we checked the state and us creating the exchange.
if c.readState() != connectionActive {
mex.shutdown()
return true
}
response := new(InboundCallResponse)
response.call = call
response.calledAt = now
response.timeNow = c.timeNow
response.span = c.extractInboundSpan(callReq)
if response.span != nil {
mex.ctx = opentracing.ContextWithSpan(mex.ctx, response.span)
}
response.mex = mex
response.conn = c
response.cancel = cancel
response.log = c.log.WithFields(LogField{"In-Response", callReq.ID()})
response.contents = newFragmentingWriter(response.log, response, initialFragment.checksumType.New())
response.headers = transportHeaders{}
response.messageForFragment = func(initial bool) message {
if initial {
callRes := new(callRes)
callRes.Headers = response.headers
callRes.ResponseCode = responseOK
if response.applicationError {
callRes.ResponseCode = responseApplicationError
}
return callRes
}
return new(callResContinue)
}
call.mex = mex
call.initialFragment = initialFragment
call.serviceName = string(callReq.Service)
call.headers = callReq.Headers
call.response = response
call.log = c.log.WithFields(LogField{"In-Call", callReq.ID()})
call.messageForFragment = func(initial bool) message { return new(callReqContinue) }
call.contents = newFragmentingReader(call.log, call)
call.statsReporter = c.statsReporter
call.createStatsTags(c.commonStatsTags)
response.statsReporter = c.statsReporter
response.commonStatsTags = call.commonStatsTags
setResponseHeaders(call.headers, response.headers)
go c.dispatchInbound(c.connID, callReq.ID(), call, frame)
return false
}
// handleCallReqContinue handles the continuation of a call request, forwarding
// it to the request channel for that request, where it can be pulled during
// defragmentation
func (c *Connection) handleCallReqContinue(frame *Frame) bool {
if err := c.inbound.forwardPeerFrame(frame); err != nil {
// If forward fails, it's due to a timeout. We can free this frame.
return true
}
return false
}
// createStatsTags creates the common stats tags, if they are not already created.
func (call *InboundCall) createStatsTags(connectionTags map[string]string) {
call.commonStatsTags = map[string]string{
"calling-service": call.CallerName(),
}
for k, v := range connectionTags {
call.commonStatsTags[k] = v
}
}
// dispatchInbound ispatches an inbound call to the appropriate handler
func (c *Connection) dispatchInbound(_ uint32, _ uint32, call *InboundCall, frame *Frame) {
if call.log.Enabled(LogLevelDebug) {
call.log.Debugf("Received incoming call for %s from %s", call.ServiceName(), c.remotePeerInfo)
}
if err := call.readMethod(); err != nil {
call.log.WithFields(
LogField{"remotePeer", c.remotePeerInfo},
ErrField(err),
).Error("Couldn't read method.")
c.opts.FramePool.Release(frame)
return
}
call.commonStatsTags["endpoint"] = call.methodString
call.statsReporter.IncCounter("inbound.calls.recvd", call.commonStatsTags, 1)
if span := call.response.span; span != nil {
span.SetOperationName(call.methodString)
}
// TODO(prashant): This is an expensive way to check for cancellation. Use a heap for timeouts.
go func() {
select {
case <-call.mex.ctx.Done():
// checking if message exchange timedout or was cancelled
// only two possible errors at this step:
// context.DeadlineExceeded
// context.Canceled
if call.mex.ctx.Err() != nil {
call.mex.inboundExpired()
}
case <-call.mex.errCh.c:
if c.log.Enabled(LogLevelDebug) {
call.log.Debugf("Wait for timeout/cancellation interrupted by error: %v", call.mex.errCh.err)
}
// when an exchange errors out, mark the exchange as expired
// and call cancel so the server handler's context is canceled
// TODO: move the cancel to the parent context at connnection level
call.response.cancel()
call.mex.inboundExpired()
}
}()
// Internal handlers (e.g., introspection) trump all other user-registered handlers on
// the "tchannel" name.
if call.ServiceName() == "tchannel" {
if h := c.internalHandlers.find(call.Method()); h != nil {
h.Handle(call.mex.ctx, call)
return
}
}
c.handler.Handle(call.mex.ctx, call)
}
// An InboundCall is an incoming call from a peer
type InboundCall struct {
reqResReader
conn *Connection
response *InboundCallResponse
serviceName string
method []byte
methodString string
headers transportHeaders
statsReporter StatsReporter
commonStatsTags map[string]string
}
// ServiceName returns the name of the service being called
func (call *InboundCall) ServiceName() string {
return call.serviceName
}
// Method returns the method being called
func (call *InboundCall) Method() []byte {
return call.method
}
// MethodString returns the method being called as a string.
func (call *InboundCall) MethodString() string {
return call.methodString
}
// Format the format of the request from the ArgScheme transport header.
func (call *InboundCall) Format() Format {
return Format(call.headers[ArgScheme])
}
// CallerName returns the caller name from the CallerName transport header.
func (call *InboundCall) CallerName() string {
return call.headers[CallerName]
}
// ShardKey returns the shard key from the ShardKey transport header.
func (call *InboundCall) ShardKey() string {
return call.headers[ShardKey]
}
// RoutingKey returns the routing key from the RoutingKey transport header.
func (call *InboundCall) RoutingKey() string {
return call.headers[RoutingKey]
}
// RoutingDelegate returns the routing delegate from the RoutingDelegate transport header.
func (call *InboundCall) RoutingDelegate() string {
return call.headers[RoutingDelegate]
}
// LocalPeer returns the local peer information for this call.
func (call *InboundCall) LocalPeer() LocalPeerInfo {
return call.conn.localPeerInfo
}
// RemotePeer returns the remote peer information for this call.
func (call *InboundCall) RemotePeer() PeerInfo {
return call.conn.RemotePeerInfo()
}
// CallOptions returns a CallOptions struct suitable for forwarding a request.
func (call *InboundCall) CallOptions() *CallOptions {
return &CallOptions{
CallerName: call.CallerName(),
Format: call.Format(),
ShardKey: call.ShardKey(),
RoutingDelegate: call.RoutingDelegate(),
RoutingKey: call.RoutingKey(),
}
}
// Reads the entire method name (arg1) from the request stream.
func (call *InboundCall) readMethod() error {
var arg1 []byte
if err := NewArgReader(call.arg1Reader()).Read(&arg1); err != nil {
return call.failed(err)
}
call.method = arg1
call.methodString = string(arg1)
return nil
}
// Arg2Reader returns an ArgReader to read the second argument.
// The ReadCloser must be closed once the argument has been read.
func (call *InboundCall) Arg2Reader() (ArgReader, error) {
return call.arg2Reader()
}
// Arg3Reader returns an ArgReader to read the last argument.
// The ReadCloser must be closed once the argument has been read.
func (call *InboundCall) Arg3Reader() (ArgReader, error) {
return call.arg3Reader()
}
// Response provides access to the InboundCallResponse object which can be used
// to write back to the calling peer
func (call *InboundCall) Response() *InboundCallResponse {
if call.err != nil {
// While reading Thrift, we cannot distinguish between malformed Thrift and other errors,
// and so we may try to respond with a bad request. We should ensure that the response
// is marked as failed if the request has failed so that we don't try to shutdown the exchange
// a second time.
call.response.err = call.err
}
return call.response
}
func (call *InboundCall) doneReading(unexpected error) {}
// An InboundCallResponse is used to send the response back to the calling peer
type InboundCallResponse struct {
reqResWriter
call *InboundCall
cancel context.CancelFunc
// calledAt is the time the inbound call was routed to the application.
calledAt time.Time
timeNow func() time.Time
applicationError bool
systemError bool
headers transportHeaders
span opentracing.Span
statsReporter StatsReporter
commonStatsTags map[string]string
}
// SendSystemError returns a system error response to the peer. The call is considered
// complete after this method is called, and no further data can be written.
func (response *InboundCallResponse) SendSystemError(err error) error {
if response.err != nil {
return response.err
}
// Fail all future attempts to read fragments
response.state = reqResWriterComplete
response.systemError = true
response.doneSending()
response.call.releasePreviousFragment()
span := CurrentSpan(response.mex.ctx)
return response.conn.SendSystemError(response.mex.msgID, *span, err)
}
// SetApplicationError marks the response as being an application error. This method can
// only be called before any arguments have been sent to the calling peer.
func (response *InboundCallResponse) SetApplicationError() error {
if response.state > reqResWriterPreArg2 {
return response.failed(errReqResWriterStateMismatch{
state: response.state,
expectedState: reqResWriterPreArg2,
})
}
response.applicationError = true
return nil
}
// Blackhole indicates no response will be sent, and cleans up any resources
// associated with this request. This allows for services to trigger a timeout in
// clients without holding on to any goroutines on the server.
func (response *InboundCallResponse) Blackhole() {
response.cancel()
}
// Arg2Writer returns a WriteCloser that can be used to write the second argument.
// The returned writer must be closed once the write is complete.
func (response *InboundCallResponse) Arg2Writer() (ArgWriter, error) {
if err := NewArgWriter(response.arg1Writer()).Write(nil); err != nil {
return nil, err
}
return response.arg2Writer()
}
// Arg3Writer returns a WriteCloser that can be used to write the last argument.
// The returned writer must be closed once the write is complete.
func (response *InboundCallResponse) Arg3Writer() (ArgWriter, error) {
return response.arg3Writer()
}
// doneSending shuts down the message exchange for this call.
// For incoming calls, the last message is sending the call response.
func (response *InboundCallResponse) doneSending() {
// TODO(prashant): Move this to when the message is actually being sent.
now := response.timeNow()
if span := response.span; span != nil {
if response.applicationError || response.systemError {
ext.Error.Set(span, true)
}
span.FinishWithOptions(opentracing.FinishOptions{FinishTime: now})
}
latency := now.Sub(response.calledAt)
response.statsReporter.RecordTimer("inbound.calls.latency", response.commonStatsTags, latency)
if response.systemError {
// TODO(prashant): Report the error code type as per metrics doc and enable.
// response.statsReporter.IncCounter("inbound.calls.system-errors", response.commonStatsTags, 1)
} else if response.applicationError {
response.statsReporter.IncCounter("inbound.calls.app-errors", response.commonStatsTags, 1)
} else {
response.statsReporter.IncCounter("inbound.calls.success", response.commonStatsTags, 1)
}
// Cancel the context since the response is complete.
response.cancel()
// The message exchange is still open if there are no errors, call shutdown.
if response.err == nil {
response.mex.shutdown()
}
}