@@ -13,6 +13,7 @@ import (
1313 "net/url"
1414 "strconv"
1515 "strings"
16+ "sync"
1617 "time"
1718
1819 "github.com/git-pkgs/cooldown"
@@ -48,6 +49,15 @@ func hasDotDotSegment(path string) bool {
4849
4950const defaultHTTPTimeout = 30 * time .Second
5051
52+ const artifactCopyBufferSize = 32 << 10
53+
54+ var artifactCopyBufferPool = sync.Pool { //nolint:gochecknoglobals // shared across artifact responses
55+ New : func () any {
56+ buffer := make ([]byte , artifactCopyBufferSize )
57+ return & buffer
58+ },
59+ }
60+
5161// canonicalPackagePURL returns a versionless PURL in canonical form so cooldown
5262// lookups match keys produced by config.CooldownConfig.NormalizedPackages.
5363func canonicalPackagePURL (ecosystem , name string ) string {
@@ -157,27 +167,11 @@ func (p *Proxy) GetCachedArtifact(ctx context.Context, ecosystem, name, version,
157167
158168// checkCache looks up an artifact in the cache. Returns nil if not cached.
159169func (p * Proxy ) checkCache (ctx context.Context , pkgPURL , versionPURL , filename string ) (* CacheResult , error ) {
160- pkg , err := p .DB .GetPackageByPURL (pkgPURL )
161- if err != nil {
162- return nil , fmt .Errorf ("checking package cache: %w" , err )
163- }
164- if pkg == nil {
165- return nil , nil
166- }
167-
168- ver , err := p .DB .GetVersionByPURL (versionPURL )
169- if err != nil {
170- return nil , fmt .Errorf ("checking version cache: %w" , err )
171- }
172- if ver == nil {
173- return nil , nil
174- }
175-
176- artifact , err := p .DB .GetArtifact (versionPURL , filename )
170+ artifact , err := p .DB .GetCachedArtifact (pkgPURL , versionPURL , filename )
177171 if err != nil {
178172 return nil , fmt .Errorf ("checking artifact cache: %w" , err )
179173 }
180- if artifact == nil || ! artifact . IsCached () {
174+ if artifact == nil {
181175 return nil , nil
182176 }
183177
@@ -189,39 +183,39 @@ func (p *Proxy) checkCache(ctx context.Context, pkgPURL, versionPURL, filename s
189183 }
190184
191185 if p .DirectServe {
192- signed , err := p .Storage .SignedURL (ctx , artifact .StoragePath . String , p .DirectServeTTL )
186+ signed , err := p .Storage .SignedURL (ctx , artifact .StoragePath , p .DirectServeTTL )
193187 if err == nil {
194188 result .RedirectURL = rewriteSignedURLHost (signed , p .DirectServeBaseURL )
195- p .recordCacheHit (pkgPURL , versionPURL , filename )
189+ p .recordCacheHit (artifact . Ecosystem , versionPURL , filename )
196190 return result , nil
197191 }
198192 if ! errors .Is (err , storage .ErrSignedURLUnsupported ) {
199193 p .Logger .Warn ("failed to sign storage URL, falling back to streaming" ,
200- "path" , artifact .StoragePath . String , "error" , err )
194+ "path" , artifact .StoragePath , "error" , err )
201195 }
202196 }
203197
204198 start := time .Now ()
205- reader , err := p .Storage .Open (ctx , artifact .StoragePath . String )
199+ reader , err := p .Storage .Open (ctx , artifact .StoragePath )
206200 metrics .RecordStorageOperation ("read" , time .Since (start ))
207201 if err != nil {
208202 metrics .RecordStorageError ("read" )
209203 p .Logger .Warn ("cached artifact missing from storage, will refetch" ,
210- "path" , artifact .StoragePath . String , "error" , err )
204+ "path" , artifact .StoragePath , "error" , err )
211205 return nil , nil
212206 }
213207
214- result .Reader = newVerifyingReader (reader , artifact .ContentHash .String , ver .Integrity .String ,
208+ result .Reader = newVerifyingReader (reader , artifact .ContentHash .String , artifact .Integrity .String ,
215209 func (reason string ) {
216210 p .Logger .Error ("cached artifact failed integrity check" ,
217211 "purl" , versionPURL , "filename" , filename ,
218- "path" , artifact .StoragePath . String , "reason" , reason )
219- metrics .RecordIntegrityFailure (pkg .Ecosystem )
212+ "path" , artifact .StoragePath , "reason" , reason )
213+ metrics .RecordIntegrityFailure (artifact .Ecosystem )
220214 if err := p .DB .ClearArtifactCache (versionPURL , filename ); err != nil {
221215 p .Logger .Warn ("failed to clear corrupt artifact from cache" , "error" , err )
222216 }
223217 })
224- p .recordCacheHit (pkgPURL , versionPURL , filename )
218+ p .recordCacheHit (artifact . Ecosystem , versionPURL , filename )
225219 return result , nil
226220}
227221
@@ -245,11 +239,9 @@ func rewriteSignedURLHost(signed, baseURL string) string {
245239 return s .String ()
246240}
247241
248- func (p * Proxy ) recordCacheHit (pkgPURL , versionPURL , filename string ) {
242+ func (p * Proxy ) recordCacheHit (ecosystem , versionPURL , filename string ) {
249243 _ = p .DB .RecordArtifactHit (versionPURL , filename )
250- if parsed , err := purl .Parse (pkgPURL ); err == nil {
251- metrics .RecordCacheHit (purl .PURLTypeToEcosystem (parsed .Type ))
252- }
244+ metrics .RecordCacheHit (purl .NormalizeEcosystem (ecosystem ))
253245}
254246
255247func (p * Proxy ) fetchAndCache (ctx context.Context , ecosystem , name , version , filename , pkgPURL , versionPURL string ) (* CacheResult , error ) {
@@ -376,7 +368,7 @@ func ServeArtifact(w http.ResponseWriter, result *CacheResult) {
376368func serveArtifact (w http.ResponseWriter , method string , result * CacheResult ) {
377369 if result .RedirectURL != "" {
378370 if result .Hash != "" {
379- w .Header ().Set ("ETag" , fmt . Sprintf ( `"%s"` , result .Hash ) )
371+ w .Header ().Set ("ETag" , `"` + result .Hash + `"` )
380372 }
381373 w .Header ().Set ("Location" , result .RedirectURL )
382374 w .WriteHeader (http .StatusFound )
@@ -391,15 +383,18 @@ func serveArtifact(w http.ResponseWriter, method string, result *CacheResult) {
391383 w .Header ().Set ("Content-Type" , result .ContentType )
392384 }
393385 if result .Size > 0 || (method == http .MethodHead && result .Size == 0 ) {
394- w .Header ().Set ("Content-Length" , fmt . Sprintf ( "%d" , result .Size ))
386+ w .Header ().Set ("Content-Length" , strconv . FormatInt ( result .Size , 10 ))
395387 }
396388 if result .Hash != "" {
397- w .Header ().Set ("ETag" , fmt . Sprintf ( `"%s"` , result .Hash ) )
389+ w .Header ().Set ("ETag" , `"` + result .Hash + `"` )
398390 }
399391
400392 w .WriteHeader (http .StatusOK )
401393 if method != http .MethodHead && result .Reader != nil {
402- _ , _ = io .Copy (w , result .Reader )
394+ buffer := artifactCopyBufferPool .Get ().(* []byte )
395+ defer artifactCopyBufferPool .Put (buffer )
396+ // Hide optional ReaderFrom methods so io.CopyBuffer uses the pooled buffer.
397+ _ , _ = io .CopyBuffer (struct { io.Writer }{w }, result .Reader , * buffer )
403398 }
404399}
405400
0 commit comments