Skip to content

Commit

Permalink
HSH: Limit the number of ban checks before vary matching
Browse files Browse the repository at this point in the history
Refs: #4236
  • Loading branch information
walid-git committed Jan 15, 2025
1 parent 63a46b7 commit 0c87a12
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
12 changes: 11 additions & 1 deletion bin/varnishd/cache/cache_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ HSH_Lookup(struct req *req, struct objcore **ocp, struct objcore **bocp)
const uint8_t *vary;
intmax_t boc_progress;
unsigned xid = 0;
unsigned ban_checks;
float dttl = 0.0;

AN(ocp);
Expand Down Expand Up @@ -420,6 +421,7 @@ HSH_Lookup(struct req *req, struct objcore **ocp, struct objcore **bocp)
busy_found = 0;
exp_oc = NULL;
exp_t_origin = 0.0;
ban_checks = 0;
VTAILQ_FOREACH(oc, &oh->objcs, hsh_list) {
/* Must be at least our own ref + the objcore we examine */
assert(oh->refcnt > 1);
Expand Down Expand Up @@ -451,7 +453,8 @@ HSH_Lookup(struct req *req, struct objcore **ocp, struct objcore **bocp)
if (oc->ttl <= 0.)
continue;

if (BAN_CheckObject(wrk, oc, req)) {
if (ban_checks++ < cache_param->ban_variant_checks
&& BAN_CheckObject(wrk, oc, req)) {
oc->flags |= OC_F_DYING;
EXP_Remove(oc, NULL);
continue;
Expand All @@ -466,6 +469,13 @@ HSH_Lookup(struct req *req, struct objcore **ocp, struct objcore **bocp)
}
}

if (ban_checks >= cache_param->ban_variant_checks
&& BAN_CheckObject(wrk, oc, req)) {
oc->flags |= OC_F_DYING;
EXP_Remove(oc, NULL);
continue;
}

if (req->vcf != NULL) {
vr = req->vcf->func(req, &oc, &exp_oc, 0);
if (vr == VCF_CONTINUE)
Expand Down
41 changes: 41 additions & 0 deletions bin/varnishtest/tests/c00133.vtc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
varnishtest "test ban + vary behavior"

server s0 {
rxreq
txresp -hdr "vary: version" -body "Variant A"
rxreq
txresp -hdr "vary: version" -body "Variant B"
rxreq
txresp -hdr "vary: version" -body "New variant A"
rxreq
txresp -hdr "vary: version" -body "New variant B"
} -start

varnish v1 -arg "-p ban_variant_checks=0" -vcl+backend {} -start

client c1 {
txreq -hdr "version: a"
rxresp
expect resp.body == "Variant A"
} -run

client c2 {
txreq -hdr "version: b"
rxresp
expect resp.body == "Variant B"
} -run

varnish v1 -cliok "ban req.http.version == a"

# Should this remove a single variant from cache
client c3 {
txreq -hdr "version: a"
rxresp
expect resp.body == "New variant A"
} -run

client c4 {
txreq -hdr "version: b"
rxresp
expect resp.body == "Variant B"
} -run

0 comments on commit 0c87a12

Please sign in to comment.