fork of whitequark.org/git-pages with mods for tangled
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Refactor `S3Backend.GetManifest`. NFCI

This is both to reduce the amount of loose variables in the code, as
well as to make it closer to `S3Backend.GetBlob`.

+27 -18
+27 -18
src/backend_s3.go
··· 399 } 400 401 func (l s3ManifestLoader) load(ctx context.Context, name string, oldManifest *CachedManifest) (*CachedManifest, error) { 402 - log.Printf("s3: get manifest %s\n", name) 403 404 - startTime := time.Now() 405 406 - manifest, size, etag, err := func() (*Manifest, uint32, string, error) { 407 opts := minio.GetObjectOptions{} 408 if oldManifest != nil && oldManifest.etag != "" { 409 opts.SetMatchETagExcept(oldManifest.etag) ··· 411 object, err := l.s3.client.GetObject(ctx, l.s3.bucket, manifestObjectName(name), opts) 412 // Note that many errors (e.g. NoSuchKey) will be reported only after this point. 413 if err != nil { 414 - return nil, 0, "", err 415 } 416 defer object.Close() 417 418 data, err := io.ReadAll(object) 419 if err != nil { 420 - return nil, 0, "", err 421 } 422 423 stat, err := object.Stat() 424 if err != nil { 425 - return nil, 0, "", err 426 } 427 428 manifest, err := DecodeManifest(data) 429 if err != nil { 430 - return nil, 0, "", err 431 } 432 433 - return manifest, uint32(len(data)), stat.ETag, nil 434 - }() 435 436 - s3GetObjectDurationSeconds. 437 - With(prometheus.Labels{"kind": "manifest"}). 438 - Observe(time.Since(startTime).Seconds()) 439 440 if err != nil { 441 if errResp := minio.ToErrorResponse(err); errResp.Code == "NoSuchKey" { 442 s3GetObjectErrorsCount.With(prometheus.Labels{"object_kind": "manifest"}).Inc() 443 err = fmt.Errorf("%w: %s", ErrObjectNotFound, errResp.Key) 444 - return &CachedManifest{nil, 1, etag, err}, nil 445 } else if errResp.StatusCode == http.StatusNotModified && oldManifest != nil { 446 return oldManifest, nil 447 } else { 448 return nil, err 449 } 450 } else { 451 - return &CachedManifest{manifest, size, etag, err}, nil 452 } 453 } 454 455 - func (s3 *S3Backend) GetManifest(ctx context.Context, name string, opts GetManifestOptions) (*Manifest, error) { 456 if opts.BypassCache { 457 entry, found := s3.siteCache.Cache.GetEntry(name) 458 if found && entry.RefreshableAt().Before(time.Now()) { ··· 460 } 461 } 462 463 - cached, err := s3.siteCache.Get(ctx, name, s3ManifestLoader{s3}) 464 if err != nil { 465 - return nil, err 466 } else { 467 - return cached.manifest, cached.err 468 } 469 } 470
··· 399 } 400 401 func (l s3ManifestLoader) load(ctx context.Context, name string, oldManifest *CachedManifest) (*CachedManifest, error) { 402 + loader := func() (*CachedManifest, error) { 403 + log.Printf("s3: get manifest %s\n", name) 404 405 + startTime := time.Now() 406 407 opts := minio.GetObjectOptions{} 408 if oldManifest != nil && oldManifest.etag != "" { 409 opts.SetMatchETagExcept(oldManifest.etag) ··· 411 object, err := l.s3.client.GetObject(ctx, l.s3.bucket, manifestObjectName(name), opts) 412 // Note that many errors (e.g. NoSuchKey) will be reported only after this point. 413 if err != nil { 414 + return nil, err 415 } 416 defer object.Close() 417 418 data, err := io.ReadAll(object) 419 if err != nil { 420 + return nil, err 421 } 422 423 stat, err := object.Stat() 424 if err != nil { 425 + return nil, err 426 } 427 428 manifest, err := DecodeManifest(data) 429 if err != nil { 430 + return nil, err 431 } 432 433 + s3GetObjectDurationSeconds. 434 + With(prometheus.Labels{"kind": "manifest"}). 435 + Observe(time.Since(startTime).Seconds()) 436 437 + return &CachedManifest{manifest, uint32(len(data)), stat.ETag, nil}, nil 438 + } 439 440 + var cached *CachedManifest 441 + cached, err := loader() 442 if err != nil { 443 if errResp := minio.ToErrorResponse(err); errResp.Code == "NoSuchKey" { 444 s3GetObjectErrorsCount.With(prometheus.Labels{"object_kind": "manifest"}).Inc() 445 err = fmt.Errorf("%w: %s", ErrObjectNotFound, errResp.Key) 446 + return &CachedManifest{nil, 1, "", err}, nil 447 } else if errResp.StatusCode == http.StatusNotModified && oldManifest != nil { 448 return oldManifest, nil 449 } else { 450 return nil, err 451 } 452 } else { 453 + return cached, nil 454 } 455 } 456 457 + func (s3 *S3Backend) GetManifest( 458 + ctx context.Context, name string, opts GetManifestOptions, 459 + ) ( 460 + manifest *Manifest, err error, 461 + ) { 462 if opts.BypassCache { 463 entry, found := s3.siteCache.Cache.GetEntry(name) 464 if found && entry.RefreshableAt().Before(time.Now()) { ··· 466 } 467 } 468 469 + var cached *CachedManifest 470 + cached, err = s3.siteCache.Get(ctx, name, s3ManifestLoader{s3}) 471 if err != nil { 472 + return 473 } else { 474 + // This could be `manifest, nil` or `nil, ErrObjectNotFound`. 475 + manifest, err = cached.manifest, cached.err 476 + return 477 } 478 } 479