@@ -109,7 +109,10 @@ func (dir *registryDirectory) update(res *registry.ServiceEvent) {
109
109
}
110
110
111
111
func (dir * registryDirectory ) refreshInvokers (res * registry.ServiceEvent ) {
112
- var url * common.URL
112
+ var (
113
+ url * common.URL
114
+ oldInvoker protocol.Invoker = nil
115
+ )
113
116
//judge is override or others
114
117
if res != nil {
115
118
url = & res .Service
@@ -126,10 +129,10 @@ func (dir *registryDirectory) refreshInvokers(res *registry.ServiceEvent) {
126
129
switch res .Action {
127
130
case remoting .EventTypeAdd , remoting .EventTypeUpdate :
128
131
//dir.cacheService.EventTypeAdd(res.Path, dir.serviceTTL)
129
- dir .cacheInvoker (url )
132
+ oldInvoker = dir .cacheInvoker (url )
130
133
case remoting .EventTypeDel :
131
134
//dir.cacheService.EventTypeDel(res.Path, dir.serviceTTL)
132
- dir .uncacheInvoker (url )
135
+ oldInvoker = dir .uncacheInvoker (url )
133
136
logger .Infof ("selector delete service url{%s}" , res .Service )
134
137
default :
135
138
return
@@ -138,8 +141,14 @@ func (dir *registryDirectory) refreshInvokers(res *registry.ServiceEvent) {
138
141
139
142
newInvokers := dir .toGroupInvokers ()
140
143
dir .listenerLock .Lock ()
141
- defer dir .listenerLock .Unlock ()
142
144
dir .cacheInvokers = newInvokers
145
+ dir .listenerLock .Unlock ()
146
+ // After dir.cacheInvokers is updated,destroy the oldInvoker
147
+ // Ensure that no request will enter the oldInvoker
148
+ if oldInvoker != nil {
149
+ oldInvoker .Destroy ()
150
+ }
151
+
143
152
}
144
153
145
154
func (dir * registryDirectory ) toGroupInvokers () []protocol.Invoker {
@@ -177,12 +186,18 @@ func (dir *registryDirectory) toGroupInvokers() []protocol.Invoker {
177
186
return groupInvokersList
178
187
}
179
188
180
- func (dir * registryDirectory ) uncacheInvoker (url * common.URL ) {
189
+ // uncacheInvoker return abandoned Invoker,if no Invoker to be abandoned,return nil
190
+ func (dir * registryDirectory ) uncacheInvoker (url * common.URL ) protocol.Invoker {
181
191
logger .Debugf ("service will be deleted in cache invokers: invokers key is %s!" , url .Key ())
182
- dir .cacheInvokersMap .Delete (url .Key ())
192
+ if cacheInvoker , ok := dir .cacheInvokersMap .Load (url .Key ()); ok {
193
+ dir .cacheInvokersMap .Delete (url .Key ())
194
+ return cacheInvoker .(protocol.Invoker )
195
+ }
196
+ return nil
183
197
}
184
198
185
- func (dir * registryDirectory ) cacheInvoker (url * common.URL ) {
199
+ // cacheInvoker return abandoned Invoker,if no Invoker to be abandoned,return nil
200
+ func (dir * registryDirectory ) cacheInvoker (url * common.URL ) protocol.Invoker {
186
201
dir .overrideUrl (dir .GetDirectoryUrl ())
187
202
referenceUrl := dir .GetDirectoryUrl ().SubURL
188
203
@@ -193,7 +208,7 @@ func (dir *registryDirectory) cacheInvoker(url *common.URL) {
193
208
}
194
209
if url == nil {
195
210
logger .Error ("URL is nil ,pls check if service url is subscribe successfully!" )
196
- return
211
+ return nil
197
212
}
198
213
//check the url's protocol is equal to the protocol which is configured in reference config or referenceUrl is not care about protocol
199
214
if url .Protocol == referenceUrl .Protocol || referenceUrl .Protocol == "" {
@@ -210,10 +225,11 @@ func (dir *registryDirectory) cacheInvoker(url *common.URL) {
210
225
newInvoker := extension .GetProtocol (protocolwrapper .FILTER ).Refer (* newUrl )
211
226
if newInvoker != nil {
212
227
dir .cacheInvokersMap .Store (newUrl .Key (), newInvoker )
213
- cacheInvoker .(protocol.Invoker ). Destroy ( )
228
+ return cacheInvoker .(protocol.Invoker )
214
229
}
215
230
}
216
231
}
232
+ return nil
217
233
}
218
234
219
235
//select the protocol invokers from the directory
@@ -239,10 +255,11 @@ func (dir *registryDirectory) IsAvailable() bool {
239
255
func (dir * registryDirectory ) Destroy () {
240
256
//TODO:unregister & unsubscribe
241
257
dir .BaseDirectory .Destroy (func () {
242
- for _ , ivk := range dir .cacheInvokers {
258
+ invokers := dir .cacheInvokers
259
+ dir .cacheInvokers = []protocol.Invoker {}
260
+ for _ , ivk := range invokers {
243
261
ivk .Destroy ()
244
262
}
245
- dir .cacheInvokers = []protocol.Invoker {}
246
263
})
247
264
}
248
265
0 commit comments