Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(middleware): order storages with configuration #569

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading