Skip to content

Commit

Permalink
vbf: implement vcl_backend_refresh logic
Browse files Browse the repository at this point in the history
  • Loading branch information
walid-git committed Oct 6, 2023
1 parent f9c1306 commit 319b37d
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 4 deletions.
31 changes: 27 additions & 4 deletions bin/varnishd/cache/cache_fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ vbf_expected_304(struct busyobj *bo)
static const struct fetch_step * v_matchproto_(vbf_state_f)
vbf_stp_startfetch(struct worker *wrk, struct busyobj *bo)
{
int i;
int i, skip_vbr = 0;
vtim_real now;
unsigned handling;
struct objcore *oc;
Expand Down Expand Up @@ -498,14 +498,37 @@ vbf_stp_startfetch(struct worker *wrk, struct busyobj *bo)
if (http_IsStatus(bo->beresp, 304)) {
if (!vbf_expected_304(bo))
return (F_STP_ERROR);
vbf_304_logic(bo);

bo->was_304 = 1;
VCL_backend_refresh_method(bo->vcl, wrk, NULL, bo, NULL);
switch (wrk->vpi->handling) {
case VCL_RET_MERGE:
vbf_304_logic(bo);
break;
case VCL_RET_BERESP:
break;
case VCL_RET_OBJ_STALE:
HTTP_Decode(bo->beresp, ObjGetAttr(bo->wrk, bo->stale_oc, OA_HEADERS, NULL));
break;
case VCL_RET_ERROR:
/* FALLTHROUGH */
case VCL_RET_ABANDON:
/* FALLTHROUGH */
case VCL_RET_FAIL:
/* FALLTHROUGH */
case VCL_RET_RETRY:
skip_vbr = 1;
break;
default:
WRONG("Illegal return from vcl_backend_refresh{}");
}
}

if (bo->htc != NULL && bo->htc->doclose == SC_NULL &&
http_GetHdrField(bo->bereq, H_Connection, "close", NULL))
bo->htc->doclose = SC_REQ_CLOSE;

VCL_backend_response_method(bo->vcl, wrk, NULL, bo, NULL);
if (!skip_vbr)
VCL_backend_response_method(bo->vcl, wrk, NULL, bo, NULL);

if (bo->htc != NULL && bo->htc->doclose == SC_NULL &&
http_GetHdrField(bo->beresp, H_Connection, "close", NULL))
Expand Down
115 changes: 115 additions & 0 deletions bin/varnishtest/tests/b00082.vtc
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
varnishtest "Test vcl_backend_refresh"

server s1 {
rxreq
txresp -hdr "Etag: abcd" -hdr "from-bo: true" -bodylen 10
rxreq
expect req.http.if-none-match == "abcd"
txresp -status 304 -hdr "be304-1: true"
rxreq
expect req.http.if-none-match == "abcd"
txresp -status 304 -hdr "be304-2: true"
rxreq
expect req.http.if-none-match == "abcd"
txresp -status 304 -hdr "be304-3: true"
} -start

varnish v1 -vcl+backend {

sub vcl_backend_response {
set beresp.http.vbresp = "true";
set beresp.ttl = 0.01s;
set beresp.grace = 0s;
set beresp.keep = 10m;
set beresp.http.was-304 = beresp.was_304;
}

sub vcl_backend_refresh {
set beresp.http.vbref = "true";
return (merge);
}
} -start

client c1 {
txreq
rxresp
expect resp.status == 200
expect resp.http.was-304 == false
expect resp.http.vbref == <undef>
expect resp.http.vbresp == true
expect resp.http.from-bo == true
} -run

delay 0.01

client c2 {
txreq
rxresp
expect resp.status == 200
expect resp.http.was-304 == true
expect resp.http.be304-1 == true
expect resp.http.vbref == true
expect resp.http.vbresp == true
expect resp.http.from-bo == true
} -run

varnish v1 -vcl+backend {

sub vcl_backend_response {
set beresp.http.vbresp = "true";
set beresp.ttl = 0.01s;
set beresp.grace = 0s;
set beresp.keep = 10m;
set beresp.http.was-304 = beresp.was_304;
}

sub vcl_backend_refresh {
set beresp.http.vbref = "true";
return (obj_stale);
}
}

delay 0.01

client c3 {
txreq
rxresp
expect resp.status == 200
expect resp.http.was-304 == true
expect resp.http.be304-1 == true
expect resp.http.be304-2 == <undef>
expect resp.http.vbref == true
expect resp.http.vbresp == true
expect resp.http.from-bo == true
} -run

varnish v1 -vcl+backend {

sub vcl_backend_response {
set beresp.http.vbresp = "true";
set beresp.ttl = 0.01s;
set beresp.grace = 0s;
set beresp.keep = 10m;
set beresp.http.was-304 = beresp.was_304;
}

sub vcl_backend_refresh {
set beresp.http.vbref = "true";
return (beresp);
}
}

delay 0.01

client c4 {
txreq
rxresp
expect resp.status == 304
expect resp.http.was-304 == true
expect resp.http.be304-1 == <undef>
expect resp.http.be304-2 == <undef>
expect resp.http.be304-3 == true
expect resp.http.vbref == true
expect resp.http.vbresp == true
expect resp.http.from-bo == <undef>
} -run

0 comments on commit 319b37d

Please sign in to comment.