@@ -19,14 +19,17 @@ import (
19
19
// NewFromStore returns a file cache that will fetch files from the provided
20
20
// database.
21
21
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 ) {
23
23
file , err := store .GetFileByID (ctx , fileID )
24
24
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 )
26
26
}
27
27
28
28
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
30
33
}
31
34
32
35
return New (fetch , registerer )
@@ -100,6 +103,10 @@ type Cache struct {
100
103
fetcher
101
104
102
105
// metrics
106
+ cacheMetrics
107
+ }
108
+
109
+ type cacheMetrics struct {
103
110
currentOpenFileReferences prometheus.Gauge
104
111
totalOpenFileReferences prometheus.Counter
105
112
@@ -111,7 +118,7 @@ type Cache struct {
111
118
}
112
119
113
120
type cacheEntryValue struct {
114
- dir fs.FS
121
+ fs.FS
115
122
size int64
116
123
}
117
124
@@ -121,7 +128,7 @@ type cacheEntry struct {
121
128
value * lazy.ValueWithError [cacheEntryValue ]
122
129
}
123
130
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 )
125
132
126
133
// Acquire will load the fs.FS for the given file. It guarantees that parallel
127
134
// 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) {
136
143
it , err := c .prepare (ctx , fileID ).Load ()
137
144
if err != nil {
138
145
c .Release (fileID )
146
+ return nil , err
139
147
}
140
- return it .dir , err
148
+ return it .FS , err
141
149
}
142
150
143
151
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
147
155
entry , ok := c .data [fileID ]
148
156
if ! ok {
149
157
value := lazy .NewWithError (func () (cacheEntryValue , error ) {
150
- dir , size , err := c .fetcher (ctx , fileID )
158
+ val , err := c .fetcher (ctx , fileID )
151
159
152
160
// Always add to the cache size the bytes of the file loaded.
153
161
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 ))
156
164
}
157
165
158
- return cacheEntryValue {
159
- dir : dir ,
160
- size : size ,
161
- }, err
166
+ return val , err
162
167
})
163
168
164
169
entry = & cacheEntry {
0 commit comments