-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryDiscovery.go
142 lines (123 loc) · 4.03 KB
/
MemoryDiscovery.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
package connect
import (
"context"
"github.com/pip-services4/pip-services4-go/pip-services4-components-go/config"
)
// MemoryDiscovery discovery service that keeps connections in memory.
//
// Configuration parameters
// [connection key 1]:
// ... connection parameters for key 1
// [connection key 2]:
// ... connection parameters for key N
// see IDiscovery
// see ConnectionParams
// Example
// config := NewConfigParamsFromTuples(
// "key1.host", "10.1.1.100",
// "key1.port", "8080",
// "key2.host", "10.1.1.100",
// "key2.port", "8082"
// );
// discovery := NewMemoryDiscovery();
// discovery.ReadConnections(config);
// conn, err := discovery.ResolveOne("123", "key1");
//
// Result: host=10.1.1.100;port=8080
type MemoryDiscovery struct {
items map[string][]*ConnectionParams
}
// NewEmptyMemoryDiscovery creates a new instance of discovery service.
//
// Returns: *MemoryDiscovery
func NewEmptyMemoryDiscovery() *MemoryDiscovery {
return &MemoryDiscovery{
items: map[string][]*ConnectionParams{},
}
}
// NewMemoryDiscovery creates a new instance of discovery service.
//
// Parameters:
// - ctx context.Context
// - config *config.ConfigParams configuration with connection parameters.
// Returns: *MemoryDiscovery
func NewMemoryDiscovery(ctx context.Context, config *config.ConfigParams) *MemoryDiscovery {
c := &MemoryDiscovery{
items: map[string][]*ConnectionParams{},
}
if config != nil {
c.Configure(ctx, config)
}
return c
}
// Configure component by passing configuration parameters.
//
// Parameters:
// - ctx context.Context
// - config *config.ConfigParams configuration parameters to be set.
func (c *MemoryDiscovery) Configure(ctx context.Context, config *config.ConfigParams) {
c.ReadConnections(config)
}
// ReadConnections from configuration parameters. Each section represents an individual Connectionparams
//
// Parameters:
// - ctx context.Context
// - config *configure.ConfigParams configuration parameters to be read
func (c *MemoryDiscovery) ReadConnections(config *config.ConfigParams) {
c.items = make(map[string][]*ConnectionParams)
if config.Len() > 0 {
connectionSections := config.GetSectionNames()
for _, key := range connectionSections {
connection := config.GetSection(key)
c.items[key] = []*ConnectionParams{NewConnectionParamsFromValue(connection)}
}
}
}
// Register connection parameters into the discovery service.
//
// Parameters:
// - transaction id to trace execution through call chain.
// - key string a key to uniquely identify the connection parameters.
// - connection *ConnectionParams
// Returns: *ConnectionParams, error registered connection or error.
func (c *MemoryDiscovery) Register(ctx context.Context, key string,
connection *ConnectionParams) (result *ConnectionParams, err error) {
if connection != nil {
connections, ok := c.items[key]
if ok && connections != nil {
connections = append(connections, connection)
} else {
connections = []*ConnectionParams{connection}
}
c.items[key] = connections
}
return connection, nil
}
// ResolveOne a single connection parameters by its key.
//
// Parameters:
// - ctx context.Context execution context to trace execution through call chain.
// - key: string a key to uniquely identify the connection.
// Returns: *ConnectionParams, error receives found connection or error.
func (c *MemoryDiscovery) ResolveOne(ctx context.Context,
key string) (result *ConnectionParams, err error) {
connections, _ := c.ResolveAll(ctx, key)
if len(connections) > 0 {
return connections[0], nil
}
return nil, nil
}
// ResolveAll connection parameters by its key.
//
// Parameters:
// - ctx context.Context execution context to trace execution through call chain.
// - key: string a key to uniquely identify the connection.
// Returns: *ConnectionParams, error receives found connection or error.
func (c *MemoryDiscovery) ResolveAll(ctx context.Context,
key string) (result []*ConnectionParams, err error) {
connections := c.items[key]
if connections == nil {
connections = []*ConnectionParams{}
}
return connections, nil
}