Skip to content

Commit 69e02e1

Browse files
committed
doesn't need to be an atomic int
1 parent 125ef95 commit 69e02e1

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

coderd/files/cache.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"io/fs"
77
"sync"
8-
"sync/atomic"
98

109
"github.com/google/uuid"
1110
"golang.org/x/xerrors"
@@ -47,7 +46,8 @@ type Cache struct {
4746
}
4847

4948
type cacheEntry struct {
50-
refCount *atomic.Int64
49+
// refCount must only be accessed while the Cache lock is held.
50+
refCount int
5151
value *lazy.ValueWithError[fs.FS]
5252
}
5353

@@ -70,19 +70,18 @@ func (c *Cache) prepare(ctx context.Context, fileID uuid.UUID) *lazy.ValueWithEr
7070

7171
entry, ok := c.data[fileID]
7272
if !ok {
73-
var refCount atomic.Int64
7473
value := lazy.NewWithError(func() (fs.FS, error) {
7574
return c.fetcher(ctx, fileID)
7675
})
7776

7877
entry = &cacheEntry{
7978
value: value,
80-
refCount: &refCount,
79+
refCount: 0,
8180
}
8281
c.data[fileID] = entry
8382
}
8483

85-
entry.refCount.Add(1)
84+
entry.refCount += 1
8685
return entry.value
8786
}
8887

@@ -99,8 +98,8 @@ func (c *Cache) Release(fileID uuid.UUID) {
9998
// this function with an incorrect ID. Should this function return an error?
10099
return
101100
}
102-
refCount := entry.refCount.Add(-1)
103-
if refCount > 0 {
101+
entry.refCount -= 1
102+
if entry.refCount > 0 {
104103
return
105104
}
106105
delete(c.data, fileID)

0 commit comments

Comments
 (0)