Accessing original object in varnish background Fetch

Arunabha Saha arunabha.saha at gmail.com
Thu Mar 4 06:08:22 UTC 2021


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 at 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 at 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 at varnish-cache.org
>> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc



-- 
regards,
Arun


More information about the varnish-misc mailing list