-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectionResolver.go
288 lines (246 loc) · 8.5 KB
/
ConnectionResolver.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
package connect
import (
"context"
"github.com/pip-services4/pip-services4-go/pip-services4-components-go/config"
"github.com/pip-services4/pip-services4-go/pip-services4-components-go/refer"
)
// ConnectionResolver helper class to retrieve component connections.
// If connections are configured to be retrieved from IDiscovery, it automatically locates
// IDiscovery in component references and retrieve connections from there using discovery_key parameter.
//
// Configuration parameters
// - connection:
// - discovery_key: (optional) a key to retrieve the connection from IDiscovery
// - ... other connection parameters
// - connections: alternative to connection
// - [connection params 1]: first connection parameters
// - ... connection parameters for key 1
// - [connection params N]: Nth connection parameters
// - ... connection parameters for key N
// References:
// - *:discovery:*:*:1.0 (optional) IDiscovery services to resolve connections
//
// see ConnectionParams
// see IDiscovery
//
// Example:
// config = NewConfigParamsFromTuples(
// "connection.host", "10.1.1.100",
// "connection.port", 8080
// );
// connectionResolver := NewConnectionResolver();
// connectionResolver.Configure(context.Background(), config);
// connectionResolver.SetReferences(context.Background(), references);
// res, err := connectionResolver.Resolve("123");
type ConnectionResolver struct {
connections []*ConnectionParams
references refer.IReferences
}
// NewEmptyConnectionResolver creates a new instance of connection resolver.
//
// Returns: *ConnectionResolver
func NewEmptyConnectionResolver() *ConnectionResolver {
return &ConnectionResolver{
connections: []*ConnectionParams{},
references: nil,
}
}
// NewConnectionResolver Creates a new instance of connection resolver.
//
// Parameters:
// - ctx context.Context
// - config *config.ConfigParams component configuration parameters
// - references refer.IReferences component references
// Returns: *ConnectionResolver
func NewConnectionResolver(ctx context.Context, config *config.ConfigParams, references refer.IReferences) *ConnectionResolver {
c := &ConnectionResolver{
connections: []*ConnectionParams{},
references: references,
}
if config != nil {
c.Configure(ctx, config)
}
return c
}
// Configure Configures component by passing configuration parameters.
//
// Parameters:
// - ctx context.Context
// - config *config.ConfigParams configuration parameters to be set.
func (c *ConnectionResolver) Configure(ctx context.Context, config *config.ConfigParams) {
connections := NewManyConnectionParamsFromConfig(config)
c.connections = append(c.connections, connections...)
}
// SetReferences sets references to dependent components.
//
// Parameters:
// - ctx context.Context
// - references refer.IReferences references to locate the component dependencies.
func (c *ConnectionResolver) SetReferences(ctx context.Context, references refer.IReferences) {
c.references = references
}
// GetAll gets all connections configured in component configuration.
// Redirect to Discovery services is not done at this point.
// If you need fully fleshed connection use resolve method instead.
//
// Returns: []*ConnectionParams a list with connection parameters
func (c *ConnectionResolver) GetAll() []*ConnectionParams {
return c.connections
}
// Add a new connection to component connections
//
// Parameters:
// - connection *ConnectionParams new connection parameters to be added
func (c *ConnectionResolver) Add(connection *ConnectionParams) {
c.connections = append(c.connections, connection)
}
func (c *ConnectionResolver) resolveInDiscovery(ctx context.Context,
connection *ConnectionParams) (result *ConnectionParams, err error) {
if !connection.UseDiscovery() {
return connection, nil
}
key := connection.DiscoveryKey()
if c.references == nil {
return nil, nil
}
discoveryDescriptor := refer.NewDescriptor("*", "discovery", "*", "*", "*")
components := c.references.GetOptional(discoveryDescriptor)
if len(components) == 0 {
err := refer.NewReferenceError(ctx, discoveryDescriptor)
return nil, err
}
for _, component := range components {
if discovery, ok := component.(IDiscovery); ok && discovery != nil {
connection, err = discovery.ResolveOne(ctx, key)
if connection != nil || err != nil {
return connection, err
}
}
}
return nil, nil
}
// Resolve a single component connection. If connections are configured to be retrieved
// from Discovery service it finds a IDiscovery and resolves the connection there.
//
// see IDiscovery
// Parameters:
// - ctx context.Context execution context to trace execution through call chain.
// Returns: *ConnectionParams, error resolved connection or error.
func (c *ConnectionResolver) Resolve(ctx context.Context) (*ConnectionParams, error) {
if len(c.connections) == 0 {
return nil, nil
}
resolveConnections := make([]*ConnectionParams, 0)
for _, connection := range c.connections {
if !connection.UseDiscovery() {
return connection, nil
}
resolveConnections = append(resolveConnections, connection)
}
for _, connection := range resolveConnections {
c, err := c.resolveInDiscovery(ctx, connection)
if c != nil || err != nil {
return c, err
}
}
return nil, nil
}
func (c *ConnectionResolver) resolveAllInDiscovery(ctx context.Context,
connection *ConnectionParams) (result []*ConnectionParams, err error) {
if !connection.UseDiscovery() {
return []*ConnectionParams{connection}, nil
}
key := connection.DiscoveryKey()
if c.references == nil {
return nil, nil
}
discoveryDescriptor := refer.NewDescriptor("*", "discovery", "*", "*", "*")
components := c.references.GetOptional(discoveryDescriptor)
if len(components) == 0 {
err := refer.NewReferenceError(ctx, discoveryDescriptor)
return nil, err
}
resolvedConnections := make([]*ConnectionParams, 0)
for _, component := range components {
if discovery, ok := component.(IDiscovery); ok && discovery != nil {
connections, err := discovery.ResolveAll(ctx, key)
if err != nil {
return nil, err
}
if connections != nil {
for _, c := range connections {
resolvedConnections = append(resolvedConnections, c)
}
}
}
}
return resolvedConnections, nil
}
// ResolveAll resolves all component connection. If connections are configured to be
// retrieved from Discovery service it finds a IDiscovery and resolves the connection there.
//
// see IDiscovery
// Parameters:
// - transaction id to trace execution through call chain.
// Returns: []*ConnectionParams, error resolved connections or error.
func (c *ConnectionResolver) ResolveAll(ctx context.Context) ([]*ConnectionParams, error) {
resolvedConnections := make([]*ConnectionParams, 0)
resolveConnections := make([]*ConnectionParams, 0)
for _, connection := range c.connections {
if !connection.UseDiscovery() {
resolvedConnections = append(resolvedConnections, connection)
} else {
resolveConnections = append(resolveConnections, connection)
}
}
for _, connection := range resolveConnections {
connections, err := c.resolveAllInDiscovery(ctx, connection)
if err != nil {
return nil, err
}
resolvedConnections = append(resolvedConnections, connections...)
}
return resolvedConnections, nil
}
func (c *ConnectionResolver) registerInDiscovery(ctx context.Context,
connection *ConnectionParams) (result bool, err error) {
if !connection.UseDiscovery() {
return false, nil
}
key := connection.DiscoveryKey()
if c.references == nil {
return false, nil
}
discoveryDescriptor := refer.NewDescriptor("*", "discovery", "*", "*", "*")
components := c.references.GetOptional(discoveryDescriptor)
if len(components) == 0 {
err := refer.NewReferenceError(ctx, discoveryDescriptor)
return false, err
}
registered := false
for _, component := range components {
if discovery, ok := component.(IDiscovery); ok && discovery != nil {
_, err = discovery.Register(ctx, key, connection)
if err != nil {
return false, err
}
registered = true
}
}
return registered, nil
}
// Register the given connection in all referenced discovery services.
// This method can be used for dynamic service discovery.
//
// see IDiscovery
// Parameters:
// - transaction id to trace execution through call chain.
// - connection *ConnectionParams a connection to register.
// Returns: error
func (c *ConnectionResolver) Register(ctx context.Context, connection *ConnectionParams) error {
registered, err := c.registerInDiscovery(ctx, connection)
if registered {
c.connections = append(c.connections, connection)
}
return err
}