-
Notifications
You must be signed in to change notification settings - Fork 2
/
dictionary.go
executable file
·221 lines (196 loc) · 5.91 KB
/
dictionary.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
// Copyright 2016 IBM Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//Package goEurekaClient Implements a go client that interacts with a eureka server
package goEurekaClient
type instanceMap map[string]map[string]*Instance
// Dictionary interface defines the methods to be used for the eureka client dictionary
type Dictionary interface {
Delete(inst *Instance, id string, app *Application)
Add(inst *Instance, id string, app *Application)
Update(inst *Instance, id string, app *Application)
getApplication(appName string) *Application
getApplications() []*Application
GetInstancesByVip(vipAddress string) []*Instance
GetInstancesBySecVip(svipAddress string) []*Instance
isEmpty() bool
copyDictionary() dictionary
}
type dictionary struct {
appNameIndex instanceMap
vipIndex instanceMap
svipIndex instanceMap
}
func newDictionary() dictionary {
return dictionary{appNameIndex: instanceMap{}, vipIndex: instanceMap{},
svipIndex: instanceMap{}}
}
func (d *dictionary) Delete(inst *Instance, id string, app *Application) {
if vipInsts, ok := d.vipIndex[inst.VIPAddr]; ok {
delete(vipInsts, id)
if len(vipInsts) == 0 {
delete(d.vipIndex, inst.VIPAddr)
}
}
if svipInsts, ok := d.svipIndex[inst.SecVIPAddr]; ok {
delete(d.svipIndex[inst.SecVIPAddr], id)
if len(svipInsts) == 0 {
delete(d.svipIndex, inst.SecVIPAddr)
}
}
if appInsts, ok := d.appNameIndex[app.Name]; ok {
delete(appInsts, id)
if len(appInsts) == 0 {
delete(d.appNameIndex, app.Name)
}
}
}
func (d *dictionary) Add(inst *Instance, id string, app *Application) {
if inst.VIPAddr != "" {
insts := d.vipIndex[inst.VIPAddr]
if insts == nil {
insts = map[string]*Instance{}
d.vipIndex[inst.VIPAddr] = insts
d.vipIndex[inst.VIPAddr][inst.ID] = inst
} else {
d.vipIndex[inst.VIPAddr][inst.ID] = inst
}
}
if inst.SecVIPAddr != "" {
if d.svipIndex[inst.SecVIPAddr] == nil {
d.svipIndex[inst.SecVIPAddr] = map[string]*Instance{}
d.svipIndex[inst.SecVIPAddr][inst.ID] = inst
} else {
d.svipIndex[inst.SecVIPAddr][inst.ID] = inst
}
}
if inst.Application != "" {
if d.appNameIndex[app.Name] == nil {
d.appNameIndex[app.Name] = map[string]*Instance{}
d.appNameIndex[app.Name][inst.ID] = inst
} else {
d.appNameIndex[app.Name][inst.ID] = inst
}
}
}
func (d *dictionary) Update(inst *Instance, id string, app *Application) {
if inst.VIPAddr != "" {
insts := d.vipIndex[inst.VIPAddr]
if insts == nil {
insts = map[string]*Instance{}
d.vipIndex[inst.VIPAddr] = insts
d.vipIndex[inst.VIPAddr][inst.ID] = inst
} else {
d.vipIndex[inst.VIPAddr][inst.ID] = inst
}
}
if inst.SecVIPAddr != "" {
if d.svipIndex[inst.SecVIPAddr] == nil {
d.svipIndex[inst.SecVIPAddr] = map[string]*Instance{}
d.svipIndex[inst.SecVIPAddr][inst.ID] = inst
} else {
d.svipIndex[inst.SecVIPAddr][inst.ID] = inst
}
}
if inst.Application != "" {
if d.appNameIndex[app.Name] == nil {
d.appNameIndex[app.Name] = map[string]*Instance{}
d.appNameIndex[app.Name][inst.ID] = inst
} else {
d.appNameIndex[app.Name][inst.ID] = inst
}
}
}
func (d *dictionary) getApplication(appName string) *Application {
instancesMap, ok := d.appNameIndex[appName]
if !ok {
return nil
}
var instancesArray []*Instance
for _, v := range instancesMap {
dc := v.deepCopy()
instancesArray = append(instancesArray, &dc)
}
app := &Application{Name: appName,
Instances: instancesArray}
return app
}
func (d *dictionary) getApplications() []*Application {
var applicationsArray []*Application
for appName, instancesMap := range d.appNameIndex {
var instancesArray []*Instance
for _, v := range instancesMap {
dc := v.deepCopy()
instancesArray = append(instancesArray, &dc)
}
app := &Application{Name: appName,
Instances: instancesArray}
applicationsArray = append(applicationsArray, app)
}
return applicationsArray
}
func (d *dictionary) GetInstancesByVip(vipAddress string) []*Instance {
instancesMap, ok := d.vipIndex[vipAddress]
if !ok {
return nil
}
var instancesArray []*Instance
for _, v := range instancesMap {
dc := v.deepCopy()
instancesArray = append(instancesArray, &dc)
}
return instancesArray
}
func (d *dictionary) GetInstancesBySecVip(svipAddress string) []*Instance {
instancesMap, ok := d.svipIndex[svipAddress]
if !ok {
return nil
}
var instancesArray []*Instance
for _, v := range instancesMap {
dc := v.deepCopy()
instancesArray = append(instancesArray, &dc)
}
return instancesArray
}
func (d *dictionary) isEmpty() bool {
if len(d.appNameIndex) == 0 && len(d.svipIndex) == 0 && len(d.vipIndex) == 0 {
return true
}
return false
}
func (d *dictionary) copyDictionary() *dictionary {
dict := dictionary{appNameIndex: map[string]map[string]*Instance{}, svipIndex: map[string]map[string]*Instance{}, vipIndex: map[string]map[string]*Instance{}}
for appIndex, insts := range d.appNameIndex {
copyInsts := map[string]*Instance{}
for instName, inst := range insts {
copyInsts[instName] = inst
}
dict.appNameIndex[appIndex] = copyInsts
}
for vipIndex, insts := range d.vipIndex {
copyInsts := map[string]*Instance{}
for instName, inst := range insts {
copyInsts[instName] = inst
}
dict.vipIndex[vipIndex] = copyInsts
}
for svipIndex, insts := range d.svipIndex {
copyInsts := map[string]*Instance{}
for instName, inst := range insts {
copyInsts[instName] = inst
}
dict.svipIndex[svipIndex] = copyInsts
}
return &dict
}