-
Notifications
You must be signed in to change notification settings - Fork 103
/
handler.go
335 lines (294 loc) · 10 KB
/
handler.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
// Copyright (c) 2019 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"
"fmt"
"time"
"github.com/opentracing/opentracing-go"
"github.com/uber/tchannel-go"
"go.uber.org/multierr"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/internal/bufferpool"
"go.uber.org/yarpc/pkg/errors"
"go.uber.org/yarpc/yarpcerrors"
"go.uber.org/zap"
ncontext "golang.org/x/net/context"
)
// inboundCall provides an interface similar tchannel.InboundCall.
//
// We use it instead of *tchannel.InboundCall because tchannel.InboundCall is
// not an interface, so we have little control over its behavior in tests.
type inboundCall interface {
ServiceName() string
CallerName() string
MethodString() string
ShardKey() string
RoutingKey() string
RoutingDelegate() string
Format() tchannel.Format
Arg2Reader() (tchannel.ArgReader, error)
Arg3Reader() (tchannel.ArgReader, error)
Response() inboundCallResponse
}
// inboundCallResponse provides an interface similar to
// tchannel.InboundCallResponse.
//
// Its purpose is the same as inboundCall: Make it easier to test functions
// that consume InboundCallResponse without having control of
// InboundCallResponse's behavior.
type inboundCallResponse interface {
Arg2Writer() (tchannel.ArgWriter, error)
Arg3Writer() (tchannel.ArgWriter, error)
Blackhole()
SendSystemError(err error) error
SetApplicationError() error
}
// responseWriter provides an interface similar to handlerWriter.
//
// It allows us to control handlerWriter during testing.
type responseWriter interface {
AddHeaders(h transport.Headers)
AddHeader(key string, value string)
Close() error
IsApplicationError() bool
SetApplicationError()
Write(s []byte) (int, error)
}
// tchannelCall wraps a TChannel InboundCall into an inboundCall.
//
// We need to do this so that we can change the return type of call.Response()
// to match inboundCall's Response().
type tchannelCall struct{ *tchannel.InboundCall }
func (c tchannelCall) Response() inboundCallResponse {
return c.InboundCall.Response()
}
// handler wraps a transport.UnaryHandler into a TChannel Handler.
type handler struct {
existing map[string]tchannel.Handler
router transport.Router
tracer opentracing.Tracer
headerCase headerCase
logger *zap.Logger
newResponseWriter func(inboundCallResponse, tchannel.Format, headerCase) responseWriter
}
func (h handler) Handle(ctx ncontext.Context, call *tchannel.InboundCall) {
h.handle(ctx, tchannelCall{call})
}
func (h handler) handle(ctx context.Context, call inboundCall) {
// you MUST close the responseWriter no matter what unless you have a tchannel.SystemError
responseWriter := h.newResponseWriter(call.Response(), call.Format(), h.headerCase)
// echo accepted rpc-service in response header
responseWriter.AddHeader(ServiceHeaderKey, call.ServiceName())
err := h.callHandler(ctx, call, responseWriter)
// black-hole requests on resource exhausted errors
if yarpcerrors.FromError(err).Code() == yarpcerrors.CodeResourceExhausted {
// all TChannel clients will time out instead of receiving an error
call.Response().Blackhole()
return
}
if err != nil && !responseWriter.IsApplicationError() {
if err := call.Response().SendSystemError(getSystemError(err)); err != nil {
h.logger.Error("SendSystemError failed", zap.Error(err))
}
h.logger.Error("handler failed", zap.Error(err))
return
}
if err != nil && responseWriter.IsApplicationError() {
// we have an error, so we're going to propagate it as a yarpc error,
// regardless of whether or not it is a system error.
status := yarpcerrors.FromError(errors.WrapHandlerError(err, call.ServiceName(), call.MethodString()))
// TODO: what to do with error? we could have a whole complicated scheme to
// return a SystemError here, might want to do that
text, _ := status.Code().MarshalText()
responseWriter.AddHeader(ErrorCodeHeaderKey, string(text))
if status.Name() != "" {
responseWriter.AddHeader(ErrorNameHeaderKey, status.Name())
}
if status.Message() != "" {
responseWriter.AddHeader(ErrorMessageHeaderKey, status.Message())
}
}
if err := responseWriter.Close(); err != nil {
if err := call.Response().SendSystemError(getSystemError(err)); err != nil {
h.logger.Error("SendSystemError failed", zap.Error(err))
}
h.logger.Error("responseWriter failed to close", zap.Error(err))
}
}
func (h handler) callHandler(ctx context.Context, call inboundCall, responseWriter responseWriter) error {
start := time.Now()
_, ok := ctx.Deadline()
if !ok {
return tchannel.ErrTimeoutRequired
}
treq := &transport.Request{
Caller: call.CallerName(),
Service: call.ServiceName(),
Encoding: transport.Encoding(call.Format()),
Transport: transportName,
Procedure: call.MethodString(),
ShardKey: call.ShardKey(),
RoutingKey: call.RoutingKey(),
RoutingDelegate: call.RoutingDelegate(),
}
ctx, headers, err := readRequestHeaders(ctx, call.Format(), call.Arg2Reader)
if err != nil {
return errors.RequestHeadersDecodeError(treq, err)
}
treq.Headers = headers
if tcall, ok := call.(tchannelCall); ok {
tracer := h.tracer
ctx = tchannel.ExtractInboundSpan(ctx, tcall.InboundCall, headers.Items(), tracer)
}
body, err := call.Arg3Reader()
if err != nil {
return err
}
defer body.Close()
treq.Body = body
if err := transport.ValidateRequest(treq); err != nil {
return err
}
spec, err := h.router.Choose(ctx, treq)
if err != nil {
if yarpcerrors.FromError(err).Code() != yarpcerrors.CodeUnimplemented {
return err
}
if tcall, ok := call.(tchannelCall); !ok {
if m, ok := h.existing[call.MethodString()]; ok {
m.Handle(ctx, tcall.InboundCall)
return nil
}
}
return err
}
if err := transport.ValidateRequestContext(ctx); err != nil {
return err
}
switch spec.Type() {
case transport.Unary:
return transport.InvokeUnaryHandler(transport.UnaryInvokeRequest{
Context: ctx,
StartTime: start,
Request: treq,
ResponseWriter: responseWriter,
Handler: spec.Unary(),
Logger: h.logger,
})
default:
return yarpcerrors.Newf(yarpcerrors.CodeUnimplemented, "transport tchannel does not handle %s handlers", spec.Type().String())
}
}
type handlerWriter struct {
failedWith error
format tchannel.Format
headers transport.Headers
buffer *bufferpool.Buffer
response inboundCallResponse
applicationError bool
headerCase headerCase
}
func newHandlerWriter(response inboundCallResponse, format tchannel.Format, headerCase headerCase) responseWriter {
return &handlerWriter{
response: response,
format: format,
headerCase: headerCase,
}
}
func (hw *handlerWriter) AddHeaders(h transport.Headers) {
for k, v := range h.OriginalItems() {
// TODO: is this considered a breaking change?
if isReservedHeaderKey(k) {
hw.failedWith = appendError(hw.failedWith, fmt.Errorf("cannot use reserved header key: %s", k))
return
}
hw.AddHeader(k, v)
}
}
func (hw *handlerWriter) AddHeader(key string, value string) {
hw.headers = hw.headers.With(key, value)
}
func (hw *handlerWriter) SetApplicationError() {
hw.applicationError = true
}
func (hw *handlerWriter) IsApplicationError() bool {
return hw.applicationError
}
func (hw *handlerWriter) Write(s []byte) (int, error) {
if hw.failedWith != nil {
return 0, hw.failedWith
}
if hw.buffer == nil {
hw.buffer = bufferpool.Get()
}
n, err := hw.buffer.Write(s)
if err != nil {
hw.failedWith = appendError(hw.failedWith, err)
}
return n, err
}
func (hw *handlerWriter) Close() error {
retErr := hw.failedWith
if hw.IsApplicationError() {
if err := hw.response.SetApplicationError(); err != nil {
retErr = appendError(retErr, fmt.Errorf("SetApplicationError() failed: %v", err))
}
}
headers := headerMap(hw.headers, hw.headerCase)
retErr = appendError(retErr, writeHeaders(hw.format, headers, nil, hw.response.Arg2Writer))
// Arg3Writer must be opened and closed regardless of if there is data
// However, if there is a system error, we do not want to do this
bodyWriter, err := hw.response.Arg3Writer()
if err != nil {
return appendError(retErr, err)
}
defer func() { retErr = appendError(retErr, bodyWriter.Close()) }()
if hw.buffer != nil {
defer bufferpool.Put(hw.buffer)
if _, err := hw.buffer.WriteTo(bodyWriter); err != nil {
return appendError(retErr, err)
}
}
return retErr
}
func getSystemError(err error) error {
if _, ok := err.(tchannel.SystemError); ok {
return err
}
if !yarpcerrors.IsStatus(err) {
return tchannel.NewSystemError(tchannel.ErrCodeUnexpected, err.Error())
}
status := yarpcerrors.FromError(err)
tchannelCode, ok := _codeToTChannelCode[status.Code()]
if !ok {
tchannelCode = tchannel.ErrCodeUnexpected
}
return tchannel.NewSystemError(tchannelCode, status.Message())
}
func appendError(left error, right error) error {
if _, ok := left.(tchannel.SystemError); ok {
return left
}
if _, ok := right.(tchannel.SystemError); ok {
return right
}
return multierr.Append(left, right)
}