-
Notifications
You must be signed in to change notification settings - Fork 100
/
outbound.go
284 lines (230 loc) · 9.46 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
// Copyright (c) 2020 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 middleware
import (
"context"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/api/x/introspection"
)
var (
_ transport.Namer = (*unaryOutboundWithMiddleware)(nil)
_ transport.Namer = (*onewayOutboundWithMiddleware)(nil)
_ transport.Namer = (*streamOutboundWithMiddleware)(nil)
)
// UnaryOutbound defines transport-level middleware for
// `UnaryOutbound`s.
//
// UnaryOutbound middleware MAY do zero or more of the following: change the
// context, change the request, change the returned response, handle the
// returned error, call the given outbound zero or more times.
//
// UnaryOutbound middleware MUST always return a non-nil Response or error,
// and they MUST be thread-safe
//
// UnaryOutbound middleware is re-used across requests and MAY be called
// multiple times on the same request.
type UnaryOutbound interface {
Call(ctx context.Context, request *transport.Request, out transport.UnaryOutbound) (*transport.Response, error)
}
// NopUnaryOutbound is a unary outbound middleware that does not do
// anything special. It simply calls the underlying UnaryOutbound.
var NopUnaryOutbound UnaryOutbound = nopUnaryOutbound{}
// ApplyUnaryOutbound applies the given UnaryOutbound middleware to
// the given UnaryOutbound transport.
func ApplyUnaryOutbound(o transport.UnaryOutbound, f UnaryOutbound) transport.UnaryOutbound {
if f == nil {
return o
}
var name string
if namer, ok := o.(transport.Namer); ok {
name = namer.TransportName()
}
return unaryOutboundWithMiddleware{o: o, f: f, name: name}
}
// UnaryOutboundFunc adapts a function into a UnaryOutbound middleware.
type UnaryOutboundFunc func(context.Context, *transport.Request, transport.UnaryOutbound) (*transport.Response, error)
// Call for UnaryOutboundFunc.
func (f UnaryOutboundFunc) Call(ctx context.Context, request *transport.Request, out transport.UnaryOutbound) (*transport.Response, error) {
return f(ctx, request, out)
}
type unaryOutboundWithMiddleware struct {
name string
o transport.UnaryOutbound
f UnaryOutbound
}
func (fo unaryOutboundWithMiddleware) TransportName() string {
return fo.name
}
func (fo unaryOutboundWithMiddleware) Transports() []transport.Transport {
return fo.o.Transports()
}
func (fo unaryOutboundWithMiddleware) Start() error {
return fo.o.Start()
}
func (fo unaryOutboundWithMiddleware) Stop() error {
return fo.o.Stop()
}
func (fo unaryOutboundWithMiddleware) IsRunning() bool {
return fo.o.IsRunning()
}
func (fo unaryOutboundWithMiddleware) Introspect() introspection.OutboundStatus {
if o, ok := fo.o.(introspection.IntrospectableOutbound); ok {
return o.Introspect()
}
return introspection.OutboundStatusNotSupported
}
func (fo unaryOutboundWithMiddleware) Call(ctx context.Context, request *transport.Request) (*transport.Response, error) {
return fo.f.Call(ctx, request, fo.o)
}
type nopUnaryOutbound struct{}
func (nopUnaryOutbound) Call(ctx context.Context, request *transport.Request, out transport.UnaryOutbound) (*transport.Response, error) {
return out.Call(ctx, request)
}
// OnewayOutbound defines transport-level middleware for `OnewayOutbound`s.
//
// OnewayOutbound middleware MAY do zero or more of the following: change the
// context, change the request, change the returned ack, handle the returned
// error, call the given outbound zero or more times.
//
// OnewayOutbound middleware MUST always return an Ack (nil or not) or an
// error, and they MUST be thread-safe.
//
// OnewayOutbound middleware is re-used across requests and MAY be called
// multiple times on the same request.
type OnewayOutbound interface {
CallOneway(ctx context.Context, request *transport.Request, out transport.OnewayOutbound) (transport.Ack, error)
}
// NopOnewayOutbound is a oneway outbound middleware that does not do
// anything special. It simply calls the underlying OnewayOutbound transport.
var NopOnewayOutbound OnewayOutbound = nopOnewayOutbound{}
// ApplyOnewayOutbound applies the given OnewayOutbound middleware to
// the given OnewayOutbound transport.
func ApplyOnewayOutbound(o transport.OnewayOutbound, f OnewayOutbound) transport.OnewayOutbound {
if f == nil {
return o
}
var name string
if namer, ok := o.(transport.Namer); ok {
name = namer.TransportName()
}
return onewayOutboundWithMiddleware{o: o, f: f, name: name}
}
// OnewayOutboundFunc adapts a function into a OnewayOutbound middleware.
type OnewayOutboundFunc func(context.Context, *transport.Request, transport.OnewayOutbound) (transport.Ack, error)
// CallOneway for OnewayOutboundFunc.
func (f OnewayOutboundFunc) CallOneway(ctx context.Context, request *transport.Request, out transport.OnewayOutbound) (transport.Ack, error) {
return f(ctx, request, out)
}
type onewayOutboundWithMiddleware struct {
name string
o transport.OnewayOutbound
f OnewayOutbound
}
func (fo onewayOutboundWithMiddleware) TransportName() string {
return fo.name
}
func (fo onewayOutboundWithMiddleware) Transports() []transport.Transport {
return fo.o.Transports()
}
func (fo onewayOutboundWithMiddleware) Start() error {
return fo.o.Start()
}
func (fo onewayOutboundWithMiddleware) Stop() error {
return fo.o.Stop()
}
func (fo onewayOutboundWithMiddleware) IsRunning() bool {
return fo.o.IsRunning()
}
func (fo onewayOutboundWithMiddleware) CallOneway(ctx context.Context, request *transport.Request) (transport.Ack, error) {
return fo.f.CallOneway(ctx, request, fo.o)
}
func (fo onewayOutboundWithMiddleware) Introspect() introspection.OutboundStatus {
if o, ok := fo.o.(introspection.IntrospectableOutbound); ok {
return o.Introspect()
}
return introspection.OutboundStatusNotSupported
}
type nopOnewayOutbound struct{}
func (nopOnewayOutbound) CallOneway(ctx context.Context, request *transport.Request, out transport.OnewayOutbound) (transport.Ack, error) {
return out.CallOneway(ctx, request)
}
// StreamOutbound defines transport-level middleware for
// `StreamOutbound`s.
//
// StreamOutbound middleware MAY do zero or more of the following: change the
// context, change the requestMeta, change the returned Stream, handle the
// returned error, call the given outbound zero or more times.
//
// StreamOutbound middleware MUST always return a non-nil Stream or error,
// and they MUST be thread-safe
//
// StreamOutbound middleware is re-used across requests and MAY be called
// multiple times on the same request.
type StreamOutbound interface {
CallStream(ctx context.Context, request *transport.StreamRequest, out transport.StreamOutbound) (*transport.ClientStream, error)
}
// NopStreamOutbound is a stream outbound middleware that does not do
// anything special. It simply calls the underlying StreamOutbound.
var NopStreamOutbound StreamOutbound = nopStreamOutbound{}
// ApplyStreamOutbound applies the given StreamOutbound middleware to
// the given StreamOutbound transport.
func ApplyStreamOutbound(o transport.StreamOutbound, f StreamOutbound) transport.StreamOutbound {
if f == nil {
return o
}
var name string
if namer, ok := o.(transport.Namer); ok {
name = namer.TransportName()
}
return streamOutboundWithMiddleware{o: o, f: f, name: name}
}
// StreamOutboundFunc adapts a function into a StreamOutbound middleware.
type StreamOutboundFunc func(context.Context, *transport.StreamRequest, transport.StreamOutbound) (*transport.ClientStream, error)
// CallStream for StreamOutboundFunc.
func (f StreamOutboundFunc) CallStream(ctx context.Context, request *transport.StreamRequest, out transport.StreamOutbound) (*transport.ClientStream, error) {
return f(ctx, request, out)
}
type streamOutboundWithMiddleware struct {
name string
o transport.StreamOutbound
f StreamOutbound
}
func (fo streamOutboundWithMiddleware) TransportName() string {
return fo.name
}
func (fo streamOutboundWithMiddleware) Transports() []transport.Transport {
return fo.o.Transports()
}
func (fo streamOutboundWithMiddleware) Start() error {
return fo.o.Start()
}
func (fo streamOutboundWithMiddleware) Stop() error {
return fo.o.Stop()
}
func (fo streamOutboundWithMiddleware) IsRunning() bool {
return fo.o.IsRunning()
}
func (fo streamOutboundWithMiddleware) CallStream(ctx context.Context, request *transport.StreamRequest) (*transport.ClientStream, error) {
return fo.f.CallStream(ctx, request, fo.o)
}
type nopStreamOutbound struct{}
func (nopStreamOutbound) CallStream(ctx context.Context, request *transport.StreamRequest, out transport.StreamOutbound) (*transport.ClientStream, error) {
return out.CallStream(ctx, request)
}