forked from yarpc/yarpc-go
/
outbound.go
177 lines (146 loc) · 6.27 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
// 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 middleware
import (
"context"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/internal/introspection"
)
// 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
}
return unaryOutboundWithMiddleware{o: o, f: f}
}
// 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 {
o transport.UnaryOutbound
f UnaryOutbound
}
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
}
return onewayOutboundWithMiddleware{o: o, f: f}
}
// 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 {
o transport.OnewayOutbound
f OnewayOutbound
}
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)
}