Skip to content

Commit

Permalink
fix(chore): surrogate use Content-Location instead of generated key
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Sep 2, 2024
1 parent bf786a2 commit 71ade83
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/generate_release.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

plugins=("beego" "caddy" "chi" "dotweb" "echo" "fiber" "gin" "go-zero" "goa" "goyave" "hertz" "kratos" "roadrunner" "skipper" "souin" "traefik" "tyk" "webgo")
plugins=("beego" "caddy" "chi" "dotweb" "echo" "fiber" "gin" "go-zero" "goa" "goyave" "hertz" "kratos" "roadrunner" "skipper" "souin" "souin/storages" "traefik" "tyk" "webgo")

IFS= read -r -d '' tpl <<EOF
name: Tag submodules on release
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/release_plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ jobs:
ref: 'refs/tags/plugins/souin/${{ github.ref_name }}',
sha: context.sha
})
-
name: Create Souin/storages tag
uses: actions/github-script@v7
with:
script: |
github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: 'refs/tags/plugins/souin/storages/${{ github.ref_name }}',
sha: context.sha
})
-
name: Create Traefik tag
uses: actions/github-script@v7
Expand Down
6 changes: 3 additions & 3 deletions pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,9 @@ func (s *SouinBaseHandler) Store(

wg.Wait()
if len(fails) < s.storersLen {
go func(rs http.Response, key string, basekey string) {
_ = s.SurrogateKeyStorer.Store(&rs, key, uri, basekey)
}(res, variedKey, cachedKey)
go func(rs http.Response, key string) {
_ = s.SurrogateKeyStorer.Store(&rs, key, uri)
}(res, variedKey)
status += "; stored"
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/surrogate/providers/akamai.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func (*AkamaiSurrogateStorage) getHeaderSeparator() string {
}

// Store stores the response tags located in the first non empty supported header
func (a *AkamaiSurrogateStorage) Store(response *http.Response, cacheKey, uri, basekey string) error {
func (a *AkamaiSurrogateStorage) Store(response *http.Response, cacheKey, uri string) error {
defer func() {
response.Header.Del(surrogateKey)
response.Header.Del(surrogateControl)
}()
e := a.baseStorage.Store(response, cacheKey, uri, basekey)
e := a.baseStorage.Store(response, cacheKey, uri)
response.Header.Set(edgeCacheTag, response.Header.Get(surrogateKey))

return e
Expand Down
4 changes: 2 additions & 2 deletions pkg/surrogate/providers/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func (*CloudflareSurrogateStorage) getHeaderSeparator() string {
}

// Store stores the response tags located in the first non empty supported header
func (c *CloudflareSurrogateStorage) Store(response *http.Response, cacheKey, uri, basekey string) error {
func (c *CloudflareSurrogateStorage) Store(response *http.Response, cacheKey, uri string) error {
defer func() {
response.Header.Del(surrogateKey)
response.Header.Del(surrogateControl)
}()
e := c.baseStorage.Store(response, cacheKey, uri, basekey)
e := c.baseStorage.Store(response, cacheKey, uri)
response.Header.Set(cacheTag, strings.Join(c.ParseHeaders(response.Header.Get(surrogateKey)), c.getHeaderSeparator()))

return e
Expand Down
9 changes: 6 additions & 3 deletions pkg/surrogate/providers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (s *baseStorage) purgeTag(tag string) []string {
}

// Store will take the lead to store the cache key for each provided Surrogate-key
func (s *baseStorage) Store(response *http.Response, cacheKey, uri, basekey string) error {
func (s *baseStorage) Store(response *http.Response, cacheKey, uri string) error {
h := response.Header

cacheKey = url.QueryEscape(cacheKey)
Expand Down Expand Up @@ -238,8 +238,11 @@ func (s *baseStorage) Store(response *http.Response, cacheKey, uri, basekey stri
}
}

urlRegexp = regexp.MustCompile("(^|" + regexp.QuoteMeta(souinStorageSeparator) + ")" + regexp.QuoteMeta(basekey) + "(" + regexp.QuoteMeta(souinStorageSeparator) + "|$)")
s.storeTag(uri, basekey, urlRegexp)
if h.Get("Content-Location") != "" {
location := h.Get("Content-Location")
urlRegexp = regexp.MustCompile("(^|" + regexp.QuoteMeta(souinStorageSeparator) + ")" + regexp.QuoteMeta(location) + "(" + regexp.QuoteMeta(souinStorageSeparator) + "|$)")
s.storeTag(uri, location, urlRegexp)
}
s.storeTag(uri, cacheKey, urlRegexp)

return nil
Expand Down
12 changes: 6 additions & 6 deletions pkg/surrogate/providers/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestBaseStorage_Store(t *testing.T) {

bs := mockCommonProvider()

e := bs.Store(&res, "((((invalid_key_but_escaped", "", "")
e := bs.Store(&res, "((((invalid_key_but_escaped", "")
if e != nil {
t.Error("It shouldn't throw an error with a valid key.")
}
Expand All @@ -116,7 +116,7 @@ func TestBaseStorage_Store(t *testing.T) {
_ = bs.Storage.Set("test5", []byte("first,second,fifth"), storageToInfiniteTTLMap[bs.Storage.Name()])
_ = bs.Storage.Set("testInvalid", []byte("invalid"), storageToInfiniteTTLMap[bs.Storage.Name()])

if e = bs.Store(&res, "stored", "", ""); e != nil {
if e = bs.Store(&res, "stored", ""); e != nil {
t.Error("It shouldn't throw an error with a valid key.")
}

Expand All @@ -133,10 +133,10 @@ func TestBaseStorage_Store(t *testing.T) {
}

res.Header.Set(surrogateKey, "something")
_ = bs.Store(&res, "/something", "", "")
_ = bs.Store(&res, "/something", "", "")
_ = bs.Store(&res, "/something", "")
_ = bs.Store(&res, "/something", "")
res.Header.Set(surrogateKey, "something")
_ = bs.Store(&res, "/some", "", "")
_ = bs.Store(&res, "/some", "")

_ = len(bs.Storage.MapKeys(surrogatePrefix))
// if storageSize != 6 {
Expand All @@ -161,7 +161,7 @@ func TestBaseStorage_Store_Load(t *testing.T) {
wg.Add(1)
go func(r http.Response, iteration int, group *sync.WaitGroup) {
defer wg.Done()
_ = bs.Store(&r, fmt.Sprintf("my_dynamic_cache_key_%d", iteration), "", "")
_ = bs.Store(&r, fmt.Sprintf("my_dynamic_cache_key_%d", iteration), "")
}(res, i, &wg)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/surrogate/providers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type SurrogateInterface interface {
Purge(http.Header) (cacheKeys []string, surrogateKeys []string)
Invalidate(method string, h http.Header)
purgeTag(string) []string
Store(*http.Response, string, string, string) error
Store(*http.Response, string, string) error
storeTag(string, string, *regexp.Regexp)
ParseHeaders(string) []string
List() map[string]string
Expand Down
48 changes: 48 additions & 0 deletions plugins/caddy/httpcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1038,3 +1038,51 @@ func TestExpires(t *testing.T) {
cacheChecker(caddyTester, "/expires-with-max-age", "Hello, expires-with-max-age!", 59)
cacheChecker(caddyTester, "/expires-with-s-maxage", "Hello, expires-with-s-maxage!", 4)
}

func TestComplexQuery(t *testing.T) {
caddyTester := caddytest.NewTester(t)
caddyTester.InitServer(`
{
admin localhost:2999
http_port 9080
https_port 9443
cache {
ttl 10s
}
}
localhost:9080 {
route /complex-query {
cache
respond "Hello, {query}!"
}
}`, "caddyfile")

cacheChecker := func(tester *caddytest.Tester, query string, expectedDuration int) {
body := fmt.Sprintf("Hello, %s!", query)
resp1, _ := tester.AssertGetResponse("http://localhost:9080/complex-query?"+query, 200, body)
if resp1.Header.Get("Age") != "" {
t.Errorf("unexpected Age header %v", resp1.Header.Get("Age"))
}

if resp1.Header.Get("Cache-Status") != "Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/complex-query?"+query {
t.Errorf("unexpected first Cache-Status header %v", resp1.Header.Get("Cache-Status"))
}

resp1, _ = tester.AssertGetResponse("http://localhost:9080/complex-query?"+query, 200, body)

if resp1.Header.Get("Age") != "1" {
t.Errorf("unexpected Age header %v", resp1.Header.Get("Age"))
}

if resp1.Header.Get("Cache-Status") != fmt.Sprintf("Souin; hit; ttl=%d; key=GET-http-localhost:9080-/complex-query?%s; detail=DEFAULT", expectedDuration, query) {
t.Errorf(
"unexpected second Cache-Status header %v, expected %s",
resp1.Header.Get("Cache-Status"),
fmt.Sprintf("Souin; hit; ttl=%d; key=GET-http-localhost:9080-/complex-query?%s; detail=DEFAULT", expectedDuration, query),
)
}
}

cacheChecker(caddyTester, "fields[]=id&pagination=true", 9)
cacheChecker(caddyTester, "fields[]=id&pagination=false", 9)
}
2 changes: 2 additions & 0 deletions plugins/go-zero/examples/internal/handler/routes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/souin/storages/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.1
replace github.com/darkweak/souin => ../../..

require (
github.com/darkweak/souin v1.6.49
github.com/darkweak/souin v1.6.50
github.com/darkweak/storages/badger v0.0.8
github.com/darkweak/storages/core v0.0.8
github.com/darkweak/storages/etcd v0.0.8
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 71ade83

Please sign in to comment.