/
registry_common.go
302 lines (272 loc) · 6.65 KB
/
registry_common.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
package lox
import (
"sync"
"time"
"github.com/nomos/go-lokas"
"github.com/nomos/go-lokas/protocol"
"github.com/nomos/go-lokas/util"
)
func NewCommonRegistry() *CommonRegistry {
ret := &CommonRegistry{
Processes: map[util.ProcessId]*ProcessRegistry{},
Services: map[string]map[uint16]*ServiceRegistry{},
Actors: map[util.ID]*ActorRegistry{},
ActorsByType: map[string][]util.ID{},
ActorsByServer: map[int32][]util.ID{},
Ts: time.Time{},
}
return ret
}
type CommonRegistry struct {
Processes map[util.ProcessId]*ProcessRegistry
Services map[string]map[uint16]*ServiceRegistry
Actors map[util.ID]*ActorRegistry
ActorsByType map[string][]util.ID
ActorsByServer map[int32][]util.ID
Ts time.Time
mu sync.RWMutex
}
func (this *CommonRegistry) GetActorRegistry(id util.ID) *ActorRegistry {
this.mu.RLock()
defer this.mu.RUnlock()
return this.Actors[id]
}
func (this *CommonRegistry) GetActorIdsByTypeAndServerId(serverId int32, typ string) []util.ID {
this.mu.Lock()
defer this.mu.Unlock()
ret := []util.ID{}
serverIds, ok := this.ActorsByServer[serverId]
if !ok {
return ret
}
typeIds, ok := this.ActorsByType[typ]
if !ok {
return ret
}
for _, a := range serverIds {
for _, b := range typeIds {
if a == b {
ret = append(ret, a)
}
}
}
return ret
}
func removeIdFromArr(id util.ID, arr []util.ID) []util.ID {
ret := []util.ID{}
for _, v := range arr {
if v != id {
ret = append(ret, v)
}
}
return ret
}
func addId2ArrOnce(id util.ID, arr []util.ID) []util.ID {
for _, v := range arr {
if v == id {
return arr
}
}
arr = append(arr, id)
return arr
}
func (this *CommonRegistry) AddActor(actor *ActorRegistry) {
this.mu.Lock()
defer this.mu.Unlock()
this.Actors[actor.Id] = actor
if actorArr, ok := this.ActorsByType[actor.Type]; ok {
actorArr = addId2ArrOnce(actor.Id, actorArr)
this.ActorsByType[actor.Type] = actorArr
} else {
this.ActorsByType[actor.Type] = []util.ID{actor.Id}
}
if actorArr, ok := this.ActorsByServer[actor.ServerId]; ok {
actorArr = addId2ArrOnce(actor.Id, actorArr)
this.ActorsByServer[actor.ServerId] = actorArr
} else {
this.ActorsByServer[actor.ServerId] = []util.ID{actor.Id}
}
}
func (this *CommonRegistry) RemoveActor(actorId util.ID) {
this.mu.Lock()
defer this.mu.Unlock()
if actor, ok := this.Actors[actorId]; ok {
delete(this.Actors, actorId)
if actorArr, ok1 := this.ActorsByType[actor.Type]; ok1 {
this.ActorsByType[actor.Type] = removeIdFromArr(actor.Id, actorArr)
}
if actorArr, ok1 := this.ActorsByServer[actor.ServerId]; ok1 {
this.ActorsByServer[actor.ServerId] = removeIdFromArr(actor.Id, actorArr)
}
}
}
func (this *CommonRegistry) AddProcess(process *ProcessRegistry) {
this.mu.Lock()
defer this.mu.Unlock()
this.Processes[process.Id] = process
}
func (this *CommonRegistry) RemoveProcess(id util.ProcessId) {
this.mu.Lock()
defer this.mu.Unlock()
delete(this.Processes, id)
}
func (this *CommonRegistry) AddService(service *ServiceRegistry) {
this.mu.Lock()
defer this.mu.Unlock()
if _, ok := this.Services[service.ServiceType]; !ok {
this.Services[service.ServiceType] = make(map[uint16]*ServiceRegistry)
}
this.Services[service.ServiceType][service.ServiceId] = service
}
func (this *CommonRegistry) RemoveService(serviceType string, serviceId uint16) {
this.mu.Lock()
defer this.mu.Unlock()
delete(this.Services[serviceType], serviceId)
}
// service
type ServiceRegistry struct {
// Id uint32
ServiceType string
ServiceId uint16
// GameId string
Host string
Port uint32
Version string
Cnt uint32
ProcessId util.ProcessId
CreateAt time.Time
// Weights map[util.ID]int
// Ts time.Time
}
// type ServiceInfo struct {
// // Id uint32
// ServiceType string
// ServiceId uint16
// // GameId string
// Host string
// Port uint32
// Version string
// Cnt uint32
// ProcessId util.ProcessId
// CreateAt time.Time
// // Weights map[util.ID]int
// // Ts time.Time
// }
func NewServiceRegistry(serviceType string, serviceId uint16) *ServiceRegistry {
return &ServiceRegistry{
// Id: uint32(serviceId) * 100,
ServiceType: serviceType,
ServiceId: serviceId,
Host: "",
Port: 0,
Version: "",
Cnt: 0,
CreateAt: time.Time{},
}
}
type ActorRegistry struct {
Id util.ID
ProcessId util.ProcessId
Type string
GameId string
Version string
ServerId int32
//Health lokas.ActorState
Ts time.Time
}
func NewActorRegistry(id util.ID) *ActorRegistry {
ret := &ActorRegistry{
Id: id,
ProcessId: 0,
Type: "",
GameId: "",
Version: "",
ServerId: 0,
Ts: time.Time{},
}
return ret
}
type ProcessRegistry struct {
Id util.ProcessId
GameId string
Version string
ServerId int32
Host string
Port string
Services map[protocol.BINARY_TAG]*lokas.Service
Actors map[util.ID]*ActorRegistry
Ts time.Time
}
func NewProcessRegistry(id util.ProcessId) *ProcessRegistry {
ret := &ProcessRegistry{
Id: id,
GameId: "",
Version: "",
ServerId: 0,
Host: "",
Port: "",
Services: nil,
Actors: nil,
Ts: time.Time{},
}
return ret
}
type ProcessActorsInfo struct {
Id util.ProcessId
Actors []util.ID
Ts time.Time
}
func CreateProcessActorsInfo(process lokas.IProcess) *ProcessActorsInfo {
ret := &ProcessActorsInfo{
Id: process.PId(),
Actors: process.GetActorIds(),
Ts: time.Now(),
}
return ret
}
type ActorRegistryInfo struct {
Id util.ID
Type string
ProcessId util.ProcessId
GameId string
Version string
ServerId int32
Ts time.Time
}
func CreateActorRegistryInfo(actor lokas.IActor) *ActorRegistryInfo {
ret := &ActorRegistryInfo{
Id: actor.GetId(),
Type: actor.Type(),
ProcessId: actor.GetProcess().PId(),
GameId: actor.GetProcess().GameId(),
Version: actor.GetProcess().Version(),
ServerId: actor.GetProcess().ServerId(),
Ts: time.Now(),
}
return ret
}
type ProcessServiceInfo struct {
Id util.ProcessId
Services map[protocol.BINARY_TAG]int
}
type ProcessRegistryInfo struct {
Id util.ProcessId
GameId string
Version string
ServerId int32
Host string
Port string
Ts time.Time
}
func CreateProcessRegistryInfo(process lokas.IProcess) *ProcessRegistryInfo {
ret := &ProcessRegistryInfo{
Id: process.PId(),
GameId: process.GameId(),
Version: process.Version(),
ServerId: process.ServerId(),
Host: process.Config().GetString("host"),
Port: process.Config().GetString("port"),
Ts: time.Now(),
}
return ret
}