Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jun 17, 2025

Made idempotent

Copy link
Member Author

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be even more correct to have each file reference have some uuid. Then we can be sure Aquire to Release is 1:1.

I did not want to refactor too much right now, so kept the underlying release mechanism the same.

Comment on lines 150 to 156
type CloseFS struct {
fs.FS

close func() error
}

func (f *CloseFS) Close() error { return f.close() }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I named this Close to match the pattern of opening a file and deferring a Close

//
// 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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexported to force callers to use the Close method for releasing.

@Emyrk Emyrk marked this pull request as ready for review June 17, 2025 21:27
@Emyrk Emyrk requested a review from aslilac June 17, 2025 21:27
return nil, err
}

return it.FS, err
var once sync.Once
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather store the Once on the struct and then just put the closing logic directly in the Close method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also have to store the files.Cache on the struct then too.
Which allows calling Acquire and other methods.

It would be something like:

type CloseFS struct {
	fs.FS
	
	once sync.Once
	fileID uuid.UUID
	cache *Cache
}

func (f *CloseFS) Close() error {
	f.once.Do(func() {
		f.cache.release(f.fileID)
	})
}

I do not want cache to be accessible, so I could throw the release as a method, but then we're back to having an anonymous function as a field. In that case, I'd rather not add fields to the struct that are only used in Close

@@ -206,7 +208,8 @@ func TestRelease(t *testing.T) {
for closedIdx, id := range ids {
stillOpen := len(ids) - closedIdx
for closingIdx := range batchSize {
c.Release(id)
_ = releases[id][0]()
releases[id] = releases[id][1:]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just change the loops to iterate over this releases map if you really wanna do it this way

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't feel like changing the test. This works

@Emyrk Emyrk requested a review from aslilac June 18, 2025 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants