diff --git a/internal/httprange/http_reader.go b/internal/httprange/http_reader.go index 44694e8587977823405a4e125397827d818a2225..c573781397c3e8753f1c45724570b1baf2bae5fa 100644 --- a/internal/httprange/http_reader.go +++ b/internal/httprange/http_reader.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "net/http/httputil" "time" "gitlab.com/gitlab-org/gitlab-pages/internal/httptransport" @@ -76,12 +77,18 @@ func (r *Reader) ensureResponse() error { metrics.HTTPRangeOpenRequests.Inc() + dreq, err := httputil.DumpRequestOut(req, true) + fmt.Printf("req: %s err: %+v\n", dreq, err) + res, err := httpClient.Do(req) if err != nil { metrics.HTTPRangeOpenRequests.Dec() return err } + dres, err := httputil.DumpResponse(res, false) + fmt.Printf("res: %s err: %+v\n", dres, err) + err = r.setResponse(res) if err != nil { metrics.HTTPRangeOpenRequests.Dec() diff --git a/internal/vfs/zip/archive.go b/internal/vfs/zip/archive.go index 548ba6518812358f9cd4fd4d27be58ab8f09d2ce..7846c8788772adbfd2f1e9c681ae395c469c7334 100644 --- a/internal/vfs/zip/archive.go +++ b/internal/vfs/zip/archive.go @@ -247,3 +247,11 @@ func (a *zipArchive) Readlink(ctx context.Context, name string) (string, error) func (a *zipArchive) onEvicted() { metrics.ZipArchiveEntriesCached.Sub(float64(len(a.files))) } + +func (a *zipArchive) Hash() string { + if a.resource == nil { + return "" + } + + return a.resource.ETag +} diff --git a/internal/vfs/zip/vfs.go b/internal/vfs/zip/vfs.go index 78a77e1cb374a3cd9ff20701c78756760522844c..82e63d72160a078dfbecc2453e014030ffe36b82 100644 --- a/internal/vfs/zip/vfs.go +++ b/internal/vfs/zip/vfs.go @@ -4,11 +4,13 @@ import ( "context" "errors" "net/url" + "strings" "sync" "time" "github.com/patrickmn/go-cache" + "gitlab.com/gitlab-org/gitlab-pages/internal/httprange" "gitlab.com/gitlab-org/gitlab-pages/internal/vfs" "gitlab.com/gitlab-org/gitlab-pages/metrics" ) @@ -101,14 +103,30 @@ func (fs *zipVFS) findOrCreateArchive(ctx context.Context, path string) (*zipArc fs.cacheLock.Lock() defer fs.cacheLock.Unlock() - archive, expiry, found := fs.cache.GetWithExpiration(path) + key := strings.TrimRight(path, "?") + archive, expiry, found := fs.cache.GetWithExpiration(key) if found { metrics.ZipCacheRequests.WithLabelValues("archive", "hit").Inc() // TODO: do not refreshed errored archives https://gitlab.com/gitlab-org/gitlab-pages/-/merge_requests/351 if time.Until(expiry) < defaultCacheRefreshInterval { - // refresh item - fs.cache.SetDefault(path, archive) + + zipArchive := archive.(*zipArchive) + + res, err := httprange.NewResource(ctx, path) + if err != nil { + err := zipArchive.openArchive(ctx) + if err != nil { + return nil, err + } + // do not refresh on error + return zipArchive, nil + } + + if res.ETag != zipArchive.Hash() { + // refresh item with new path + fs.cache.SetDefault(key, archive) + } } } else { archive = newArchive(fs, path, DefaultOpenTimeout) @@ -120,7 +138,7 @@ func (fs *zipVFS) findOrCreateArchive(ctx context.Context, path string) (*zipArc // if adding the archive to the cache fails it means it's already been added before // this is done to find concurrent additions. - if fs.cache.Add(path, archive, cache.DefaultExpiration) != nil { + if fs.cache.Add(key, archive, cache.DefaultExpiration) != nil { return nil, errAlreadyCached }