@@ -114,6 +114,7 @@ type cacheEntry struct {
114
114
value * lazy.ValueWithError [CacheEntryValue ]
115
115
116
116
close func ()
117
+ purge func ()
117
118
}
118
119
119
120
type CacheEntryValue struct {
@@ -149,7 +150,8 @@ func (c *Cache) Acquire(ctx context.Context, db database.Store, fileID uuid.UUID
149
150
e := c .prepare (db , fileID )
150
151
ev , err := e .value .Load ()
151
152
if err != nil {
152
- c .purge (fileID )
153
+ e .close ()
154
+ e .purge ()
153
155
return nil , err
154
156
}
155
157
@@ -193,9 +195,9 @@ func (c *Cache) prepare(db database.Store, fileID uuid.UUID) *cacheEntry {
193
195
if ! ok {
194
196
hitLabel = "false"
195
197
196
- var releaseOnce sync.Once
197
- release := func () {
198
- releaseOnce .Do (func () {
198
+ var purgeOnce sync.Once
199
+ purge := func () {
200
+ purgeOnce .Do (func () {
199
201
c .purge (fileID )
200
202
})
201
203
}
@@ -204,10 +206,6 @@ func (c *Cache) prepare(db database.Store, fileID uuid.UUID) *cacheEntry {
204
206
value : lazy .NewWithError (func () (CacheEntryValue , error ) {
205
207
val , err := fetch (db , fileID )
206
208
if err != nil {
207
- // Force future calls to Acquire to trigger a new fetch as soon as
208
- // a fetch has failed, even if references are still held.
209
- entry .close ()
210
- release ()
211
209
return val , err
212
210
}
213
211
@@ -228,8 +226,10 @@ func (c *Cache) prepare(db database.Store, fileID uuid.UUID) *cacheEntry {
228
226
return
229
227
}
230
228
231
- release ()
229
+ purge ()
232
230
},
231
+
232
+ purge : purge ,
233
233
}
234
234
c .data [fileID ] = entry
235
235
@@ -245,7 +245,8 @@ func (c *Cache) prepare(db database.Store, fileID uuid.UUID) *cacheEntry {
245
245
return entry
246
246
}
247
247
248
- // purge immediately removes an entry from the cache. It should be called
248
+ // purge immediately removes an entry from the cache, even if it has open references.
249
+ // It should only be called from the `close` function in a `cacheEntry`.
249
250
func (c * Cache ) purge (fileID uuid.UUID ) {
250
251
c .lock .Lock ()
251
252
defer c .lock .Unlock ()
0 commit comments