Skip to content

Commit d134659

Browse files
author
thekingofworld
committed
update highspeed map during each mutation
1 parent a58cae7 commit d134659

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

nsqlookupd/registration_db.go

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,50 @@ func NewRegistrationDB() *RegistrationDB {
6262
}
6363

6464
// update high speed registrations map
65-
// should call this func when registrationMap updated
66-
func (r *RegistrationDB) fillHighSpeedRegistrations() {
67-
r.highSpeedRegistrations = make(map[Registration]Registrations)
68-
for k := range r.registrationMap {
69-
key := Registration{k.Category, "*", ""}
70-
if _, ok := r.highSpeedRegistrations[key]; !ok {
71-
r.highSpeedRegistrations[key] = Registrations{}
65+
// should call this func when registrationMap add Registration
66+
func (r *RegistrationDB) addHighSpeedRegistration(k Registration) {
67+
key := Registration{k.Category, "*", ""}
68+
if _, ok := r.highSpeedRegistrations[key]; !ok {
69+
r.highSpeedRegistrations[key] = Registrations{}
70+
}
71+
if k.IsMatch(key.Category, key.Key, key.SubKey) {
72+
r.highSpeedRegistrations[key] = append(r.highSpeedRegistrations[key], k)
73+
}
74+
if k.SubKey != "" {
75+
subKey := Registration{k.Category, k.Key, "*"}
76+
if _, ok := r.highSpeedRegistrations[subKey]; !ok {
77+
r.highSpeedRegistrations[subKey] = Registrations{}
7278
}
73-
if k.IsMatch(key.Category, key.Key, key.SubKey) {
74-
r.highSpeedRegistrations[key] = append(r.highSpeedRegistrations[key], k)
79+
if k.IsMatch(subKey.Category, subKey.Key, subKey.SubKey) {
80+
r.highSpeedRegistrations[subKey] = append(r.highSpeedRegistrations[subKey], k)
7581
}
76-
if k.SubKey != "" {
77-
subKey := Registration{k.Category, k.Key, "*"}
78-
if _, ok := r.highSpeedRegistrations[subKey]; !ok {
79-
r.highSpeedRegistrations[subKey] = Registrations{}
82+
}
83+
}
84+
85+
// update high speed registrations map
86+
// should call this func when registrationMap remove Registration
87+
func (r *RegistrationDB) removeHighSpeedRegistration(k Registration) {
88+
key := Registration{k.Category, "*", ""}
89+
if registrations, ok := r.highSpeedRegistrations[key]; ok {
90+
for i, registration := range registrations {
91+
if registration == k {
92+
r.highSpeedRegistrations[key] = append(
93+
r.highSpeedRegistrations[key][:i], r.highSpeedRegistrations[key][i+1:]...,
94+
)
95+
break
8096
}
81-
if k.IsMatch(subKey.Category, subKey.Key, subKey.SubKey) {
82-
r.highSpeedRegistrations[subKey] = append(r.highSpeedRegistrations[subKey], k)
97+
}
98+
}
99+
if k.SubKey != "" {
100+
subKey := Registration{k.Category, k.Key, "*"}
101+
if registrations, ok := r.highSpeedRegistrations[subKey]; ok {
102+
for i, registration := range registrations {
103+
if registration == k {
104+
r.highSpeedRegistrations[subKey] = append(
105+
r.highSpeedRegistrations[subKey][:i], r.highSpeedRegistrations[subKey][i+1:]...,
106+
)
107+
break
108+
}
83109
}
84110
}
85111
}
@@ -92,7 +118,7 @@ func (r *RegistrationDB) AddRegistration(k Registration) {
92118
_, ok := r.registrationMap[k]
93119
if !ok {
94120
r.registrationMap[k] = make(map[string]*Producer)
95-
r.fillHighSpeedRegistrations()
121+
r.addHighSpeedRegistration(k)
96122
}
97123
}
98124

@@ -103,7 +129,7 @@ func (r *RegistrationDB) AddProducer(k Registration, p *Producer) bool {
103129
_, ok := r.registrationMap[k]
104130
if !ok {
105131
r.registrationMap[k] = make(map[string]*Producer)
106-
r.fillHighSpeedRegistrations()
132+
r.addHighSpeedRegistration(k)
107133
}
108134
producers := r.registrationMap[k]
109135
_, found := producers[p.peerInfo.id]
@@ -152,7 +178,7 @@ func (r *RegistrationDB) RemoveRegistration(k Registration) {
152178
r.Lock()
153179
defer r.Unlock()
154180
delete(r.registrationMap, k)
155-
r.fillHighSpeedRegistrations()
181+
r.removeHighSpeedRegistration(k)
156182
}
157183

158184
func (r *RegistrationDB) needFilter(key string, subkey string) bool {

0 commit comments

Comments
 (0)