From acfce68f45c90df82c77fcb15206480db9c424a4 Mon Sep 17 00:00:00 2001 From: darkweak Date: Tue, 29 Oct 2024 21:04:39 +0100 Subject: [PATCH 1/4] fix(middleware): order storages with configuration --- pkg/middleware/middleware.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/middleware/middleware.go b/pkg/middleware/middleware.go index 4b4295d74..e1f9a371d 100644 --- a/pkg/middleware/middleware.go +++ b/pkg/middleware/middleware.go @@ -33,6 +33,23 @@ import ( "golang.org/x/sync/singleflight" ) +func reorderStorers(storers []types.Storer, expectedStorers []string) []types.Storer { + if len(expectedStorers) == 0 { + return storers + } + + newStorers := make([]types.Storer, 0) + for _, expectedStorer := range expectedStorers { + for _, storer := range storers { + if storer.Name() == strings.ToUpper(expectedStorer) { + newStorers = append(newStorers, storer) + } + } + } + + return newStorers +} + func NewHTTPCacheHandler(c configurationtypes.AbstractConfigurationInterface) *SouinBaseHandler { if c.GetLogger() == nil { var logLevel zapcore.Level @@ -75,12 +92,14 @@ func NewHTTPCacheHandler(c configurationtypes.AbstractConfigurationInterface) *S } } + storers = reorderStorers(storers, c.GetDefaultCache().GetStorers()) + if len(storers) > 0 { names := []string{} for _, storer := range storers { names = append(names, storer.Name()) } - c.GetLogger().Debugf("You're running Souin with the following storages %s", strings.Join(names, ", ")) + c.GetLogger().Debugf("You're running Souin with the following storages in this order %s", strings.Join(names, ", ")) } } if len(storers) == 0 { From d86c93aed532068bc3d0218793e3800d05240b84 Mon Sep 17 00:00:00 2001 From: darkweak Date: Tue, 29 Oct 2024 22:09:22 +0100 Subject: [PATCH 2/4] fix(race-condition): pass response as goroutine param --- pkg/middleware/middleware.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/middleware/middleware.go b/pkg/middleware/middleware.go index e1f9a371d..411957c4f 100644 --- a/pkg/middleware/middleware.go +++ b/pkg/middleware/middleware.go @@ -350,24 +350,24 @@ func (s *SouinBaseHandler) Store( } for _, storer := range s.Storers { wg.Add(1) - go func(currentStorer types.Storer) { + go func(currentStorer types.Storer, currentRes http.Response) { defer wg.Done() if currentStorer.SetMultiLevel( cachedKey, variedKey, response, vhs, - res.Header.Get("Etag"), ma, + currentRes.Header.Get("Etag"), ma, variedKey, ) == nil { s.Configuration.GetLogger().Debugf("Stored the key %s in the %s provider", variedKey, currentStorer.Name()) - res.Request = rq + currentRes.Request = rq } else { mu.Lock() fails = append(fails, fmt.Sprintf("; detail=%s-INSERTION-ERROR", currentStorer.Name())) mu.Unlock() } - }(storer) + }(storer, res) } wg.Wait() From a72ca349b70a60712bc2f1141c2851974ec2479b Mon Sep 17 00:00:00 2001 From: darkweak Date: Sun, 3 Nov 2024 19:16:57 +0100 Subject: [PATCH 3/4] fix(typo): log cache timeout --- context/timeout.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context/timeout.go b/context/timeout.go index f5a816a6f..a38fb593e 100644 --- a/context/timeout.go +++ b/context/timeout.go @@ -36,7 +36,7 @@ func (t *timeoutContext) SetupContext(c configurationtypes.AbstractConfiguration t.timeoutBackend = c.GetDefaultCache().GetTimeout().Backend.Duration } c.GetLogger().Infof("Set backend timeout to %v", t.timeoutBackend) - c.GetLogger().Infof("Set cache timeout to %v", t.timeoutBackend) + c.GetLogger().Infof("Set cache timeout to %v", t.timeoutCache) } func (t *timeoutContext) SetContext(req *http.Request) *http.Request { From bdcf017d66c93cfd57893ef28df48d2e94d61b56 Mon Sep 17 00:00:00 2001 From: darkweak Date: Tue, 5 Nov 2024 21:34:35 +0100 Subject: [PATCH 4/4] fix(key): return stars to show key is hidden --- docs/e2e/Souin E2E.postman_collection.json | 4 ++-- pkg/rfc/cache_status.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/e2e/Souin E2E.postman_collection.json b/docs/e2e/Souin E2E.postman_collection.json index 9c8aba953..2c118f9ba 100644 --- a/docs/e2e/Souin E2E.postman_collection.json +++ b/docs/e2e/Souin E2E.postman_collection.json @@ -3498,7 +3498,7 @@ " pm.response.to.have.status(200);", " if (exclude) {", " pm.response.to.have.header(\"Cache-Status\");", - " pm.expect(pm.response.headers.get(\"Cache-Status\")).to.eql(\"Souin; fwd=uri-miss; key=; detail=CANNOT-HANDLE\");", + " pm.expect(pm.response.headers.get(\"Cache-Status\")).to.eql(\"Souin; fwd=uri-miss; key=*****; detail=CANNOT-HANDLE\");", " } else {", " pm.response.to.have.header(\"Cache-Status\");", " if (isStore) {", @@ -3516,7 +3516,7 @@ " expected.to.have.status(200);", " if (exclude) {", " pm.response.to.have.header(\"Cache-Status\");", - " pm.expect(pm.response.headers.get(\"Cache-Status\")).to.eql(\"Souin; fwd=uri-miss; key=; detail=CANNOT-HANDLE\");", + " pm.expect(pm.response.headers.get(\"Cache-Status\")).to.eql(\"Souin; fwd=uri-miss; key=*****; detail=CANNOT-HANDLE\");", " } else {", " expected.to.have.header(\"Cache-Status\");", "", diff --git a/pkg/rfc/cache_status.go b/pkg/rfc/cache_status.go index f02132fb7..804f8da16 100644 --- a/pkg/rfc/cache_status.go +++ b/pkg/rfc/cache_status.go @@ -67,7 +67,7 @@ func GetCacheKeyFromCtx(currentCtx ctx.Context) string { } } - return "" + return "*****" } // MissCache set miss fwd