forked from yarpc/yarpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.go
428 lines (381 loc) · 14.3 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright (c) 2018 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 (
"context"
"fmt"
"go.uber.org/multierr"
"go.uber.org/net/metrics"
"go.uber.org/yarpc/api/middleware"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/internal"
"go.uber.org/yarpc/internal/inboundmiddleware"
"go.uber.org/yarpc/internal/observability"
"go.uber.org/yarpc/internal/outboundmiddleware"
"go.uber.org/yarpc/internal/request"
"go.uber.org/yarpc/pkg/lifecycle"
"go.uber.org/zap"
)
// Inbounds contains a list of inbound transports. Each inbound transport
// specifies a source through which incoming requests are received.
type Inbounds []transport.Inbound
// Outbounds provides access to outbounds for a remote service. Outbounds
// define how requests are sent from this service to the remote service.
type Outbounds map[string]transport.Outbounds
// OutboundMiddleware contains the different types of outbound middlewares.
type OutboundMiddleware struct {
Unary middleware.UnaryOutbound
Oneway middleware.OnewayOutbound
Stream middleware.StreamOutbound
}
// InboundMiddleware contains the different types of inbound middlewares.
type InboundMiddleware struct {
Unary middleware.UnaryInbound
Oneway middleware.OnewayInbound
Stream middleware.StreamInbound
}
// RouterMiddleware wraps the Router middleware
type RouterMiddleware middleware.Router
// NewDispatcher builds a new Dispatcher using the specified Config. At
// minimum, a service name must be specified.
//
// Invalid configurations or errors in constructing the Dispatcher will cause
// panics.
func NewDispatcher(cfg Config) *Dispatcher {
if cfg.Name == "" {
panic("yarpc.NewDispatcher expects a service name")
}
if err := internal.ValidateServiceName(cfg.Name); err != nil {
panic("yarpc.NewDispatcher expects a valid service name: " + err.Error())
}
logger := cfg.Logging.logger(cfg.Name)
extractor := cfg.Logging.extractor()
meter, stopMeter := cfg.Metrics.scope(cfg.Name, logger)
cfg = addObservingMiddleware(cfg, meter, logger, extractor)
return &Dispatcher{
name: cfg.Name,
table: middleware.ApplyRouteTable(NewMapRouter(cfg.Name), cfg.RouterMiddleware),
inbounds: cfg.Inbounds,
outbounds: convertOutbounds(cfg.Outbounds, cfg.OutboundMiddleware),
transports: collectTransports(cfg.Inbounds, cfg.Outbounds),
inboundMiddleware: cfg.InboundMiddleware,
log: logger,
meter: meter,
stopMeter: stopMeter,
once: lifecycle.NewOnce(),
}
}
func addObservingMiddleware(cfg Config, meter *metrics.Scope, logger *zap.Logger, extractor observability.ContextExtractor) Config {
if cfg.DisableAutoObservabilityMiddleware {
return cfg
}
observer := observability.NewMiddleware(logger, meter, extractor)
cfg.InboundMiddleware.Unary = inboundmiddleware.UnaryChain(observer, cfg.InboundMiddleware.Unary)
cfg.InboundMiddleware.Oneway = inboundmiddleware.OnewayChain(observer, cfg.InboundMiddleware.Oneway)
cfg.InboundMiddleware.Stream = inboundmiddleware.StreamChain(observer, cfg.InboundMiddleware.Stream)
cfg.OutboundMiddleware.Unary = outboundmiddleware.UnaryChain(cfg.OutboundMiddleware.Unary, observer)
cfg.OutboundMiddleware.Oneway = outboundmiddleware.OnewayChain(cfg.OutboundMiddleware.Oneway, observer)
cfg.OutboundMiddleware.Stream = outboundmiddleware.StreamChain(cfg.OutboundMiddleware.Stream, observer)
return cfg
}
// convertOutbounds applies outbound middleware and creates validator outbounds
func convertOutbounds(outbounds Outbounds, mw OutboundMiddleware) Outbounds {
outboundSpecs := make(Outbounds, len(outbounds))
for outboundKey, outs := range outbounds {
if outs.Unary == nil && outs.Oneway == nil && outs.Stream == nil {
panic(fmt.Sprintf("no outbound set for outbound key %q in dispatcher", outboundKey))
}
var (
unaryOutbound transport.UnaryOutbound
onewayOutbound transport.OnewayOutbound
streamOutbound transport.StreamOutbound
)
serviceName := outboundKey
// apply outbound middleware and create ValidatorOutbounds
if outs.Unary != nil {
unaryOutbound = middleware.ApplyUnaryOutbound(outs.Unary, mw.Unary)
unaryOutbound = request.UnaryValidatorOutbound{UnaryOutbound: unaryOutbound}
}
if outs.Oneway != nil {
onewayOutbound = middleware.ApplyOnewayOutbound(outs.Oneway, mw.Oneway)
onewayOutbound = request.OnewayValidatorOutbound{OnewayOutbound: onewayOutbound}
}
if outs.Stream != nil {
streamOutbound = middleware.ApplyStreamOutbound(outs.Stream, mw.Stream)
streamOutbound = request.StreamValidatorOutbound{StreamOutbound: streamOutbound}
}
if outs.ServiceName != "" {
serviceName = outs.ServiceName
}
outboundSpecs[outboundKey] = transport.Outbounds{
ServiceName: serviceName,
Unary: unaryOutbound,
Oneway: onewayOutbound,
Stream: streamOutbound,
}
}
return outboundSpecs
}
// 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{}{}
}
}
if stream := outbound.Stream; stream != nil {
for _, transport := range stream.Transports() {
transports[transport] = struct{}{}
}
}
}
keys := make([]transport.Transport, 0, len(transports))
for key := range transports {
keys = append(keys, key)
}
return keys
}
// Dispatcher encapsulates a YARPC application. It acts as the entry point to
// send and receive YARPC requests in a transport and encoding agnostic way.
type Dispatcher struct {
table transport.RouteTable
name string
inbounds Inbounds
outbounds Outbounds
transports []transport.Transport
inboundMiddleware InboundMiddleware
log *zap.Logger
meter *metrics.Scope
stopMeter context.CancelFunc
once *lifecycle.Once
}
// 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.
func (d *Dispatcher) Inbounds() Inbounds {
inbounds := make(Inbounds, len(d.inbounds))
copy(inbounds, d.inbounds)
return inbounds
}
// Outbounds returns a copy of the list of outbounds for this RPC object.
func (d *Dispatcher) Outbounds() Outbounds {
outbounds := make(Outbounds, len(d.outbounds))
for k, v := range d.outbounds {
outbounds[k] = v
}
return outbounds
}
// ClientConfig provides the configuration needed to talk to the given
// service through an outboundKey. This configuration may be directly
// passed into encoding-specific RPC clients.
//
// keyvalueClient := json.New(dispatcher.ClientConfig("keyvalue"))
//
// This function panics if the outboundKey is not known.
func (d *Dispatcher) ClientConfig(outboundKey string) transport.ClientConfig {
return d.MustOutboundConfig(outboundKey)
}
// MustOutboundConfig provides the configuration needed to talk to the given
// service through an outboundKey. This configuration may be directly
// passed into encoding-specific RPC clients.
//
// keyvalueClient := json.New(dispatcher.MustOutboundConfig("keyvalue"))
//
// This function panics if the outboundKey is not known.
func (d *Dispatcher) MustOutboundConfig(outboundKey string) *transport.OutboundConfig {
if oc, ok := d.OutboundConfig(outboundKey); ok {
return oc
}
panic(fmt.Sprintf("no configured outbound transport for outbound key %q", outboundKey))
}
// OutboundConfig provides the configuration needed to talk to the given
// service through an outboundKey. This configuration may be directly
// passed into encoding-specific RPC clients.
//
// outboundConfig, ok := dispatcher.OutboundConfig("keyvalue")
// if !ok {
// // do something
// }
// keyvalueClient := json.New(outboundConfig)
func (d *Dispatcher) OutboundConfig(outboundKey string) (oc *transport.OutboundConfig, ok bool) {
if out, ok := d.outbounds[outboundKey]; ok {
return &transport.OutboundConfig{
CallerName: d.name,
Outbounds: out,
}, true
}
return nil, false
}
// InboundMiddleware returns the middleware applied to all inbound handlers.
// Router middleware and fallback handlers can use the InboundMiddleware to
// wrap custom handlers.
func (d *Dispatcher) InboundMiddleware() InboundMiddleware {
return d.inboundMiddleware
}
// Register registers zero or more procedures with this dispatcher. Incoming
// requests to these procedures will be routed to the handlers specified in
// the given Procedures.
func (d *Dispatcher) Register(rs []transport.Procedure) {
procedures := make([]transport.Procedure, 0, len(rs))
for _, r := range rs {
switch r.HandlerSpec.Type() {
case transport.Unary:
h := middleware.ApplyUnaryInbound(r.HandlerSpec.Unary(),
d.inboundMiddleware.Unary)
r.HandlerSpec = transport.NewUnaryHandlerSpec(h)
case transport.Oneway:
h := middleware.ApplyOnewayInbound(r.HandlerSpec.Oneway(),
d.inboundMiddleware.Oneway)
r.HandlerSpec = transport.NewOnewayHandlerSpec(h)
case transport.Streaming:
h := middleware.ApplyStreamInbound(r.HandlerSpec.Stream(),
d.inboundMiddleware.Stream)
r.HandlerSpec = transport.NewStreamHandlerSpec(h)
default:
panic(fmt.Sprintf("unknown handler type %q for service %q, procedure %q",
r.HandlerSpec.Type(), r.Service, r.Name))
}
procedures = append(procedures, r)
d.log.Info("Registration succeeded.", zap.Object("registeredProcedure", r))
}
d.table.Register(procedures)
}
// Start starts the Dispatcher, allowing it to accept and process new incoming
// requests. This starts all inbounds and outbounds configured on this
// Dispatcher.
//
// This function returns immediately after everything has been started.
// Servers should add a `select {}` to block to process all incoming requests.
//
// if err := dispatcher.Start(); err != nil {
// log.Fatal(err)
// }
// defer dispatcher.Stop()
//
// select {}
//
// Start and PhasedStart are mutually exclusive. See the PhasedStart
// documentation for details.
func (d *Dispatcher) Start() error {
starter := &PhasedStarter{
dispatcher: d,
log: d.log,
}
return d.once.Start(func() error {
d.log.Info("starting dispatcher")
starter.setRouters()
if err := starter.StartTransports(); err != nil {
return err
}
if err := starter.StartOutbounds(); err != nil {
return err
}
if err := starter.StartInbounds(); err != nil {
return err
}
d.log.Info("dispatcher startup complete")
return nil
})
}
// PhasedStart is a more granular alternative to Start, and is intended only
// for advanced users. Rather than starting all transports, inbounds, and
// outbounds at once, it lets the user start them separately.
//
// Start and PhasedStart are mutually exclusive. If Start is called first,
// PhasedStart is a no-op and returns the same error (if any) that Start
// returned. If PhasedStart is called first, Start is a no-op and always
// returns a nil error; the caller is responsible for using the PhasedStarter
// to complete startup.
func (d *Dispatcher) PhasedStart() (*PhasedStarter, error) {
starter := &PhasedStarter{
dispatcher: d,
log: d.log,
}
if err := d.once.Start(func() error {
starter.log.Info("beginning phased dispatcher start")
starter.setRouters()
return nil
}); err != nil {
return nil, err
}
return starter, nil
}
// Stop stops the Dispatcher, shutting down all inbounds, outbounds, and
// transports. This function returns after everything has been stopped.
//
// Stop and PhasedStop are mutually exclusive. See the PhasedStop
// documentation for details.
func (d *Dispatcher) Stop() error {
stopper := &PhasedStopper{
dispatcher: d,
log: d.log,
}
return d.once.Stop(func() error {
d.log.Info("shutting down dispatcher")
return multierr.Combine(
stopper.StopInbounds(),
stopper.StopOutbounds(),
stopper.StopTransports(),
)
})
}
// PhasedStop is a more granular alternative to Stop, and is intended only for
// advanced users. Rather than stopping all inbounds, outbounds, and
// transports at once, it lets the user stop them separately.
//
// Stop and PhasedStop are mutually exclusive. If Stop is called first,
// PhasedStop is a no-op and returns the same error (if any) that Stop
// returned. If PhasedStop is called first, Stop is a no-op and always returns
// a nil error; the caller is responsible for using the PhasedStopper to
// complete shutdown.
func (d *Dispatcher) PhasedStop() (*PhasedStopper, error) {
if err := d.once.Stop(func() error { return nil }); err != nil {
return nil, err
}
return &PhasedStopper{
dispatcher: d,
log: d.log,
}, nil
}
// Router returns the procedure router.
func (d *Dispatcher) Router() transport.Router {
return d.table
}
// Name returns the name of the dispatcher.
func (d *Dispatcher) Name() string {
return d.name
}