Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
close to not return an error
  • Loading branch information
Emyrk committed Jun 18, 2025
commit 93170203b2bda7e96f5b62029f9e3cd7905a6658
11 changes: 4 additions & 7 deletions coderd/files/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package files
import (
"bytes"
"context"
"io"
"io/fs"
"sync"

Expand Down Expand Up @@ -142,8 +141,7 @@ type cacheEntry struct {
type fetcher func(context.Context, uuid.UUID) (CacheEntryValue, error)

var (
_ fs.FS = (*CloseFS)(nil)
_ io.Closer = (*CloseFS)(nil)
_ fs.FS = (*CloseFS)(nil)
)

// CloseFS is a wrapper around fs.FS that implements io.Closer. The Close()
Expand All @@ -152,10 +150,10 @@ var (
type CloseFS struct {
fs.FS

close func() error
close func()
}

func (f *CloseFS) Close() error { return f.close() }
func (f *CloseFS) Close() { f.close() }

// 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
Expand Down Expand Up @@ -187,11 +185,10 @@ func (c *Cache) Acquire(ctx context.Context, fileID uuid.UUID) (*CloseFS, error)
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

return &CloseFS{
FS: it.FS,
close: func() error {
close: func() {
// 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
}
Expand Down
Loading