forked from yarpc/yarpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatcher.go
366 lines (313 loc) · 10.5 KB
/
dispatcher.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// 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 yarpc
import (
"fmt"
"sync"
"go.uber.org/yarpc/api/middleware"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/internal/clientconfig"
"go.uber.org/yarpc/internal/errors"
"go.uber.org/yarpc/internal/request"
intsync "go.uber.org/yarpc/internal/sync"
"github.com/opentracing/opentracing-go"
)
// StartStoppable objects are used to define a common Start/Stop functionality
// across different dispatcher objects
type StartStoppable interface {
// Starts the RPC allowing it to accept and process new incoming
// requests.
//
// Blocks until the RPC is ready to start accepting new requests.
Start() error
// Stops the RPC. No new requests will be accepted.
//
// Blocks until the RPC has stopped.
Stop() error
}
// Dispatcher object is used to configure a YARPC application; it is used by
// Clients to send RPCs, and by Procedures to recieve them. This object is what
// enables an application to be transport-agnostic.
type Dispatcher interface {
transport.Registrar
transport.ClientConfigProvider
// Inbounds returns a copy of the list of inbounds for this RPC object.
//
// The Inbounds will be returned in the same order that was used in the
// configuration.
Inbounds() Inbounds
// Starts the RPC allowing it to accept and process new incoming
// requests.
//
// Blocks until the RPC is ready to start accepting new requests.
Start() error
// Stops the RPC. No new requests will be accepted.
//
// Blocks until the RPC has stopped.
Stop() error
}
// Config specifies the parameters of a new RPC constructed via New.
type Config struct {
Name string
Inbounds Inbounds
Outbounds Outbounds
// Inbound and Outbound Middleware that will be applied to all incoming and
// outgoing requests respectively.
InboundMiddleware InboundMiddleware
OutboundMiddleware OutboundMiddleware
// Tracer is deprecated. The dispatcher does nothing with this propery.
Tracer opentracing.Tracer
}
// Inbounds contains a list of inbound transports
type Inbounds []transport.Inbound
// Outbounds encapsulates a service and its outbounds
type Outbounds map[string]transport.Outbounds
// OutboundMiddleware contains the different type of outbound middleware
type OutboundMiddleware struct {
Unary middleware.UnaryOutboundMiddleware
Oneway middleware.OnewayOutboundMiddleware
}
// InboundMiddleware contains the different type of inbound middleware
type InboundMiddleware struct {
Unary middleware.UnaryInboundMiddleware
Oneway middleware.OnewayInboundMiddleware
}
// NewDispatcher builds a new Dispatcher using the specified Config.
func NewDispatcher(cfg Config) Dispatcher {
if cfg.Name == "" {
panic("a service name is required")
}
return dispatcher{
Name: cfg.Name,
Registrar: NewMapRegistry(cfg.Name),
inbounds: cfg.Inbounds,
outbounds: convertOutbounds(cfg.Outbounds, cfg.OutboundMiddleware),
transports: collectTransports(cfg.Inbounds, cfg.Outbounds),
InboundMiddleware: cfg.InboundMiddleware,
}
}
// convertOutbounds applys outbound middleware and creates validator outbounds
func convertOutbounds(outbounds Outbounds, mw OutboundMiddleware) Outbounds {
convertedOutbounds := make(Outbounds, len(outbounds))
for service, outs := range outbounds {
if outs.Unary == nil && outs.Oneway == nil {
panic(fmt.Sprintf("no outbound set for service %q in dispatcher", service))
}
var (
unaryOutbound transport.UnaryOutbound
onewayOutbound transport.OnewayOutbound
)
// apply outbound middleware and create ValidatorOutbounds
if outs.Unary != nil {
unaryOutbound = middleware.ApplyUnaryOutboundMiddleware(outs.Unary, mw.Unary)
unaryOutbound = request.UnaryValidatorOutbound{UnaryOutbound: unaryOutbound}
}
if outs.Oneway != nil {
onewayOutbound = middleware.ApplyOnewayOutboundMiddleware(outs.Oneway, mw.Oneway)
onewayOutbound = request.OnewayValidatorOutbound{OnewayOutbound: outs.Oneway}
}
convertedOutbounds[service] = transport.Outbounds{
Unary: unaryOutbound,
Oneway: onewayOutbound,
}
}
return convertedOutbounds
}
// collectTransports iterates over all inbounds and outbounds and collects all
// of their unique underlying transports. Multiple inbounds and outbounds may
// share a transport, and we only want the dispatcher to manage their lifecycle
// once.
func collectTransports(inbounds Inbounds, outbounds Outbounds) []transport.Transport {
// Collect all unique transports from inbounds and outbounds.
transports := make(map[transport.Transport]struct{})
for _, inbound := range inbounds {
for _, transport := range inbound.Transports() {
transports[transport] = struct{}{}
}
}
for _, outbound := range outbounds {
if unary := outbound.Unary; unary != nil {
for _, transport := range unary.Transports() {
transports[transport] = struct{}{}
}
}
if oneway := outbound.Oneway; oneway != nil {
for _, transport := range oneway.Transports() {
transports[transport] = struct{}{}
}
}
}
keys := make([]transport.Transport, 0, len(transports))
for key := range transports {
keys = append(keys, key)
}
return keys
}
// dispatcher is the standard RPC implementation.
//
// It allows use of multiple Inbounds and Outbounds together.
type dispatcher struct {
transport.Registrar
Name string
inbounds Inbounds
outbounds Outbounds
transports []transport.Transport
InboundMiddleware InboundMiddleware
}
func (d dispatcher) Inbounds() Inbounds {
inbounds := make(Inbounds, len(d.inbounds))
copy(inbounds, d.inbounds)
return inbounds
}
func (d dispatcher) ClientConfig(service string) transport.ClientConfig {
if rs, ok := d.outbounds[service]; ok {
return clientconfig.MultiOutbound(d.Name, service, rs)
}
panic(noOutboundForService{Service: service})
}
func (d dispatcher) Register(rs []transport.Registrant) {
registrants := make([]transport.Registrant, 0, len(rs))
for _, r := range rs {
switch r.HandlerSpec.Type() {
case transport.Unary:
h := middleware.ApplyUnaryInboundMiddleware(r.HandlerSpec.Unary(),
d.InboundMiddleware.Unary)
r.HandlerSpec = transport.NewUnaryHandlerSpec(h)
case transport.Oneway:
h := middleware.ApplyOnewayInboundMiddleware(r.HandlerSpec.Oneway(),
d.InboundMiddleware.Oneway)
r.HandlerSpec = transport.NewOnewayHandlerSpec(h)
default:
panic(fmt.Sprintf("unknown handler type %q for service %q, procedure %q",
r.HandlerSpec.Type(), r.Service, r.Procedure))
}
registrants = append(registrants, r)
}
d.Registrar.Register(registrants)
}
// Start goes through the Transports, Outbounds and Inbounds and starts them
// *NOTE* there can be problems if we don't start these in a particular order
// The order should be: Transports -> Outbounds -> Inbounds
// If the Outbounds are started before the Transports we might get a network
// request before the Transports are ready.
// If the Inbounds are started before the Outbounds an Inbound request might
// hit an Outbound before that Outbound is ready to take requests
func (d dispatcher) Start() error {
var (
mu sync.Mutex
allStarted []StartStoppable
)
start := func(s StartStoppable) func() error {
return func() error {
if s == nil {
return nil
}
if err := s.Start(); err != nil {
return err
}
mu.Lock()
allStarted = append(allStarted, s)
mu.Unlock()
return nil
}
}
abort := func(errs []error) error {
// Failed to start so stop everything that was started.
wait := intsync.ErrorWaiter{}
for _, s := range allStarted {
wait.Submit(s.Stop)
}
if newErrors := wait.Wait(); len(newErrors) > 0 {
errs = append(errs, newErrors...)
}
return errors.ErrorGroup(errs)
}
// Start Transports
wait := intsync.ErrorWaiter{}
for _, t := range d.transports {
wait.Submit(start(t))
}
if errs := wait.Wait(); len(errs) != 0 {
return abort(errs)
}
// Start Outbounds
wait = intsync.ErrorWaiter{}
for _, o := range d.outbounds {
wait.Submit(start(o.Unary))
wait.Submit(start(o.Oneway))
}
if errs := wait.Wait(); len(errs) != 0 {
return abort(errs)
}
// Start Inbounds
wait = intsync.ErrorWaiter{}
for _, i := range d.inbounds {
i.SetRegistry(d)
wait.Submit(start(i))
}
if errs := wait.Wait(); len(errs) != 0 {
return abort(errs)
}
return nil
}
// Stop goes through the Transports, Outbounds and Inbounds and stops them
// *NOTE* there can be problems if we don't stop these in a particular order
// The order should be: Inbounds -> Outbounds -> Transports
// If the Outbounds are stopped before the Inbounds we might get a network
// request to a stopped Outbound from a still-going Inbound.
// If the Transports are stopped before the Outbounds the `peers` contained in
// the Outbound might be `deleted` from the Transports perspective and cause
// issues
func (d dispatcher) Stop() error {
var allErrs []error
// Stop Inbounds
wait := intsync.ErrorWaiter{}
for _, i := range d.inbounds {
wait.Submit(i.Stop)
}
if errs := wait.Wait(); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
// Stop Outbounds
wait = intsync.ErrorWaiter{}
for _, o := range d.outbounds {
if o.Unary != nil {
wait.Submit(o.Unary.Stop)
}
if o.Oneway != nil {
wait.Submit(o.Oneway.Stop)
}
}
if errs := wait.Wait(); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
// Stop Transports
wait = intsync.ErrorWaiter{}
for _, t := range d.transports {
wait.Submit(t.Stop)
}
if errs := wait.Wait(); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
if len(allErrs) > 0 {
return errors.ErrorGroup(allErrs)
}
return nil
}