Skip to content

Commit 24f5f01

Browse files
pieternclaude
andauthored
Remove stored context from readahead cache struct (#4690)
## Summary - Remove the `ctx context.Context` field from the readahead cache struct, per the guidance in https://go.dev/blog/context-and-structs - Introduce `enqueueReadahead` for background work tied to the cache's lifetime, separate from `enqueue` which respects individual caller cancellation Follow-up to #4657 (comment). ## Test plan - [x] Existing `TestWorkspaceFilesCache_*` tests pass 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 520988b commit 24f5f01

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

libs/filer/workspace_files_cache.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,8 @@ type executable interface {
182182
// cache stores all entries for cacheable Workspace File System calls.
183183
// We care about caching only [ReadDir] and [Stat] calls.
184184
type cache struct {
185-
f Filer
186-
ctx context.Context
187-
m sync.Mutex
185+
f Filer
186+
m sync.Mutex
188187

189188
readDir map[string]*readDirEntry
190189
stat map[string]*statEntry
@@ -198,8 +197,7 @@ type cache struct {
198197

199198
func newWorkspaceFilesReadaheadCache(ctx context.Context, f Filer) *cache {
200199
c := &cache{
201-
f: f,
202-
ctx: ctx,
200+
f: f,
203201

204202
readDir: make(map[string]*readDirEntry),
205203
stat: make(map[string]*statEntry),
@@ -224,10 +222,9 @@ func (c *cache) work(ctx context.Context) {
224222
}
225223
}
226224

227-
// enqueue adds an operation to the queue.
228-
// If the context is canceled, an error is returned.
229-
// If the queue is full, an error is returned.
230-
//
225+
// enqueue adds a caller-initiated operation to the queue.
226+
// It respects the caller's context to allow cancellation of individual requests.
227+
// It returns an error if the caller's context is canceled or the queue is full.
231228
// Its caller is holding the lock so it cannot block.
232229
func (c *cache) enqueue(ctx context.Context, e executable) error {
233230
select {
@@ -240,6 +237,20 @@ func (c *cache) enqueue(ctx context.Context, e executable) error {
240237
}
241238
}
242239

240+
// enqueueReadahead adds a background readahead operation to the queue.
241+
// It does not take a context because readahead is tied to the cache's lifetime,
242+
// not any individual caller's request.
243+
// It returns an error if the queue is full.
244+
// Its caller is holding the lock so it cannot block.
245+
func (c *cache) enqueueReadahead(e executable) error {
246+
select {
247+
case c.queue <- e:
248+
return nil
249+
default:
250+
return queueFullError{e.String()}
251+
}
252+
}
253+
243254
func (c *cache) completeReadDirForDir(name string, dirEntry fs.DirEntry) {
244255
// Add to the stat cache if not already present.
245256
if _, ok := c.stat[name]; !ok {
@@ -252,7 +263,7 @@ func (c *cache) completeReadDirForDir(name string, dirEntry fs.DirEntry) {
252263
if _, ok := c.readDir[name]; !ok {
253264
// Create a new cache entry and queue the operation.
254265
e := newReadDirEntry(name)
255-
err := c.enqueue(c.ctx, e)
266+
err := c.enqueueReadahead(e)
256267
if err != nil {
257268
e.markError(err)
258269
}
@@ -279,7 +290,7 @@ func (c *cache) completeReadDirForFile(name string, dirEntry fs.DirEntry) {
279290
// This is the only (?) case where this implementation is tied to the workspace filer.
280291

281292
// Queue a [Stat] call for the file.
282-
err := c.enqueue(c.ctx, e)
293+
err := c.enqueueReadahead(e)
283294
if err != nil {
284295
e.markError(err)
285296
}

0 commit comments

Comments
 (0)