@@ -69,7 +69,6 @@ func (p StaticSourceProvider) Sources(namespaces ...string) map[SourceKey]Source
69
69
70
70
type OperatorCacheProvider interface {
71
71
Namespaced (namespaces ... string ) MultiCatalogOperatorFinder
72
- Expire (catalog SourceKey )
73
72
}
74
73
75
74
type SourcePriorityProvider interface {
@@ -150,22 +149,11 @@ func (c *NamespacedOperatorCache) Error() error {
150
149
return errors .NewAggregate (errs )
151
150
}
152
151
153
- func (c * Cache ) Expire (catalog SourceKey ) {
154
- c .m .Lock ()
155
- defer c .m .Unlock ()
156
- s , ok := c .snapshots [catalog ]
157
- if ! ok {
158
- return
159
- }
160
- s .expiry = time .Unix (0 , 0 )
161
- }
162
-
163
152
func (c * Cache ) Namespaced (namespaces ... string ) MultiCatalogOperatorFinder {
164
153
const (
165
154
CachePopulateTimeout = time .Minute
166
155
)
167
156
168
- now := time .Now ()
169
157
sources := c .sp .Sources (namespaces ... )
170
158
171
159
result := NamespacedOperatorCache {
@@ -182,7 +170,7 @@ func (c *Cache) Namespaced(namespaces ...string) MultiCatalogOperatorFinder {
182
170
func () {
183
171
snapshot .m .RLock ()
184
172
defer snapshot .m .RUnlock ()
185
- if snapshot .Valid (now ) {
173
+ if snapshot .Valid () {
186
174
result .snapshots [key ] = snapshot
187
175
} else {
188
176
misses = append (misses , key )
@@ -205,7 +193,7 @@ func (c *Cache) Namespaced(namespaces ...string) MultiCatalogOperatorFinder {
205
193
// Take the opportunity to clear expired snapshots while holding the lock.
206
194
var expired []SourceKey
207
195
for key , snapshot := range c .snapshots {
208
- if ! snapshot .Valid (now ) {
196
+ if ! snapshot .Valid () {
209
197
snapshot .Cancel ()
210
198
expired = append (expired , key )
211
199
}
@@ -217,7 +205,7 @@ func (c *Cache) Namespaced(namespaces ...string) MultiCatalogOperatorFinder {
217
205
// Check for any snapshots that were populated while waiting to acquire the lock.
218
206
var found int
219
207
for i := range misses {
220
- if hdr , ok := c.snapshots [misses [i ]]; ok && hdr .Valid (now ) {
208
+ if hdr , ok := c.snapshots [misses [i ]]; ok && hdr .Valid () {
221
209
result.snapshots [misses [i ]] = hdr
222
210
misses [found ], misses [i ] = misses [i ], misses [found ]
223
211
found ++
@@ -230,17 +218,10 @@ func (c *Cache) Namespaced(namespaces ...string) MultiCatalogOperatorFinder {
230
218
231
219
hdr := snapshotHeader {
232
220
key : miss ,
233
- expiry : now .Add (c .ttl ),
234
221
pop : cancel ,
235
222
priority : c .sourcePriorityProvider .Priority (miss ),
236
223
}
237
224
238
- if miss .Virtual () {
239
- // hack! always refresh virtual catalogs.
240
- // todo: Sources should be responsible for determining when the Snapshots they produce become invalid
241
- hdr .expiry = time.Time {}
242
- }
243
-
244
225
hdr .m .Lock ()
245
226
c .snapshots [miss ] = & hdr
246
227
result .snapshots [miss ] = & hdr
@@ -249,7 +230,13 @@ func (c *Cache) Namespaced(namespaces ...string) MultiCatalogOperatorFinder {
249
230
defer hdr .m .Unlock ()
250
231
c .sem <- struct {}{}
251
232
defer func () { <- c .sem }()
252
- hdr .snapshot , hdr .err = source .Snapshot (ctx )
233
+ if snapshot , err := source .Snapshot (ctx ); err != nil {
234
+ hdr .err = err
235
+ } else if snapshot != nil {
236
+ hdr .snapshot = snapshot
237
+ } else {
238
+ hdr .err = fmt .Errorf ("source %q produced no snapshot and no error" , hdr .key )
239
+ }
253
240
}(ctx , & hdr , sources [miss ])
254
241
}
255
242
@@ -286,6 +273,15 @@ func (c *NamespacedOperatorCache) Find(p ...Predicate) []*Entry {
286
273
287
274
type Snapshot struct {
288
275
Entries []* Entry
276
+
277
+ // Unless closed, the Snapshot is valid.
278
+ Valid <- chan struct {}
279
+ }
280
+
281
+ func ValidOnce () <- chan struct {} {
282
+ c := make (chan struct {})
283
+ close (c )
284
+ return c
289
285
}
290
286
291
287
var _ Source = & Snapshot {}
@@ -298,7 +294,6 @@ type snapshotHeader struct {
298
294
snapshot * Snapshot
299
295
300
296
key SourceKey
301
- expiry time.Time
302
297
m sync.RWMutex
303
298
pop context.CancelFunc
304
299
err error
@@ -309,10 +304,18 @@ func (hdr *snapshotHeader) Cancel() {
309
304
hdr .pop ()
310
305
}
311
306
312
- func (hdr * snapshotHeader ) Valid (at time. Time ) bool {
307
+ func (hdr * snapshotHeader ) Valid () bool {
313
308
hdr .m .RLock ()
314
309
defer hdr .m .RUnlock ()
315
- return hdr .snapshot != nil && hdr .err == nil && at .Before (hdr .expiry )
310
+ if hdr .snapshot == nil || hdr .err != nil {
311
+ return false
312
+ }
313
+ select {
314
+ case <- hdr .snapshot .Valid :
315
+ return false
316
+ default :
317
+ }
318
+ return true
316
319
}
317
320
318
321
type sortableSnapshots struct {
0 commit comments