bloblru: move defer outside critical section

This commit is contained in:
Michael Eischer 2024-05-26 12:38:20 +02:00
parent 0ad0b7ca7c
commit 21ce03cff2
1 changed files with 6 additions and 6 deletions

View File

@ -100,7 +100,13 @@ func (c *Cache) GetOrCompute(id restic.ID, compute func() ([]byte, error)) ([]by
waitForResult, isDownloading := c.inProgress[id]
if !isDownloading {
c.inProgress[id] = finish
}
c.mu.Unlock()
if isDownloading {
// wait for result of parallel download
<-waitForResult
} else {
// remove progress channel once finished here
defer func() {
c.mu.Lock()
@ -109,12 +115,6 @@ func (c *Cache) GetOrCompute(id restic.ID, compute func() ([]byte, error)) ([]by
close(finish)
}()
}
c.mu.Unlock()
if isDownloading {
// wait for result of parallel download
<-waitForResult
}
// try again. This is necessary independent of whether isDownloading is true or not.
// The calls to `c.Get()` and checking/adding the entry in `c.inProgress` are not atomic,