mirror of
https://codeberg.org/git-pages/git-pages.git
synced 2026-05-17 04:31:38 +00:00
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`.
This commit is contained in:
@@ -399,11 +399,11 @@ func (l s3ManifestLoader) Reload(ctx context.Context, key string, oldValue *Cach
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l s3ManifestLoader) load(ctx context.Context, name string, oldManifest *CachedManifest) (*CachedManifest, error) {
|
func (l s3ManifestLoader) load(ctx context.Context, name string, oldManifest *CachedManifest) (*CachedManifest, error) {
|
||||||
log.Printf("s3: get manifest %s\n", name)
|
loader := func() (*CachedManifest, error) {
|
||||||
|
log.Printf("s3: get manifest %s\n", name)
|
||||||
|
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
|
|
||||||
manifest, size, etag, err := func() (*Manifest, uint32, string, error) {
|
|
||||||
opts := minio.GetObjectOptions{}
|
opts := minio.GetObjectOptions{}
|
||||||
if oldManifest != nil && oldManifest.etag != "" {
|
if oldManifest != nil && oldManifest.etag != "" {
|
||||||
opts.SetMatchETagExcept(oldManifest.etag)
|
opts.SetMatchETagExcept(oldManifest.etag)
|
||||||
@@ -411,48 +411,54 @@ func (l s3ManifestLoader) load(ctx context.Context, name string, oldManifest *Ca
|
|||||||
object, err := l.s3.client.GetObject(ctx, l.s3.bucket, manifestObjectName(name), opts)
|
object, err := l.s3.client.GetObject(ctx, l.s3.bucket, manifestObjectName(name), opts)
|
||||||
// Note that many errors (e.g. NoSuchKey) will be reported only after this point.
|
// Note that many errors (e.g. NoSuchKey) will be reported only after this point.
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer object.Close()
|
defer object.Close()
|
||||||
|
|
||||||
data, err := io.ReadAll(object)
|
data, err := io.ReadAll(object)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
stat, err := object.Stat()
|
stat, err := object.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
manifest, err := DecodeManifest(data)
|
manifest, err := DecodeManifest(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return manifest, uint32(len(data)), stat.ETag, nil
|
s3GetObjectDurationSeconds.
|
||||||
}()
|
With(prometheus.Labels{"kind": "manifest"}).
|
||||||
|
Observe(time.Since(startTime).Seconds())
|
||||||
|
|
||||||
s3GetObjectDurationSeconds.
|
return &CachedManifest{manifest, uint32(len(data)), stat.ETag, nil}, nil
|
||||||
With(prometheus.Labels{"kind": "manifest"}).
|
}
|
||||||
Observe(time.Since(startTime).Seconds())
|
|
||||||
|
|
||||||
|
var cached *CachedManifest
|
||||||
|
cached, err := loader()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errResp := minio.ToErrorResponse(err); errResp.Code == "NoSuchKey" {
|
if errResp := minio.ToErrorResponse(err); errResp.Code == "NoSuchKey" {
|
||||||
s3GetObjectErrorsCount.With(prometheus.Labels{"object_kind": "manifest"}).Inc()
|
s3GetObjectErrorsCount.With(prometheus.Labels{"object_kind": "manifest"}).Inc()
|
||||||
err = fmt.Errorf("%w: %s", ErrObjectNotFound, errResp.Key)
|
err = fmt.Errorf("%w: %s", ErrObjectNotFound, errResp.Key)
|
||||||
return &CachedManifest{nil, 1, etag, err}, nil
|
return &CachedManifest{nil, 1, "", err}, nil
|
||||||
} else if errResp.StatusCode == http.StatusNotModified && oldManifest != nil {
|
} else if errResp.StatusCode == http.StatusNotModified && oldManifest != nil {
|
||||||
return oldManifest, nil
|
return oldManifest, nil
|
||||||
} else {
|
} else {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return &CachedManifest{manifest, size, etag, err}, nil
|
return cached, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s3 *S3Backend) GetManifest(ctx context.Context, name string, opts GetManifestOptions) (*Manifest, error) {
|
func (s3 *S3Backend) GetManifest(
|
||||||
|
ctx context.Context, name string, opts GetManifestOptions,
|
||||||
|
) (
|
||||||
|
manifest *Manifest, err error,
|
||||||
|
) {
|
||||||
if opts.BypassCache {
|
if opts.BypassCache {
|
||||||
entry, found := s3.siteCache.Cache.GetEntry(name)
|
entry, found := s3.siteCache.Cache.GetEntry(name)
|
||||||
if found && entry.RefreshableAt().Before(time.Now()) {
|
if found && entry.RefreshableAt().Before(time.Now()) {
|
||||||
@@ -460,11 +466,14 @@ func (s3 *S3Backend) GetManifest(ctx context.Context, name string, opts GetManif
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cached, err := s3.siteCache.Get(ctx, name, s3ManifestLoader{s3})
|
var cached *CachedManifest
|
||||||
|
cached, err = s3.siteCache.Get(ctx, name, s3ManifestLoader{s3})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return
|
||||||
} else {
|
} else {
|
||||||
return cached.manifest, cached.err
|
// This could be `manifest, nil` or `nil, ErrObjectNotFound`.
|
||||||
|
manifest, err = cached.manifest, cached.err
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user