Set my own value in C for use in VCL

Philip Prince philip.prince at oxil.co.uk
Thu Jan 20 15:58:25 CET 2011


Dear Kristian,

It is very kind of you to respond so quickly!

I apologise for the C error; my colleague looking over my shoulder and reading this email with me had a laugh about it. However, even when I remove the line above and the two lines below (which could bring sp back into scope) the behaviour is identical. 

Unpacking the C code sounds like great fun but in the meantime, shouldn't the call to VRT_SetHdr with the simple arguments (taken from examples by Poul and others) be benign (I have tried it with and without vrt-magic-string)?

    C{
       VRT_SetHdr(sp, HDR_REQ, "\013X-My-Own-Value:", "1");
    }C

That segfault-ing Varnish may be one of my guilty pleasures notwithstanding, I have now searched for VMODs and have found a reference to them in the 'Documentation for trunk.'  Curiously, they don't appear to be documented in the 'Documentation for the latest stable release' which was the path I was stumbling down. Unfortunately, our preferred development environment (PHP on Macs for delivery on Ubuntu) does not lend itself to rapid prototyping C modules. The inline C seemed to amenable to me faffing about for a bit until I get something working.

I have a client who is very concerned about the very first access to a not-yet-cached bit of information by many, many people all at the same time. Their preference is to receive a retry response rather than queue and wait for the backend to respond (it is an expensive request) if someone else has already accessed the URL but the cache has not yet been populated with its content. Is there a built-in that would be ideal for this scenario?

Many thanks,
Philip

On 20 Jan 2011, at 13:47, Kristian Lyngstol wrote:

> On Thu, Jan 20, 2011 at 01:26:37PM +0000, Philip Prince wrote:
>> I would like to use a C routine to set a value which I could pass on to
>> VCL. I have made this simple start derived from examples I have seen on
>> the web. I direct the request to a PHP file that streams out simulated
>> data. If I comment out the VRT_SetHdr I get the expected data, otherwise
>> I get an empty file.
>> 
>> C{
>>  #include <stdlib.h>
>> }C
> 
> (...)
> 
>> sub vcl_miss {
>> 
>>  C{
>>    void setmyownvalue () {
>>      VRT_SetHdr(sp, HDR_REQ, "\013X-My-Own-Value:", "1", vrt_magic_string_end);
>>    }
>>    setmyownvalue();
>>  }C
> 
> You shouldn't define a function inside vcl_miss. And that function has no
> knowledge of (sp).
> 
> You have to move the function definition out of vcl_miss if you intend to
> use a function, then prototype it properly and pass sp along.
> 
> As for the actual VRT_SetHdr, I didn't really verify it, but the easiest
> test is to try exactly what you want in normal VCL and run "varnishd -C -f
> foo.vcl" which will give the resulting C code.
> 
>> It has to be easy; my apologies for being so thick. I have looked for VRT
>> documentation but I must be looking in all the wrong places...
> 
> There's a reason for that: We don't encourage in-line C unless you:
> 
> A. Know C
> B. Understand Varnish
> C. Are willing to look through the source code.
> 
> This is likely to change somewhat with Varnish 3.0 and vmods.
> 
> As for A, the majority of issues I've seen people have with in-line C is a
> lack of basic C experience. Not prototyping would be one such thing (which
> any compiler will warn you about). Almost every time I get asked to look at
> in-line C, there's also a NULL-check missing which would result in a
> segfault.
> 
> And for B... Well, if you don't properly understand Varnish, it's unfair to
> assume you will be able to write C code for it in a manner that wont
> increase deforestation, segfault varnish or harm little children. And it's
> likely that you can do what you want without in-line C.
> 
> The last one, C, might require some more explanation. In-line C was
> designed as an emergency escape hatch that we got for free. Because VCL is
> translated to C, in-line C isn't so much a "feature" as a short-cut. The
> interfaces of VRT (Varnish Run Time, which VCL and in-line C use) are not
> guaranteed to be stable, even between stable version of Varnish. It's
> simply not a design goal. If we create extensive documentation for it, we
> create an expectation that any in-line C you write will work on the next
> release too. That makes development harder.
> 
> Vmods are designed to solve that problem: We make it easier to write _good_
> in-line C code for Varnish that can be re-used and maintained. It will also
> make it easier to see what interfaces we need to keep and which ones we can
> drop.
> 
> Hope this both helped you with the original problem and explained why
> in-line C documentation isn't available :)
> 
> - Kristian

Philip Prince SB MA MBCS CITP
Network Architect, Director


Oxford Information Labs Limited

The Magdalen Centre
Oxford OX4 4GA

t: 01865 784294
d: 01865 582040
m: 07595 894469

Legally privileged/Confidential Information may be contained in this message. If you are not the addressee(s) legally indicated in this message (or responsible for delivery of the message to such person) you may not copy or deliver this message to anyone. In such case, you should destroy this message, and notify us immediately. If you or your employer does not consent to Internet e-mail messages of this kind, please advise us immediately. Opinions, conclusions and other information expressed in this message are not given or endorsed by my firm or employer unless otherwise indicated by an authorised representative independent of this message. Please note that we do not accept any responsibility for viruses and we advise you to scan attachments.

Registered office: 37 Market Square, Witney, Oxfordshire OX28 6RE. Registered in England and Wales No. 4520925. VAT No. 799526263.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20110120/7678d04e/attachment-0003.html>


More information about the varnish-misc mailing list