/
handler.go
255 lines (219 loc) · 6.76 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
// 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"
"fmt"
"time"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/internal/encoding"
"go.uber.org/yarpc/internal/errors"
"go.uber.org/yarpc/internal/request"
"github.com/opentracing/opentracing-go"
"github.com/uber/tchannel-go"
ncontext "golang.org/x/net/context"
)
// inboundCall provides an interface similiar 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
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)
SendSystemError(err error) error
SetApplicationError() 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
}
func (h handler) Handle(ctx ncontext.Context, call *tchannel.InboundCall) {
h.handle(ctx, tchannelCall{call})
}
func (h handler) handle(ctx context.Context, call inboundCall) {
start := time.Now()
err := h.callHandler(ctx, call, start)
if err == nil {
return
}
if _, ok := err.(tchannel.SystemError); ok {
// TODO: log error
_ = call.Response().SendSystemError(err)
return
}
err = errors.AsHandlerError(call.ServiceName(), call.MethodString(), err)
status := tchannel.ErrCodeUnexpected
if transport.IsBadRequestError(err) {
status = tchannel.ErrCodeBadRequest
} else if transport.IsTimeoutError(err) {
status = tchannel.ErrCodeTimeout
}
// TODO: log error
_ = call.Response().SendSystemError(tchannel.NewSystemError(status, err.Error()))
}
func (h handler) callHandler(ctx context.Context, call inboundCall, start time.Time) error {
_, ok := ctx.Deadline()
if !ok {
return tchannel.ErrTimeoutRequired
}
treq := &transport.Request{
Caller: call.CallerName(),
Service: call.ServiceName(),
Encoding: transport.Encoding(call.Format()),
Procedure: call.MethodString(),
}
ctx, headers, err := readRequestHeaders(ctx, call.Format(), call.Arg2Reader)
if err != nil {
return encoding.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
rw := newResponseWriter(treq, call)
defer rw.Close() // TODO(abg): log if this errors
if err := transport.ValidateRequest(treq); err != nil {
return err
}
spec, err := h.router.Choose(ctx, treq)
if err != nil {
if _, ok := err.(errors.UnrecognizedProcedureError); !ok {
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
}
switch spec.Type() {
case transport.Unary:
if err := request.ValidateUnaryContext(ctx); err != nil {
return err
}
err = transport.DispatchUnaryHandler(ctx, spec.Unary(), start, treq, rw)
default:
err = errors.UnsupportedTypeError{Transport: "TChannel", Type: spec.Type().String()}
}
return err
}
type responseWriter struct {
treq *transport.Request
failedWith error
bodyWriter tchannel.ArgWriter
format tchannel.Format
headers transport.Headers
response inboundCallResponse
wroteHeaders bool
}
func newResponseWriter(treq *transport.Request, call inboundCall) *responseWriter {
return &responseWriter{
treq: treq,
response: call.Response(),
format: call.Format(),
}
}
func (rw *responseWriter) AddHeaders(h transport.Headers) {
if rw.wroteHeaders {
panic("AddHeaders() cannot be called after calling Write().")
}
for k, v := range h.Items() {
rw.headers = rw.headers.With(k, v)
}
}
func (rw *responseWriter) SetApplicationError() {
if rw.wroteHeaders {
panic("SetApplicationError() cannot be called after calling Write().")
}
err := rw.response.SetApplicationError()
if err != nil {
panic(fmt.Sprintf("SetApplicationError() failed: %v", err))
}
}
func (rw *responseWriter) Write(s []byte) (int, error) {
if rw.failedWith != nil {
return 0, rw.failedWith
}
if !rw.wroteHeaders {
rw.wroteHeaders = true
if err := writeHeaders(rw.format, rw.headers, rw.response.Arg2Writer); err != nil {
err = encoding.ResponseHeadersEncodeError(rw.treq, err)
rw.failedWith = err
return 0, err
}
}
if rw.bodyWriter == nil {
var err error
rw.bodyWriter, err = rw.response.Arg3Writer()
if err != nil {
rw.failedWith = err
return 0, err
}
}
n, err := rw.bodyWriter.Write(s)
if err != nil {
rw.failedWith = err
}
return n, err
}
func (rw *responseWriter) Close() error {
var err error
if rw.bodyWriter != nil {
err = rw.bodyWriter.Close()
}
if rw.failedWith != nil {
return rw.failedWith
}
return err
}