- Notifications
You must be signed in to change notification settings - Fork914
chore: file cache Release tied 1:1 with an acquire#18410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3,6 +3,7 @@ package files | ||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"io/fs" | ||
"sync" | ||
@@ -140,20 +141,36 @@ type cacheEntry struct { | ||
type fetcher func(context.Context, uuid.UUID) (CacheEntryValue, error) | ||
var ( | ||
_ fs.FS = (*CloseFS)(nil) | ||
_ io.Closer = (*CloseFS)(nil) | ||
) | ||
// CloseFS is a wrapper around fs.FS that implements io.Closer. The Close() | ||
// method tells the cache to release the fileID. Once all open references are | ||
// closed, the file is removed from the cache. | ||
type CloseFS struct { | ||
fs.FS | ||
close func() error | ||
} | ||
func (f *CloseFS) Close() error { return f.close() } | ||
Comment on lines +152 to +158 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I named this | ||
// Acquire will load the fs.FS for the given file. It guarantees that parallel | ||
// calls for the same fileID will only result in one fetch, and that parallel | ||
// calls for distinct fileIDs will fetch in parallel. | ||
// | ||
// Safety: Every call to Acquire that does not return an error must have a | ||
// matching call to Release. | ||
func (c *Cache) Acquire(ctx context.Context, fileID uuid.UUID) (*CloseFS, error) { | ||
// It's important that this `Load` call occurs outside of `prepare`, after the | ||
// mutex has been released, or we would continue to hold the lock until the | ||
// entire file has been fetched, which may be slow, and would prevent other | ||
// files from being fetched in parallel. | ||
it, err := c.prepare(ctx, fileID).Load() | ||
if err != nil { | ||
c.release(fileID) | ||
return nil, err | ||
} | ||
@@ -163,11 +180,20 @@ func (c *Cache) Acquire(ctx context.Context, fileID uuid.UUID) (fs.FS, error) { | ||
} | ||
// Always check the caller can actually read the file. | ||
if err := c.authz.Authorize(ctx, subject, policy.ActionRead, it.Object); err != nil { | ||
c.release(fileID) | ||
return nil, err | ||
} | ||
var once sync.Once | ||
return &CloseFS{ | ||
FS: it.FS, | ||
close: func() error { | ||
// sync.Once makes the Close() idempotent, so we can call it | ||
// multiple times without worrying about double-releasing. | ||
once.Do(func() { c.release(fileID) }) | ||
return nil | ||
}, | ||
}, nil | ||
} | ||
func (c *Cache) prepare(ctx context.Context, fileID uuid.UUID) *lazy.ValueWithError[CacheEntryValue] { | ||
@@ -203,9 +229,12 @@ func (c *Cache) prepare(ctx context.Context, fileID uuid.UUID) *lazy.ValueWithEr | ||
return entry.value | ||
} | ||
//release decrements the reference count for the given fileID, and frees the | ||
// backing data if there are no further references being held. | ||
// | ||
// release should only be called after a successful call to Acquire using the Release() | ||
// method on the returned *CloseFS. | ||
func (c *Cache) release(fileID uuid.UUID) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Unexported to force callers to use the | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
Uh oh!
There was an error while loading.Please reload this page.