Skip to content

Commit 548a57d

Browse files
authored
hotfix(caddy): dispatch with right values (#663)
* hotfix(caddy): dispatch with right badger storage * bump(deps): plugins * feat(caddy): bump to latest, tricky way with a channel to dispatch the configuration for the surrogate store * bump(deps): plugins * feat(go): bump to v1.25 * feat(caddy): implement validator interface * fix(coalescing): disable on private or Set-Cookie * feat(test-suite): separate caddy unit tests and e2e
1 parent 890943d commit 548a57d

File tree

48 files changed

+7169
-8520
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+7169
-8520
lines changed

.github/workflows/non-regression.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
workflow_dispatch:
66

77
env:
8-
GO_VERSION: '1.24'
8+
GO_VERSION: '1.25'
99

1010
jobs:
1111
lint-validation:

.github/workflows/plugins-master.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- master
77

88
env:
9-
GO_VERSION: '1.24'
9+
GO_VERSION: '1.25'
1010

1111
jobs:
1212
build-caddy-validator:

.github/workflows/plugins.yml

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@ on:
55

66
jobs:
77
build-caddy-validator:
8+
name: Caddy
9+
runs-on: ubuntu-latest
10+
steps:
11+
-
12+
name: Add domain.com host to /etc/hosts
13+
run: |
14+
sudo echo "127.0.0.1 domain.com etcd redis" | sudo tee -a /etc/hosts
15+
-
16+
name: Install Go
17+
uses: actions/setup-go@v3
18+
with:
19+
go-version: '1.25'
20+
-
21+
name: Checkout code
22+
uses: actions/checkout@v4
23+
-
24+
name: Install xcaddy
25+
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
26+
-
27+
name: Build Souin as caddy module
28+
run: cd plugins/caddy && xcaddy build --with github.com/darkweak/souin/plugins/caddy=./ --with github.com/darkweak/souin=../.. --with github.com/darkweak/storages/badger/caddy --with github.com/darkweak/storages/etcd/caddy --with github.com/darkweak/storages/nats/caddy --with github.com/darkweak/storages/nuts/caddy --with github.com/darkweak/storages/olric/caddy --with github.com/darkweak/storages/otter/caddy --with github.com/darkweak/storages/redis/caddy
29+
-
30+
name: Run Caddy tests
31+
run: cd plugins/caddy && go test -v ./...
32+
build-caddy-validator-e2e:
33+
needs: build-caddy-validator
834
name: Caddy
935
runs-on: ubuntu-latest
1036
services:
@@ -36,7 +62,7 @@ jobs:
3662
name: Install Go
3763
uses: actions/setup-go@v3
3864
with:
39-
go-version: '1.24'
65+
go-version: '1.25'
4066
-
4167
name: Checkout code
4268
uses: actions/checkout@v4
@@ -46,9 +72,6 @@ jobs:
4672
-
4773
name: Build Souin as caddy module
4874
run: cd plugins/caddy && xcaddy build --with github.com/darkweak/souin/plugins/caddy=./ --with github.com/darkweak/souin=../.. --with github.com/darkweak/storages/badger/caddy --with github.com/darkweak/storages/etcd/caddy --with github.com/darkweak/storages/nats/caddy --with github.com/darkweak/storages/nuts/caddy --with github.com/darkweak/storages/olric/caddy --with github.com/darkweak/storages/otter/caddy --with github.com/darkweak/storages/redis/caddy
49-
-
50-
name: Run Caddy tests
51-
run: cd plugins/caddy && go test -v ./...
5275
-
5376
name: Run detached caddy
5477
run: cd plugins/caddy && ./caddy run &
@@ -85,7 +108,7 @@ jobs:
85108
name: Install Go
86109
uses: actions/setup-go@v5
87110
with:
88-
go-version: '1.24'
111+
go-version: '1.25'
89112

90113
- name: Install Node.js
91114
uses: actions/setup-node@v4
@@ -158,74 +181,74 @@ jobs:
158181
with:
159182
CAPITALIZED_NAME: Beego
160183
LOWER_NAME: beego
161-
GO_VERSION: '1.24'
184+
GO_VERSION: '1.25'
162185
build-chi-validator:
163186
uses: ./.github/workflows/plugin_template.yml
164187
secrets: inherit
165188
with:
166189
CAPITALIZED_NAME: Chi
167190
LOWER_NAME: chi
168-
GO_VERSION: '1.24'
191+
GO_VERSION: '1.25'
169192
build-dotweb-validator:
170193
uses: ./.github/workflows/plugin_template.yml
171194
secrets: inherit
172195
with:
173196
CAPITALIZED_NAME: Dotweb
174197
LOWER_NAME: dotweb
175-
GO_VERSION: '1.24'
198+
GO_VERSION: '1.25'
176199
build-echo-validator:
177200
uses: ./.github/workflows/plugin_template.yml
178201
secrets: inherit
179202
with:
180203
CAPITALIZED_NAME: Echo
181204
LOWER_NAME: echo
182-
GO_VERSION: '1.24'
205+
GO_VERSION: '1.25'
183206
build-fiber-validator:
184207
uses: ./.github/workflows/plugin_template.yml
185208
secrets: inherit
186209
with:
187210
CAPITALIZED_NAME: Fiber
188211
LOWER_NAME: fiber
189-
GO_VERSION: '1.24'
212+
GO_VERSION: '1.25'
190213
build-gin-validator:
191214
uses: ./.github/workflows/plugin_template.yml
192215
secrets: inherit
193216
with:
194217
CAPITALIZED_NAME: Gin
195218
LOWER_NAME: gin
196-
GO_VERSION: '1.24'
219+
GO_VERSION: '1.25'
197220
build-goa-validator:
198221
uses: ./.github/workflows/plugin_template.yml
199222
secrets: inherit
200223
with:
201224
CAPITALIZED_NAME: Goa
202225
LOWER_NAME: goa
203-
GO_VERSION: '1.24'
226+
GO_VERSION: '1.25'
204227
build-kratos-validator:
205228
uses: ./.github/workflows/plugin_template.yml
206229
secrets: inherit
207230
with:
208231
CAPITALIZED_NAME: Kratos
209232
LOWER_NAME: kratos
210-
GO_VERSION: '1.24'
233+
GO_VERSION: '1.25'
211234
build-souin-validator:
212235
uses: ./.github/workflows/plugin_template.yml
213236
secrets: inherit
214237
with:
215238
CAPITALIZED_NAME: Souin
216239
LOWER_NAME: souin
217-
GO_VERSION: '1.24'
240+
GO_VERSION: '1.25'
218241
build-traefik-validator:
219242
uses: ./.github/workflows/plugin_template.yml
220243
secrets: inherit
221244
with:
222245
CAPITALIZED_NAME: Traefik
223246
LOWER_NAME: traefik
224-
GO_VERSION: '1.24'
247+
GO_VERSION: '1.25'
225248
build-webgo-validator:
226249
uses: ./.github/workflows/plugin_template.yml
227250
secrets: inherit
228251
with:
229252
CAPITALIZED_NAME: Webgo
230253
LOWER_NAME: webgo
231-
GO_VERSION: '1.24'
254+
GO_VERSION: '1.25'

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
tags: ["v*"]
66

77
env:
8-
GO_VERSION: '1.24'
8+
GO_VERSION: '1.25'
99

1010
jobs:
1111
generate-souin-traefik-docker:

.github/workflows/workflow_plugins_generator.sh

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Disabled go-zero and hertz temporary
44
# plugins=("beego" "chi" "dotweb" "echo" "fiber" "gin" "goa" "go-zero" "hertz" "kratos" "roadrunner" "souin" "traefik" "tyk" "webgo")
55
plugins=("beego" "chi" "dotweb" "echo" "fiber" "gin" "goa" "kratos" "souin" "traefik" "webgo")
6-
go_version=1.24
6+
go_version=1.25
77

88
IFS= read -r -d '' tpl <<EOF
99
name: Build and validate Souin as plugins
@@ -13,6 +13,32 @@ on:
1313
1414
jobs:
1515
build-caddy-validator:
16+
name: Caddy
17+
runs-on: ubuntu-latest
18+
steps:
19+
-
20+
name: Add domain.com host to /etc/hosts
21+
run: |
22+
sudo echo "127.0.0.1 domain.com etcd redis" | sudo tee -a /etc/hosts
23+
-
24+
name: Install Go
25+
uses: actions/setup-go@v3
26+
with:
27+
go-version: '$go_version'
28+
-
29+
name: Checkout code
30+
uses: actions/checkout@v4
31+
-
32+
name: Install xcaddy
33+
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
34+
-
35+
name: Build Souin as caddy module
36+
run: cd plugins/caddy && xcaddy build --with github.com/darkweak/souin/plugins/caddy=./ --with github.com/darkweak/souin=../.. --with github.com/darkweak/storages/badger/caddy --with github.com/darkweak/storages/etcd/caddy --with github.com/darkweak/storages/nats/caddy --with github.com/darkweak/storages/nuts/caddy --with github.com/darkweak/storages/olric/caddy --with github.com/darkweak/storages/otter/caddy --with github.com/darkweak/storages/redis/caddy
37+
-
38+
name: Run Caddy tests
39+
run: cd plugins/caddy && go test -v ./...
40+
build-caddy-validator-e2e:
41+
needs: build-caddy-validator
1642
name: Caddy
1743
runs-on: ubuntu-latest
1844
services:
@@ -54,9 +80,6 @@ jobs:
5480
-
5581
name: Build Souin as caddy module
5682
run: cd plugins/caddy && xcaddy build --with github.com/darkweak/souin/plugins/caddy=./ --with github.com/darkweak/souin=../.. --with github.com/darkweak/storages/badger/caddy --with github.com/darkweak/storages/etcd/caddy --with github.com/darkweak/storages/nats/caddy --with github.com/darkweak/storages/nuts/caddy --with github.com/darkweak/storages/olric/caddy --with github.com/darkweak/storages/otter/caddy --with github.com/darkweak/storages/redis/caddy
57-
-
58-
name: Run Caddy tests
59-
run: cd plugins/caddy && go test -v ./...
6083
-
6184
name: Run detached caddy
6285
run: cd plugins/caddy && ./caddy run &
@@ -93,7 +116,7 @@ jobs:
93116
name: Install Go
94117
uses: actions/setup-go@v5
95118
with:
96-
go-version: '1.24'
119+
go-version: '1.25'
97120
98121
- name: Install Node.js
99122
uses: actions/setup-node@v4

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/darkweak/souin
22

3-
go 1.24
4-
5-
toolchain go1.24.2
3+
go 1.25
64

75
require (
86
github.com/caddyserver/caddy/v2 v2.10.0

pkg/middleware/middleware.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,11 @@ func (s *SouinBaseHandler) Store(
451451
}
452452

453453
type singleflightValue struct {
454-
body []byte
455-
headers http.Header
456-
requestHeaders http.Header
457-
code int
454+
body []byte
455+
headers http.Header
456+
requestHeaders http.Header
457+
code int
458+
disableCoalescing bool
458459
}
459460

460461
func (s *SouinBaseHandler) Upstream(
@@ -464,6 +465,7 @@ func (s *SouinBaseHandler) Upstream(
464465
requestCc *cacheobject.RequestCacheDirectives,
465466
cachedKey string,
466467
uri string,
468+
disableCoalescing bool,
467469
) error {
468470
s.Configuration.GetLogger().Debug("Request the upstream server")
469471
prometheus.Increment(prometheus.RequestCounter)
@@ -484,7 +486,7 @@ func (s *SouinBaseHandler) Upstream(
484486
}()
485487

486488
singleflightCacheKey := cachedKey
487-
if s.Configuration.GetDefaultCache().IsCoalescingDisable() {
489+
if s.Configuration.GetDefaultCache().IsCoalescingDisable() || disableCoalescing {
488490
singleflightCacheKey += uuid.NewString()
489491
}
490492
sfValue, err, shared := s.singleflightPool.Do(singleflightCacheKey, func() (interface{}, error) {
@@ -519,10 +521,11 @@ func (s *SouinBaseHandler) Upstream(
519521
})
520522

521523
return singleflightValue{
522-
body: customWriter.Buf.Bytes(),
523-
headers: customWriter.Header().Clone(),
524-
requestHeaders: rq.Header,
525-
code: statusCode,
524+
body: customWriter.Buf.Bytes(),
525+
headers: customWriter.Header().Clone(),
526+
requestHeaders: rq.Header,
527+
code: statusCode,
528+
disableCoalescing: strings.Contains(cacheControl, "private") || customWriter.Header().Get("Set-Cookie") != "",
526529
}, err
527530
})
528531
if recoveredFromErr != nil {
@@ -533,17 +536,22 @@ func (s *SouinBaseHandler) Upstream(
533536
}
534537

535538
if sfWriter, ok := sfValue.(singleflightValue); ok {
539+
if shared && sfWriter.disableCoalescing {
540+
return s.Upstream(customWriter, rq, next, requestCc, cachedKey, uri, true)
541+
}
542+
536543
if vary := sfWriter.headers.Get("Vary"); vary != "" {
537544
variedHeaders, isVaryStar := rfc.VariedHeaderAllCommaSepValues(sfWriter.headers)
538545
if !isVaryStar {
539546
for _, vh := range variedHeaders {
540547
if rq.Header.Get(vh) != sfWriter.requestHeaders.Get(vh) {
541548
// cachedKey += rfc.GetVariedCacheKey(rq, variedHeaders)
542-
return s.Upstream(customWriter, rq, next, requestCc, cachedKey, uri)
549+
return s.Upstream(customWriter, rq, next, requestCc, cachedKey, uri, false)
543550
}
544551
}
545552
}
546553
}
554+
547555
if shared {
548556
s.Configuration.GetLogger().Infof("Reused response from concurrent request with the key %s", cachedKey)
549557
}
@@ -971,7 +979,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n
971979
}
972980
}()
973981
prometheus.Increment(prometheus.NoCachedResponseCounter)
974-
errorCacheCh <- s.Upstream(cw, vr, next, requestCc, cachedKey, uri)
982+
errorCacheCh <- s.Upstream(cw, vr, next, requestCc, cachedKey, uri, false)
975983
}(req, customWriter)
976984

977985
select {

0 commit comments

Comments
 (0)