@@ -27,7 +27,7 @@ import (
27
27
"github.com/envoyproxy/go-control-plane/pkg/server/stream/v3"
28
28
)
29
29
30
- type watches = map [chan Response ]struct {}
30
+ type watches = map [chan Response ]stream. StreamState
31
31
32
32
// LinearCache supports collections of opaque resources. This cache has a
33
33
// single collection indexed by resource names and manages resource versions
@@ -143,15 +143,31 @@ func (cache *LinearCache) respond(value chan Response, staleResources []string)
143
143
}
144
144
}
145
145
146
- func (cache * LinearCache ) notifyAll (modified map [string ]struct {}) {
146
+ func (cache * LinearCache ) notifyAll (modified map [string ]struct {}, fromDeletion bool ) {
147
147
// de-duplicate watches that need to be responded
148
148
notifyList := make (map [chan Response ][]string )
149
- for name := range modified {
150
- for watch := range cache .watches [name ] {
151
- notifyList [watch ] = append (notifyList [watch ], name )
149
+ if ! fromDeletion {
150
+ for name := range modified {
151
+ for watch := range cache .watches [name ] {
152
+ notifyList [watch ] = append (notifyList [watch ], name )
153
+ }
154
+ delete (cache .watches , name )
155
+ }
156
+ } else {
157
+ for deletedName := range modified {
158
+ for watch , streamState := range cache .watches [deletedName ] {
159
+ resourceNames := streamState .GetKnownResourceNames (cache .typeURL )
160
+ for resourceName := range resourceNames {
161
+ // To avoid the stale in notifyList becomes empty slice.
162
+ // Don't skip resource name that has been deleted here.
163
+ // It would be filtered out in respond because the corresponding resource has been deleted.
164
+ notifyList [watch ] = append (notifyList [watch ], resourceName )
165
+ }
166
+ }
167
+ delete (cache .watches , deletedName )
152
168
}
153
- delete (cache .watches , name )
154
169
}
170
+
155
171
for value , stale := range notifyList {
156
172
cache .respond (value , stale )
157
173
}
@@ -173,6 +189,31 @@ func (cache *LinearCache) notifyAll(modified map[string]struct{}) {
173
189
}
174
190
}
175
191
192
+ func (cache * LinearCache ) notifyAllFromDeletion (modified map [string ]struct {}) {
193
+ notifyList := make (map [chan Response ][]string )
194
+ for deletedName := range modified {
195
+ for watch , streamState := range cache .watches [deletedName ] {
196
+ names := streamState .GetKnownResourceNames (cache .typeURL )
197
+ for name := range names {
198
+ if name == deletedName {
199
+ // skip the resource name has been deleted.
200
+ continue
201
+ }
202
+ notifyList [watch ] = append (notifyList [watch ], name )
203
+ }
204
+ }
205
+ delete (cache .watches , deletedName )
206
+ }
207
+
208
+ for value , stale := range notifyList {
209
+ cache .respond (value , stale )
210
+ }
211
+
212
+ for value := range cache .watchAll {
213
+ cache .respond (value , nil )
214
+ }
215
+ }
216
+
176
217
func (cache * LinearCache ) respondDelta (request * DeltaRequest , value chan DeltaResponse , state stream.StreamState ) * RawDeltaResponse {
177
218
resp := createDeltaResponse (context .Background (), request , state , resourceContainer {
178
219
resourceMap : cache .resources ,
@@ -205,7 +246,7 @@ func (cache *LinearCache) UpdateResource(name string, res types.Resource) error
205
246
cache .resources [name ] = res
206
247
207
248
// TODO: batch watch closures to prevent rapid updates
208
- cache .notifyAll (map [string ]struct {}{name : {}})
249
+ cache .notifyAll (map [string ]struct {}{name : {}}, false )
209
250
210
251
return nil
211
252
}
@@ -220,7 +261,7 @@ func (cache *LinearCache) DeleteResource(name string) error {
220
261
delete (cache .resources , name )
221
262
222
263
// TODO: batch watch closures to prevent rapid updates
223
- cache .notifyAll (map [string ]struct {}{name : {}})
264
+ cache .notifyAll (map [string ]struct {}{name : {}}, true )
224
265
return nil
225
266
}
226
267
@@ -252,7 +293,7 @@ func (cache *LinearCache) SetResources(resources map[string]types.Resource) {
252
293
modified [name ] = struct {}{}
253
294
}
254
295
255
- cache .notifyAll (modified )
296
+ cache .notifyAll (modified , false )
256
297
}
257
298
258
299
// GetResources returns current resources stored in the cache
@@ -315,7 +356,7 @@ func (cache *LinearCache) CreateWatch(request *Request, streamState stream.Strea
315
356
}
316
357
// Create open watches since versions are up to date.
317
358
if len (request .ResourceNames ) == 0 {
318
- cache .watchAll [value ] = struct {}{}
359
+ cache .watchAll [value ] = streamState
319
360
return func () {
320
361
cache .mu .Lock ()
321
362
defer cache .mu .Unlock ()
@@ -328,7 +369,7 @@ func (cache *LinearCache) CreateWatch(request *Request, streamState stream.Strea
328
369
set = make (watches )
329
370
cache .watches [name ] = set
330
371
}
331
- set [value ] = struct {}{}
372
+ set [value ] = streamState
332
373
}
333
374
return func () {
334
375
cache .mu .Lock ()
0 commit comments