Skip to content

Commit e54e48a

Browse files
committed
address PR comments
1 parent 197472e commit e54e48a

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

coderd/files/cache.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ import (
1919
// NewFromStore returns a file cache that will fetch files from the provided
2020
// database.
2121
func NewFromStore(store database.Store, registerer prometheus.Registerer) *Cache {
22-
fetch := func(ctx context.Context, fileID uuid.UUID) (fs.FS, int64, error) {
22+
fetch := func(ctx context.Context, fileID uuid.UUID) (cacheEntryValue, error) {
2323
file, err := store.GetFileByID(ctx, fileID)
2424
if err != nil {
25-
return nil, 0, xerrors.Errorf("failed to read file from database: %w", err)
25+
return cacheEntryValue{}, xerrors.Errorf("failed to read file from database: %w", err)
2626
}
2727

2828
content := bytes.NewBuffer(file.Data)
29-
return archivefs.FromTarReader(content), int64(content.Len()), nil
29+
return cacheEntryValue{
30+
FS: archivefs.FromTarReader(content),
31+
size: int64(content.Len()),
32+
}, nil
3033
}
3134

3235
return New(fetch, registerer)
@@ -100,6 +103,10 @@ type Cache struct {
100103
fetcher
101104

102105
// metrics
106+
cacheMetrics
107+
}
108+
109+
type cacheMetrics struct {
103110
currentOpenFileReferences prometheus.Gauge
104111
totalOpenFileReferences prometheus.Counter
105112

@@ -111,7 +118,7 @@ type Cache struct {
111118
}
112119

113120
type cacheEntryValue struct {
114-
dir fs.FS
121+
fs.FS
115122
size int64
116123
}
117124

@@ -121,7 +128,7 @@ type cacheEntry struct {
121128
value *lazy.ValueWithError[cacheEntryValue]
122129
}
123130

124-
type fetcher func(context.Context, uuid.UUID) (dir fs.FS, size int64, err error)
131+
type fetcher func(context.Context, uuid.UUID) (cacheEntryValue, error)
125132

126133
// Acquire will load the fs.FS for the given file. It guarantees that parallel
127134
// calls for the same fileID will only result in one fetch, and that parallel
@@ -136,8 +143,9 @@ func (c *Cache) Acquire(ctx context.Context, fileID uuid.UUID) (fs.FS, error) {
136143
it, err := c.prepare(ctx, fileID).Load()
137144
if err != nil {
138145
c.Release(fileID)
146+
return nil, err
139147
}
140-
return it.dir, err
148+
return it.FS, err
141149
}
142150

143151
func (c *Cache) prepare(ctx context.Context, fileID uuid.UUID) *lazy.ValueWithError[cacheEntryValue] {
@@ -147,18 +155,15 @@ func (c *Cache) prepare(ctx context.Context, fileID uuid.UUID) *lazy.ValueWithEr
147155
entry, ok := c.data[fileID]
148156
if !ok {
149157
value := lazy.NewWithError(func() (cacheEntryValue, error) {
150-
dir, size, err := c.fetcher(ctx, fileID)
158+
val, err := c.fetcher(ctx, fileID)
151159

152160
// Always add to the cache size the bytes of the file loaded.
153161
if err == nil {
154-
c.currentCacheSize.Add(float64(size))
155-
c.totalCacheSize.Add(float64(size))
162+
c.currentCacheSize.Add(float64(val.size))
163+
c.totalCacheSize.Add(float64(val.size))
156164
}
157165

158-
return cacheEntryValue{
159-
dir: dir,
160-
size: size,
161-
}, err
166+
return val, err
162167
})
163168

164169
entry = &cacheEntry{

coderd/files/cache_internal_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package files
22

33
import (
44
"context"
5-
"io/fs"
65
"sync/atomic"
76
"testing"
87
"time"
@@ -28,12 +27,12 @@ func TestConcurrency(t *testing.T) {
2827
emptyFS := afero.NewIOFS(afero.NewReadOnlyFs(afero.NewMemMapFs()))
2928
var fetches atomic.Int64
3029
reg := prometheus.NewRegistry()
31-
c := New(func(_ context.Context, _ uuid.UUID) (fs.FS, int64, error) {
30+
c := New(func(_ context.Context, _ uuid.UUID) (cacheEntryValue, error) {
3231
fetches.Add(1)
3332
// Wait long enough before returning to make sure that all of the goroutines
3433
// will be waiting in line, ensuring that no one duplicated a fetch.
3534
time.Sleep(testutil.IntervalMedium)
36-
return emptyFS, fileSize, nil
35+
return cacheEntryValue{FS: emptyFS, size: fileSize}, nil
3736
}, reg)
3837

3938
batches := 1000
@@ -79,8 +78,11 @@ func TestRelease(t *testing.T) {
7978
const fileSize = 10
8079
emptyFS := afero.NewIOFS(afero.NewReadOnlyFs(afero.NewMemMapFs()))
8180
reg := prometheus.NewRegistry()
82-
c := New(func(_ context.Context, _ uuid.UUID) (fs.FS, int64, error) {
83-
return emptyFS, fileSize, nil
81+
c := New(func(_ context.Context, _ uuid.UUID) (cacheEntryValue, error) {
82+
return cacheEntryValue{
83+
FS: emptyFS,
84+
size: fileSize,
85+
}, nil
8486
}, reg)
8587

8688
batches := 100

0 commit comments

Comments
 (0)