Skip to content

Commit

Permalink
fix(middleware): order storages with configuration (#569)
Browse files Browse the repository at this point in the history
* fix(middleware): order storages with configuration

* fix(race-condition): pass response as goroutine param

* fix(typo): log cache timeout

* fix(key): return stars to show key is hidden
  • Loading branch information
darkweak authored Nov 5, 2024
1 parent 3c6d62a commit 1c21a86
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion context/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions docs/e2e/Souin E2E.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -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) {",
Expand All @@ -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\");",
"",
Expand Down
29 changes: 24 additions & 5 deletions pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -331,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()
Expand Down
2 changes: 1 addition & 1 deletion pkg/rfc/cache_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func GetCacheKeyFromCtx(currentCtx ctx.Context) string {
}
}

return ""
return "*****"
}

// MissCache set miss fwd
Expand Down

0 comments on commit 1c21a86

Please sign in to comment.