Skip to content

Commit 1c21a86

Browse files
authored
fix(middleware): order storages with configuration (#569)
* 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
1 parent 3c6d62a commit 1c21a86

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

context/timeout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (t *timeoutContext) SetupContext(c configurationtypes.AbstractConfiguration
3636
t.timeoutBackend = c.GetDefaultCache().GetTimeout().Backend.Duration
3737
}
3838
c.GetLogger().Infof("Set backend timeout to %v", t.timeoutBackend)
39-
c.GetLogger().Infof("Set cache timeout to %v", t.timeoutBackend)
39+
c.GetLogger().Infof("Set cache timeout to %v", t.timeoutCache)
4040
}
4141

4242
func (t *timeoutContext) SetContext(req *http.Request) *http.Request {

docs/e2e/Souin E2E.postman_collection.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3498,7 +3498,7 @@
34983498
" pm.response.to.have.status(200);",
34993499
" if (exclude) {",
35003500
" pm.response.to.have.header(\"Cache-Status\");",
3501-
" pm.expect(pm.response.headers.get(\"Cache-Status\")).to.eql(\"Souin; fwd=uri-miss; key=; detail=CANNOT-HANDLE\");",
3501+
" pm.expect(pm.response.headers.get(\"Cache-Status\")).to.eql(\"Souin; fwd=uri-miss; key=*****; detail=CANNOT-HANDLE\");",
35023502
" } else {",
35033503
" pm.response.to.have.header(\"Cache-Status\");",
35043504
" if (isStore) {",
@@ -3516,7 +3516,7 @@
35163516
" expected.to.have.status(200);",
35173517
" if (exclude) {",
35183518
" pm.response.to.have.header(\"Cache-Status\");",
3519-
" pm.expect(pm.response.headers.get(\"Cache-Status\")).to.eql(\"Souin; fwd=uri-miss; key=; detail=CANNOT-HANDLE\");",
3519+
" pm.expect(pm.response.headers.get(\"Cache-Status\")).to.eql(\"Souin; fwd=uri-miss; key=*****; detail=CANNOT-HANDLE\");",
35203520
" } else {",
35213521
" expected.to.have.header(\"Cache-Status\");",
35223522
"",

pkg/middleware/middleware.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ import (
3333
"golang.org/x/sync/singleflight"
3434
)
3535

36+
func reorderStorers(storers []types.Storer, expectedStorers []string) []types.Storer {
37+
if len(expectedStorers) == 0 {
38+
return storers
39+
}
40+
41+
newStorers := make([]types.Storer, 0)
42+
for _, expectedStorer := range expectedStorers {
43+
for _, storer := range storers {
44+
if storer.Name() == strings.ToUpper(expectedStorer) {
45+
newStorers = append(newStorers, storer)
46+
}
47+
}
48+
}
49+
50+
return newStorers
51+
}
52+
3653
func NewHTTPCacheHandler(c configurationtypes.AbstractConfigurationInterface) *SouinBaseHandler {
3754
if c.GetLogger() == nil {
3855
var logLevel zapcore.Level
@@ -75,12 +92,14 @@ func NewHTTPCacheHandler(c configurationtypes.AbstractConfigurationInterface) *S
7592
}
7693
}
7794

95+
storers = reorderStorers(storers, c.GetDefaultCache().GetStorers())
96+
7897
if len(storers) > 0 {
7998
names := []string{}
8099
for _, storer := range storers {
81100
names = append(names, storer.Name())
82101
}
83-
c.GetLogger().Debugf("You're running Souin with the following storages %s", strings.Join(names, ", "))
102+
c.GetLogger().Debugf("You're running Souin with the following storages in this order %s", strings.Join(names, ", "))
84103
}
85104
}
86105
if len(storers) == 0 {
@@ -331,24 +350,24 @@ func (s *SouinBaseHandler) Store(
331350
}
332351
for _, storer := range s.Storers {
333352
wg.Add(1)
334-
go func(currentStorer types.Storer) {
353+
go func(currentStorer types.Storer, currentRes http.Response) {
335354
defer wg.Done()
336355
if currentStorer.SetMultiLevel(
337356
cachedKey,
338357
variedKey,
339358
response,
340359
vhs,
341-
res.Header.Get("Etag"), ma,
360+
currentRes.Header.Get("Etag"), ma,
342361
variedKey,
343362
) == nil {
344363
s.Configuration.GetLogger().Debugf("Stored the key %s in the %s provider", variedKey, currentStorer.Name())
345-
res.Request = rq
364+
currentRes.Request = rq
346365
} else {
347366
mu.Lock()
348367
fails = append(fails, fmt.Sprintf("; detail=%s-INSERTION-ERROR", currentStorer.Name()))
349368
mu.Unlock()
350369
}
351-
}(storer)
370+
}(storer, res)
352371
}
353372

354373
wg.Wait()

pkg/rfc/cache_status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func GetCacheKeyFromCtx(currentCtx ctx.Context) string {
6767
}
6868
}
6969

70-
return ""
70+
return "*****"
7171
}
7272

7373
// MissCache set miss fwd

0 commit comments

Comments
 (0)