Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 610740a

Browse files
committed
proper release gating
1 parent bf6562a commit 610740a

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

coderd/files/cache.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,20 @@ func (c *Cache) prepare(ctx context.Context, db database.Store, fileID uuid.UUID
194194
hitLabel = "false"
195195

196196
var releaseOnce sync.Once
197+
release := func() {
198+
releaseOnce.Do(func() {
199+
c.purge(fileID)
200+
})
201+
}
197202
entry = &cacheEntry{
198203
refCount: 0,
199204
value: lazy.NewWithError(func() (CacheEntryValue, error) {
200205
val, err := fetch(db, fileID)
201206
if err != nil {
202207
// Force future calls to Acquire to trigger a new fetch as soon as
203208
// a fetch has failed, even if references are still held.
204-
delete(c.data, fileID)
209+
entry.close()
210+
release()
205211
return val, err
206212
}
207213

@@ -218,12 +224,11 @@ func (c *Cache) prepare(ctx context.Context, db database.Store, fileID uuid.UUID
218224

219225
entry.refCount--
220226
c.currentOpenFileReferences.Dec()
221-
222-
if entry.refCount == 0 {
223-
releaseOnce.Do(func() {
224-
c.purge(fileID)
225-
})
227+
if entry.refCount > 0 {
228+
return
226229
}
230+
231+
release()
227232
},
228233
}
229234
c.data[fileID] = entry

0 commit comments

Comments
 (0)