forked from yarpc/yarpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configurator.go
312 lines (268 loc) · 9.63 KB
/
configurator.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
// 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 yarpcconfig
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"go.uber.org/multierr"
"go.uber.org/yarpc"
"go.uber.org/yarpc/internal/config"
"go.uber.org/yarpc/internal/interpolate"
"gopkg.in/yaml.v2"
)
// Configurator helps build Dispatchers using runtime configuration.
//
// A new Configurator does not know about any transports, peer lists, or peer
// list updaters. Inform it about them by using the RegisterTransport,
// RegisterPeerList, and RegisterPeerListUpdater functions, or their Must*
// variants.
type Configurator struct {
knownTransports map[string]*compiledTransportSpec
knownPeerLists map[string]*compiledPeerListSpec
knownPeerListUpdaters map[string]*compiledPeerListUpdaterSpec
resolver interpolate.VariableResolver
}
// New sets up a new empty Configurator. The returned Configurator does not
// know about any Transports, peer lists, or peer list updaters.
func New(opts ...Option) *Configurator {
c := &Configurator{
knownTransports: make(map[string]*compiledTransportSpec),
knownPeerLists: make(map[string]*compiledPeerListSpec),
knownPeerListUpdaters: make(map[string]*compiledPeerListUpdaterSpec),
resolver: os.LookupEnv,
}
for _, opt := range opts {
opt(c)
}
return c
}
// RegisterTransport registers a TransportSpec with the Configurator, teaching
// it how to load configuration and build inbounds and outbounds for that
// transport.
//
// An error is returned if the TransportSpec is invalid. Use
// MustRegisterTransport if you want to panic in case of registration failure.
//
// If a transport with the same name already exists, it will be replaced.
//
// See TransportSpec for details on how to integrate your own transport with
// the system.
func (c *Configurator) RegisterTransport(t TransportSpec) error {
if t.Name == "" {
return errors.New("name is required")
}
spec, err := compileTransportSpec(&t)
if err != nil {
return fmt.Errorf("invalid TransportSpec for %q: %v", t.Name, err)
}
c.knownTransports[t.Name] = spec
return nil
}
// MustRegisterTransport registers the given TransportSpec with the
// Configurator. This function panics if the TransportSpec is invalid.
func (c *Configurator) MustRegisterTransport(t TransportSpec) {
if err := c.RegisterTransport(t); err != nil {
panic(err)
}
}
// RegisterPeerList registers a PeerListSpec with the given Configurator,
// teaching it how to build peer lists of this kind from configuration.
//
// An error is returned if the PeerListSpec is invalid. Use
// MustRegisterPeerList to panic in the case of registration failure.
//
// If a peer list with the same name already exists, it will be replaced.
//
// See PeerListSpec for details on how to integrate your own peer list with
// the system.
func (c *Configurator) RegisterPeerList(s PeerListSpec) error {
if s.Name == "" {
return errors.New("name is required")
}
spec, err := compilePeerListSpec(&s)
if err != nil {
return fmt.Errorf("invalid PeerListSpec for %q: %v", s.Name, err)
}
c.knownPeerLists[s.Name] = spec
return nil
}
// MustRegisterPeerList registers the given PeerListSpec with the Configurator.
// This function panics if the PeerListSpec is invalid.
func (c *Configurator) MustRegisterPeerList(s PeerListSpec) {
if err := c.RegisterPeerList(s); err != nil {
panic(err)
}
}
// RegisterPeerListUpdater registers a PeerListUpdaterSpec with the given
// Configurator, teaching it how to build peer list updaters of this kind from
// configuration.
//
// Returns an error if the PeerListUpdaterSpec is invalid. Use
// MustRegisterPeerListUpdater to panic if the registration fails.
//
// If a peer list updater with the same name already exists, it will be
// replaced.
//
// See PeerListUpdaterSpec for details on how to integrate your own peer list
// updater with the system.
func (c *Configurator) RegisterPeerListUpdater(s PeerListUpdaterSpec) error {
if s.Name == "" {
return errors.New("name is required")
}
spec, err := compilePeerListUpdaterSpec(&s)
if err != nil {
return fmt.Errorf("invalid PeerListUpdaterSpec for %q: %v", s.Name, err)
}
c.knownPeerListUpdaters[s.Name] = spec
return nil
}
// MustRegisterPeerListUpdater registers the given PeerListUpdaterSpec with
// the Configurator. This function panics if the PeerListUpdaterSpec is
// invalid.
func (c *Configurator) MustRegisterPeerListUpdater(s PeerListUpdaterSpec) {
if err := c.RegisterPeerListUpdater(s); err != nil {
panic(err)
}
}
// LoadConfigFromYAML loads a yarpc.Config from YAML data. Use LoadConfig if
// you have already parsed a map[string]interface{} or
// map[interface{}]interface{}.
func (c *Configurator) LoadConfigFromYAML(serviceName string, r io.Reader) (yarpc.Config, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return yarpc.Config{}, err
}
var data map[string]interface{}
if err := yaml.Unmarshal(b, &data); err != nil {
return yarpc.Config{}, err
}
return c.LoadConfig(serviceName, data)
}
// LoadConfig loads a yarpc.Config from a map[string]interface{} or
// map[interface{}]interface{}.
//
// See the module documentation for the shape the map[string]interface{} is
// expected to conform to.
func (c *Configurator) LoadConfig(serviceName string, data interface{}) (yarpc.Config, error) {
var cfg yarpcConfig
if err := config.DecodeInto(&cfg, data); err != nil {
return yarpc.Config{}, err
}
return c.load(serviceName, &cfg)
}
// NewDispatcherFromYAML builds a Dispatcher from the given YAML
// configuration.
func (c *Configurator) NewDispatcherFromYAML(serviceName string, r io.Reader) (*yarpc.Dispatcher, error) {
cfg, err := c.LoadConfigFromYAML(serviceName, r)
if err != nil {
return nil, err
}
return yarpc.NewDispatcher(cfg), nil
}
// NewDispatcher builds a new Dispatcher from the given configuration data.
func (c *Configurator) NewDispatcher(serviceName string, data interface{}) (*yarpc.Dispatcher, error) {
cfg, err := c.LoadConfig(serviceName, data)
if err != nil {
return nil, err
}
return yarpc.NewDispatcher(cfg), nil
}
func (c *Configurator) load(serviceName string, cfg *yarpcConfig) (_ yarpc.Config, err error) {
b := newBuilder(serviceName, &Kit{name: serviceName, c: c, resolver: c.resolver})
for _, inbound := range cfg.Inbounds {
if e := c.loadInboundInto(b, inbound); e != nil {
err = multierr.Append(err, e)
}
}
for name, outboundConfig := range cfg.Outbounds {
if e := c.loadOutboundInto(b, name, outboundConfig); e != nil {
err = multierr.Append(err, e)
}
}
for name, attrs := range cfg.Transports {
if e := c.loadTransportInto(b, name, attrs); e != nil {
err = multierr.Append(err, e)
}
}
if err != nil {
return yarpc.Config{}, err
}
return b.Build()
}
func (c *Configurator) loadInboundInto(b *builder, i inbound) error {
if i.Disabled {
return nil
}
spec, err := c.spec(i.Type)
if err != nil {
// TODO: Maybe we should keep track of the inbound name so that if
// it differs from the transport name, we can mention that in the
// error message.
return fmt.Errorf("failed to load inbound: %v", err)
}
return b.AddInboundConfig(spec, i.Attributes)
}
func (c *Configurator) loadOutboundInto(b *builder, name string, cfg outbounds) error {
// This matches the signature of builder.AddImplicitOutbound,
// AddUnaryOutbound and AddOnewayOutbound
type adder func(*compiledTransportSpec, string, string, config.AttributeMap) error
loadUsing := func(o *outbound, adder adder) error {
spec, err := c.spec(o.Type)
if err != nil {
return fmt.Errorf("failed to load configuration for outbound %q: %v", name, err)
}
if err := adder(spec, name, cfg.Service, o.Attributes); err != nil {
return fmt.Errorf("failed to add outbound %q: %v", name, err)
}
return nil
}
if implicit := cfg.Implicit; implicit != nil {
return loadUsing(implicit, b.AddImplicitOutbound)
}
if unary := cfg.Unary; unary != nil {
if err := loadUsing(unary, b.AddUnaryOutbound); err != nil {
return err
}
}
if oneway := cfg.Oneway; oneway != nil {
if err := loadUsing(oneway, b.AddOnewayOutbound); err != nil {
return err
}
}
return nil
}
func (c *Configurator) loadTransportInto(b *builder, name string, attrs config.AttributeMap) error {
spec, err := c.spec(name)
if err != nil {
return fmt.Errorf("failed to load configuration for transport %q: %v", name, err)
}
return b.AddTransportConfig(spec, attrs)
}
// Returns the compiled spec for the transport with the given name or an error
func (c *Configurator) spec(name string) (*compiledTransportSpec, error) {
spec, ok := c.knownTransports[name]
if !ok {
return nil, fmt.Errorf("unknown transport %q", name)
}
return spec, nil
}