Skip to content

chore: add files cache for reading template tar archives from db #17141

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

Merged
merged 7 commits into from
Apr 2, 2025
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
doesn't need to be an atomic int
  • Loading branch information
aslilac committed Apr 1, 2025
commit 69e02e1344db7505fddc75d5960cf37bb0abe78f
13 changes: 6 additions & 7 deletions coderd/files/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"context"
"io/fs"
"sync"
"sync/atomic"

"github.com/google/uuid"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -47,7 +46,8 @@
}

type cacheEntry struct {
refCount *atomic.Int64
// refCount must only be accessed while the Cache lock is held.
refCount int
value *lazy.ValueWithError[fs.FS]
}

Expand All @@ -70,19 +70,18 @@

entry, ok := c.data[fileID]
if !ok {
var refCount atomic.Int64
value := lazy.NewWithError(func() (fs.FS, error) {
return c.fetcher(ctx, fileID)
})

entry = &cacheEntry{
value: value,
refCount: &refCount,
refCount: 0,
}
c.data[fileID] = entry
}

entry.refCount.Add(1)
entry.refCount += 1

Check failure on line 84 in coderd/files/cache.go

View workflow job for this annotation

GitHub Actions / lint

increment-decrement: should replace entry.refCount += 1 with entry.refCount++ (revive)
return entry.value
}

Expand All @@ -99,8 +98,8 @@
// this function with an incorrect ID. Should this function return an error?
return
}
refCount := entry.refCount.Add(-1)
if refCount > 0 {
entry.refCount -= 1

Check failure on line 101 in coderd/files/cache.go

View workflow job for this annotation

GitHub Actions / lint

increment-decrement: should replace entry.refCount -= 1 with entry.refCount-- (revive)
if entry.refCount > 0 {
return
}
delete(c.data, fileID)
Expand Down
Loading