forked from temporalio/tchannel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outbound.go
379 lines (322 loc) · 12.5 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
// 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 (
"fmt"
"time"
"github.com/uber/tchannel-go/typed"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"golang.org/x/net/context"
)
// maxMethodSize is the maximum size of arg1.
const maxMethodSize = 16 * 1024
// beginCall begins an outbound call on the connection
func (c *Connection) beginCall(ctx context.Context, serviceName, methodName string, callOptions *CallOptions) (*OutboundCall, error) {
now := c.timeNow()
switch state := c.readState(); state {
case connectionActive:
break
case connectionStartClose, connectionInboundClosed, connectionClosed:
return nil, ErrConnectionClosed
default:
return nil, errConnectionUnknownState{"beginCall", state}
}
deadline, ok := ctx.Deadline()
if !ok {
// This case is handled by validateCall, so we should
// never get here.
return nil, ErrTimeoutRequired
}
// If the timeToLive is less than a millisecond, it will be encoded as 0 on
// the wire, hence we return a timeout immediately.
timeToLive := deadline.Sub(now)
if timeToLive < time.Millisecond {
return nil, ErrTimeout
}
if err := ctx.Err(); err != nil {
return nil, GetContextError(err)
}
requestID := c.NextMessageID()
mex, err := c.outbound.newExchange(ctx, c.opts.FramePool, messageTypeCallReq, requestID, mexChannelBufferSize)
if err != nil {
return nil, err
}
// Close may have been called between the time we checked the state and us creating the exchange.
if state := c.readState(); state != connectionActive {
mex.shutdown()
return nil, ErrConnectionClosed
}
// Note: We don't verify number of transport headers as the library doesn't
// allow adding arbitrary headers. Ensure we never add >= 256 headers here.
headers := transportHeaders{
CallerName: c.localPeerInfo.ServiceName,
}
callOptions.setHeaders(headers)
if opts := currentCallOptions(ctx); opts != nil {
opts.overrideHeaders(headers)
}
call := new(OutboundCall)
call.mex = mex
call.conn = c
call.callReq = callReq{
id: requestID,
Headers: headers,
Service: serviceName,
TimeToLive: timeToLive,
}
call.statsReporter = c.statsReporter
call.createStatsTags(c.commonStatsTags, callOptions, methodName)
call.log = c.log.WithFields(LogField{"Out-Call", requestID})
// TODO(mmihic): It'd be nice to do this without an fptr
call.messageForFragment = func(initial bool) message {
if initial {
return &call.callReq
}
return new(callReqContinue)
}
call.contents = newFragmentingWriter(call.log, call, c.opts.ChecksumType.New())
response := new(OutboundCallResponse)
response.startedAt = now
response.timeNow = c.timeNow
response.requestState = callOptions.RequestState
response.mex = mex
response.log = c.log.WithFields(LogField{"Out-Response", requestID})
response.span = c.startOutboundSpan(ctx, serviceName, methodName, call, now)
response.messageForFragment = func(initial bool) message {
if initial {
return &response.callRes
}
return new(callResContinue)
}
response.contents = newFragmentingReader(response.log, response)
response.statsReporter = call.statsReporter
response.commonStatsTags = call.commonStatsTags
call.response = response
if err := call.writeMethod([]byte(methodName)); err != nil {
return nil, err
}
return call, nil
}
// handleCallRes handles an incoming call req message, forwarding the
// frame to the response channel waiting for it
func (c *Connection) handleCallRes(frame *Frame) bool {
if err := c.outbound.forwardPeerFrame(frame); err != nil {
return true
}
return false
}
// handleCallResContinue handles an incoming call res continue message,
// forwarding the frame to the response channel waiting for it
func (c *Connection) handleCallResContinue(frame *Frame) bool {
if err := c.outbound.forwardPeerFrame(frame); err != nil {
return true
}
return false
}
// An OutboundCall is an active call to a remote peer. A client makes a call
// by calling BeginCall on the Channel, writing argument content via
// ArgWriter2() ArgWriter3(), and then reading reading response data via the
// ArgReader2() and ArgReader3() methods on the Response() object.
type OutboundCall struct {
reqResWriter
callReq callReq
response *OutboundCallResponse
statsReporter StatsReporter
commonStatsTags map[string]string
}
// Response provides access to the call's response object, which can be used to
// read response arguments
func (call *OutboundCall) Response() *OutboundCallResponse {
return call.response
}
// createStatsTags creates the common stats tags, if they are not already created.
func (call *OutboundCall) createStatsTags(connectionTags map[string]string, callOptions *CallOptions, method string) {
call.commonStatsTags = map[string]string{
"target-service": call.callReq.Service,
}
for k, v := range connectionTags {
call.commonStatsTags[k] = v
}
if callOptions.Format != HTTP {
call.commonStatsTags["target-endpoint"] = string(method)
}
}
// writeMethod writes the method (arg1) to the call
func (call *OutboundCall) writeMethod(method []byte) error {
call.statsReporter.IncCounter("outbound.calls.send", call.commonStatsTags, 1)
return NewArgWriter(call.arg1Writer()).Write(method)
}
// 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 (call *OutboundCall) Arg2Writer() (ArgWriter, error) {
return call.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 (call *OutboundCall) Arg3Writer() (ArgWriter, error) {
return call.arg3Writer()
}
// LocalPeer returns the local peer information for this call.
func (call *OutboundCall) LocalPeer() LocalPeerInfo {
return call.conn.localPeerInfo
}
// RemotePeer returns the remote peer information for this call.
func (call *OutboundCall) RemotePeer() PeerInfo {
return call.conn.RemotePeerInfo()
}
func (call *OutboundCall) doneSending() {}
// An OutboundCallResponse is the response to an outbound call
type OutboundCallResponse struct {
reqResReader
callRes callRes
requestState *RequestState
// startedAt is the time at which the outbound call was started.
startedAt time.Time
timeNow func() time.Time
span opentracing.Span
statsReporter StatsReporter
commonStatsTags map[string]string
}
// ApplicationError returns true if the call resulted in an application level error
// TODO(mmihic): In current implementation, you must have called Arg2Reader before this
// method returns the proper value. We should instead have this block until the first
// fragment is available, if the first fragment hasn't been received.
func (response *OutboundCallResponse) ApplicationError() bool {
// TODO(mmihic): Wait for first fragment
return response.callRes.ResponseCode == responseApplicationError
}
// Format the format of the request from the ArgScheme transport header.
func (response *OutboundCallResponse) Format() Format {
return Format(response.callRes.Headers[ArgScheme])
}
// Arg2Reader returns an ArgReader to read the second argument.
// The ReadCloser must be closed once the argument has been read.
func (response *OutboundCallResponse) Arg2Reader() (ArgReader, error) {
var method []byte
if err := NewArgReader(response.arg1Reader()).Read(&method); err != nil {
return nil, err
}
return response.arg2Reader()
}
// Arg3Reader returns an ArgReader to read the last argument.
// The ReadCloser must be closed once the argument has been read.
func (response *OutboundCallResponse) Arg3Reader() (ArgReader, error) {
return response.arg3Reader()
}
// handleError handles an error coming back from the peer. If the error is a
// protocol level error, the entire connection will be closed. If the error is
// a request specific error, it will be written to the request's response
// channel and converted into a SystemError returned from the next reader or
// access call.
// The return value is whether the frame should be released immediately.
func (c *Connection) handleError(frame *Frame) bool {
errMsg := errorMessage{
id: frame.Header.ID,
}
rbuf := typed.NewReadBuffer(frame.SizedPayload())
if err := errMsg.read(rbuf); err != nil {
c.log.WithFields(
LogField{"remotePeer", c.remotePeerInfo},
ErrField(err),
).Warn("Unable to read error frame.")
c.connectionError("parsing error frame", err)
return true
}
if errMsg.errCode == ErrCodeProtocol {
c.log.WithFields(
LogField{"remotePeer", c.remotePeerInfo},
LogField{"error", errMsg.message},
).Warn("Peer reported protocol error.")
c.connectionError("received protocol error", errMsg.AsSystemError())
return true
}
if err := c.outbound.forwardPeerFrame(frame); err != nil {
c.log.WithFields(
LogField{"frameHeader", frame.Header.String()},
LogField{"id", errMsg.id},
LogField{"errorMessage", errMsg.message},
LogField{"errorCode", errMsg.errCode},
ErrField(err),
).Info("Failed to forward error frame.")
return true
}
// If the frame was forwarded, then the other side is responsible for releasing the frame.
return false
}
func cloneTags(tags map[string]string) map[string]string {
newTags := make(map[string]string, len(tags))
for k, v := range tags {
newTags[k] = v
}
return newTags
}
// doneReading shuts down the message exchange for this call.
// For outgoing calls, the last message is reading the call response.
func (response *OutboundCallResponse) doneReading(unexpected error) {
now := response.timeNow()
isSuccess := unexpected == nil && !response.ApplicationError()
lastAttempt := isSuccess || !response.requestState.HasRetries(unexpected)
// TODO how should this work with retries?
if span := response.span; span != nil {
if unexpected != nil {
span.LogEventWithPayload("error", unexpected)
}
if !isSuccess && lastAttempt {
ext.Error.Set(span, true)
}
span.FinishWithOptions(opentracing.FinishOptions{FinishTime: now})
}
latency := now.Sub(response.startedAt)
response.statsReporter.RecordTimer("outbound.calls.per-attempt.latency", response.commonStatsTags, latency)
if lastAttempt {
requestLatency := response.requestState.SinceStart(now, latency)
response.statsReporter.RecordTimer("outbound.calls.latency", response.commonStatsTags, requestLatency)
}
if retryCount := response.requestState.RetryCount(); retryCount > 0 {
retryTags := cloneTags(response.commonStatsTags)
retryTags["retry-count"] = fmt.Sprint(retryCount)
response.statsReporter.IncCounter("outbound.calls.retries", retryTags, 1)
}
if unexpected != nil {
// TODO(prashant): Report the error code type as per metrics doc and enable.
// response.statsReporter.IncCounter("outbound.calls.system-errors", response.commonStatsTags, 1)
} else if response.ApplicationError() {
// TODO(prashant): Figure out how to add "type" to tags, which TChannel does not know about.
response.statsReporter.IncCounter("outbound.calls.per-attempt.app-errors", response.commonStatsTags, 1)
if lastAttempt {
response.statsReporter.IncCounter("outbound.calls.app-errors", response.commonStatsTags, 1)
}
} else {
response.statsReporter.IncCounter("outbound.calls.success", response.commonStatsTags, 1)
}
response.mex.shutdown()
}
func validateCall(ctx context.Context, serviceName, methodName string, callOpts *CallOptions) error {
if serviceName == "" {
return ErrNoServiceName
}
if len(methodName) > maxMethodSize {
return ErrMethodTooLarge
}
if _, ok := ctx.Deadline(); !ok {
return ErrTimeoutRequired
}
return nil
}