-
Notifications
You must be signed in to change notification settings - Fork 100
/
handler.go
208 lines (175 loc) · 5.87 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
// Copyright (c) 2016 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 http
import (
"bytes"
"context"
"io"
"net/http"
"time"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/internal/errors"
"go.uber.org/yarpc/internal/request"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
)
func popHeader(h http.Header, n string) string {
v := h.Get(n)
h.Del(n)
return v
}
// handler adapts a transport.Handler into a handler for net/http.
type handler struct {
registry transport.Registry
tracer opentracing.Tracer
}
func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
start := time.Now()
defer req.Body.Close()
if req.Method != "POST" {
http.NotFound(w, req)
return
}
service := req.Header.Get(ServiceHeader)
procedure := req.Header.Get(ProcedureHeader)
err := h.callHandler(w, req, start)
if err == nil {
return
}
err = errors.AsHandlerError(service, procedure, err)
status := http.StatusInternalServerError
if transport.IsBadRequestError(err) {
status = http.StatusBadRequest
} else if transport.IsTimeoutError(err) {
status = http.StatusGatewayTimeout
}
http.Error(w, err.Error(), status)
}
func (h handler) callHandler(w http.ResponseWriter, req *http.Request, start time.Time) error {
treq := &transport.Request{
Caller: popHeader(req.Header, CallerHeader),
Service: popHeader(req.Header, ServiceHeader),
Procedure: popHeader(req.Header, ProcedureHeader),
Encoding: transport.Encoding(popHeader(req.Header, EncodingHeader)),
Headers: applicationHeaders.FromHTTPHeaders(req.Header, transport.Headers{}),
Body: req.Body,
}
ctx := req.Context()
ctx, cancel, parseTTLErr := parseTTL(ctx, treq, popHeader(req.Header, TTLMSHeader))
// parseTTLErr != nil is a problem only if the request is unary.
defer cancel()
ctx, span := h.createSpan(ctx, req, treq, start)
v := request.Validator{Request: treq}
if err := v.ValidateCommon(ctx); err != nil {
return err
}
spec, err := h.registry.Choose(ctx, treq)
if err != nil {
return updateSpanWithErr(span, err)
}
switch spec.Type() {
case transport.Unary:
defer span.Finish()
if parseTTLErr != nil {
return parseTTLErr
}
if err := v.ValidateUnary(ctx); err != nil {
return err
}
err = transport.DispatchUnaryHandler(ctx, spec.Unary(), start, treq, newResponseWriter(w))
case transport.Oneway:
if err := v.ValidateOneway(ctx); err != nil {
return err
}
err = handleOnewayRequest(span, treq, spec.Oneway())
default:
err = errors.UnsupportedTypeError{Transport: "HTTP", Type: string(spec.Type())}
}
return updateSpanWithErr(span, err)
}
func handleOnewayRequest(
span opentracing.Span,
treq *transport.Request,
onewayHandler transport.OnewayHandler,
) error {
// we will lose access to the body unless we read all the bytes before
// returning from the request
var buff bytes.Buffer
if _, err := io.Copy(&buff, treq.Body); err != nil {
return err
}
treq.Body = &buff
// create a new context for oneway requests since the HTTP handler cancels
// http.Request's context when ServeHTTP returns
ctx := opentracing.ContextWithSpan(context.Background(), span)
go func() {
// ensure the span lasts for length of the handler in case of errors
defer span.Finish()
err := transport.DispatchOnewayHandler(ctx, onewayHandler, treq)
updateSpanWithErr(span, err)
}()
return nil
}
func updateSpanWithErr(span opentracing.Span, err error) error {
if err != nil {
span.SetTag("error", true)
span.LogEvent(err.Error())
}
return err
}
func (h handler) createSpan(ctx context.Context, req *http.Request, treq *transport.Request, start time.Time) (context.Context, opentracing.Span) {
// Extract opentracing etc baggage from headers
// Annotate the inbound context with a trace span
tracer := h.tracer
carrier := opentracing.HTTPHeadersCarrier(req.Header)
parentSpanCtx, _ := tracer.Extract(opentracing.HTTPHeaders, carrier)
// parentSpanCtx may be nil, ext.RPCServerOption handles a nil parent
// gracefully.
span := tracer.StartSpan(
treq.Procedure,
opentracing.StartTime(start),
opentracing.Tags{
"rpc.caller": treq.Caller,
"rpc.service": treq.Service,
"rpc.encoding": treq.Encoding,
"rpc.transport": "http",
},
ext.RPCServerOption(parentSpanCtx), // implies ChildOf
)
ext.PeerService.Set(span, treq.Caller)
ctx = opentracing.ContextWithSpan(ctx, span)
return ctx, span
}
// responseWriter adapts a http.ResponseWriter into a transport.ResponseWriter.
type responseWriter struct {
w http.ResponseWriter
}
func newResponseWriter(w http.ResponseWriter) responseWriter {
return responseWriter{w: w}
}
func (rw responseWriter) Write(s []byte) (int, error) {
return rw.w.Write(s)
}
func (rw responseWriter) AddHeaders(h transport.Headers) {
applicationHeaders.ToHTTPHeaders(h, rw.w.Header())
}
func (responseWriter) SetApplicationError() {
// Nothing to do.
}