Mailing List Archive

Accessing original object in varnish background Fetch
Hello,
Something I am trying to do is update an existing value in a
cached object after every background fetch. I can't seem to figure
out how to access the existing object parameters during the background
fetch.

Example

vcl_backend_response {
if (!bereq.is_bgfetch) {
set beresp.http.myval = beresp.http.val_from_backend;
} else {
#
#Here beresp.http.myval (on RHS of assignment expression).
# should be the original hdr value stored with the object
but i can't seem to access it this way
# or any other way.
#
set beresp.http.myval = beresp.http.myval +
beresp.http.val_from_backend;
}
}

--
regards,
Arun
_______________________________________________
varnish-misc mailing list
varnish-misc@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
Re: Accessing original object in varnish background Fetch [ In reply to ]
Hi,

This expectation is wrong:
# Here beresp.http.myval (on RHS of assignment expression).
# should be the original hdr value stored with the object

beresp is the new backend response, and VCL doesn't make the old one
available.

There are two ways to deal with this.

The first one is vmod_stale (
https://docs.varnish-software.com/varnish-cache-plus/vmods/stale/#get-header)
in Varnish Enterprise that will allow you to get direct access to that
header.

The second one, if you want to stick to open source, uses vcl_hit to stash
the old headers in req so that they are available in bereq. That would look
something like this (untested code, use with caution):

sub vcl_recv {
# avoid injections from the clients
unset req.http.myval;
}

sub vcl_hit {
# store the header
set req.http.myval = obj.http.myval;
}

sub vcl_backend_response {
# bereq is basically a req copy, so myval crossed over

# note that I do that unconditionally here, but you can indeed check
bereq.is_bg_fetch

set beresp.http.myval = bereq.http.myval + " extra string";
}


that should do the trick, however, be careful, the code above will add more
text to the header at every background fetch, so you are more than likely
to hit the header length limit if you are not careful.

Does that make sense?

--
Guillaume Quintard


On Wed, Mar 3, 2021 at 8:23 PM Arunabha Saha <arunabha.saha@gmail.com>
wrote:

> Hello,
> Something I am trying to do is update an existing value in a
> cached object after every background fetch. I can't seem to figure
> out how to access the existing object parameters during the background
> fetch.
>
> Example
>
> vcl_backend_response {
> if (!bereq.is_bgfetch) {
> set beresp.http.myval = beresp.http.val_from_backend;
> } else {
> #
> #Here beresp.http.myval (on RHS of assignment expression).
> # should be the original hdr value stored with the object
> but i can't seem to access it this way
> # or any other way.
> #
> set beresp.http.myval = beresp.http.myval +
> beresp.http.val_from_backend;
> }
> }
>
> --
> regards,
> Arun
> _______________________________________________
> varnish-misc mailing list
> varnish-misc@varnish-cache.org
> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
>
Re: Accessing original object in varnish background Fetch [ In reply to ]
Thank you. This(open source version) worked very well for me. The
part in vcl_hit was what i wasn't aware of.

To clarify the header value returned is a number and the value stored
back into the object is an average of the existing header value and
the number number obtained from upstream so the length of the header
value should not be a real concern.

On Wed, Mar 3, 2021 at 8:42 PM Guillaume Quintard
<guillaume@varnish-software.com> wrote:
>
> Hi,
>
> This expectation is wrong:
> # Here beresp.http.myval (on RHS of assignment expression).
> # should be the original hdr value stored with the object
>
> beresp is the new backend response, and VCL doesn't make the old one available.
>
> There are two ways to deal with this.
>
> The first one is vmod_stale (https://docs.varnish-software.com/varnish-cache-plus/vmods/stale/#get-header) in Varnish Enterprise that will allow you to get direct access to that header.
>
> The second one, if you want to stick to open source, uses vcl_hit to stash the old headers in req so that they are available in bereq. That would look something like this (untested code, use with caution):
>
> sub vcl_recv {
> # avoid injections from the clients
> unset req.http.myval;
> }
>
> sub vcl_hit {
> # store the header
> set req.http.myval = obj.http.myval;
> }
>
> sub vcl_backend_response {
> # bereq is basically a req copy, so myval crossed over
>
> # note that I do that unconditionally here, but you can indeed check bereq.is_bg_fetch
>
> set beresp.http.myval = bereq.http.myval + " extra string";
> }
>
>
> that should do the trick, however, be careful, the code above will add more text to the header at every background fetch, so you are more than likely to hit the header length limit if you are not careful.
>
> Does that make sense?
>
> --
> Guillaume Quintard
>
>
> On Wed, Mar 3, 2021 at 8:23 PM Arunabha Saha <arunabha.saha@gmail.com> wrote:
>>
>> Hello,
>> Something I am trying to do is update an existing value in a
>> cached object after every background fetch. I can't seem to figure
>> out how to access the existing object parameters during the background
>> fetch.
>>
>> Example
>>
>> vcl_backend_response {
>> if (!bereq.is_bgfetch) {
>> set beresp.http.myval = beresp.http.val_from_backend;
>> } else {
>> #
>> #Here beresp.http.myval (on RHS of assignment expression).
>> # should be the original hdr value stored with the object
>> but i can't seem to access it this way
>> # or any other way.
>> #
>> set beresp.http.myval = beresp.http.myval +
>> beresp.http.val_from_backend;
>> }
>> }
>>
>> --
>> regards,
>> Arun
>> _______________________________________________
>> varnish-misc mailing list
>> varnish-misc@varnish-cache.org
>> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc



--
regards,
Arun
_______________________________________________
varnish-misc mailing list
varnish-misc@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc