forked from yarpc/yarpc-go
/
inbound.go
172 lines (150 loc) · 5.08 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
// 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 thrift
import (
"bytes"
"context"
"go.uber.org/thriftrw/protocol"
"go.uber.org/thriftrw/wire"
encodingapi "go.uber.org/yarpc/api/encoding"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/internal/bufferpool"
"go.uber.org/yarpc/pkg/errors"
)
// thriftUnaryHandler wraps a Thrift Handler into a transport.UnaryHandler
type thriftUnaryHandler struct {
UnaryHandler UnaryHandler
Protocol protocol.Protocol
Enveloping bool
}
// thriftOnewayHandler wraps a Thrift Handler into a transport.OnewayHandler
type thriftOnewayHandler struct {
OnewayHandler OnewayHandler
Protocol protocol.Protocol
Enveloping bool
}
func (t thriftUnaryHandler) Handle(ctx context.Context, treq *transport.Request, rw transport.ResponseWriter) error {
ctx, call, reqValue, responder, err := handleRequest(ctx, treq, wire.Call, t.Protocol, t.Enveloping)
if err != nil {
return err
}
res, err := t.UnaryHandler(ctx, reqValue)
if err != nil {
return err
}
if resType := res.Body.EnvelopeType(); resType != wire.Reply {
return errors.ResponseBodyEncodeError(
treq, errUnexpectedEnvelopeType(resType))
}
value, err := res.Body.ToWire()
if err != nil {
return err
}
if res.IsApplicationError {
rw.SetApplicationError()
}
if err := call.WriteToResponse(rw); err != nil {
// not reachable
return err
}
if err = responder.EncodeResponse(value, wire.Reply, rw); err != nil {
return errors.ResponseBodyEncodeError(treq, err)
}
return nil
}
func (t thriftOnewayHandler) HandleOneway(ctx context.Context, treq *transport.Request) error {
ctx, _, reqValue, _, err := handleRequest(ctx, treq, wire.OneWay, t.Protocol, t.Enveloping)
if err != nil {
return err
}
return t.OnewayHandler(ctx, reqValue)
}
// handleRequest is a utility shared by Unary and Oneway handlers, to decode
// the request, regardless of enveloping.
func handleRequest(
parentCtx context.Context,
treq *transport.Request,
// reqEnvelopeType indicates the expected envelope type, if an envelope is
// present.
reqEnvelopeType wire.EnvelopeType,
// proto is the encoding protocol (e.g., Binary) or an
// EnvelopeAgnosticProtocol (e.g., EnvelopeAgnosticBinary)
proto protocol.Protocol,
// enveloping indicates that requests must be enveloped, used only if the
// protocol is not envelope agnostic.
enveloping bool,
) (
// ctx is a context including the inbound call.
ctx context.Context,
// call is an inboundCall populated from the transport request and context.
call *encodingapi.InboundCall,
// reqValue is the wire representation of the decoded request.
// handleRequest does not surface the envelope.
reqValue wire.Value,
// responder indicates how to encode the response, with the enveloping
// strategy corresponding to the request. It is not used for oneway handlers.
responder protocol.Responder,
err error,
) {
ctx = parentCtx
if err = errors.ExpectEncodings(treq, Encoding); err != nil {
return
}
ctx, call = encodingapi.NewInboundCall(ctx)
if err = call.ReadFromRequest(treq); err != nil {
// not reachable
return
}
buf := bufferpool.Get()
defer bufferpool.Put(buf)
if _, err = buf.ReadFrom(treq.Body); err != nil {
return
}
reader := bytes.NewReader(buf.Bytes())
// Discover or choose the appropriate envelope
if agnosticProto, ok := proto.(protocol.EnvelopeAgnosticProtocol); ok {
// Envelope-agnostic
reqValue, responder, err = agnosticProto.DecodeRequest(reqEnvelopeType, reader)
if err != nil {
return
}
} else if enveloping {
// Enveloped
var envelope wire.Envelope
envelope, err = proto.DecodeEnveloped(reader)
if err != nil {
return
}
if envelope.Type != reqEnvelopeType {
err = errors.RequestBodyDecodeError(treq, errUnexpectedEnvelopeType(envelope.Type))
return
}
reqValue = envelope.Value
responder = protocol.EnvelopeV1Responder{Name: envelope.Name, SeqID: envelope.SeqID}
} else {
// Not-enveloped
reqValue, err = proto.Decode(reader, wire.TStruct)
if err != nil {
return
}
responder = protocol.NoEnvelopeResponder
}
return
}