Switch/Case statement?

Mark Moseley moseleymark at gmail.com
Thu Dec 13 19:59:51 CET 2012

Just want to make sure I'm not missing something obvious...

Is there an equivalent of a switch/case statement for VCL? I've not
been able to find one, nor a VMOD that implements one. I've got a
backend selection chunk of VCL code that is turning into a very long
if/elsif/elsif/elsif/..../else. Before I try to optimizing the
if/elsif's a bit, I went in search of a VCL equiv for 'case' but came
up empty-handed.

As a bit of background, it's doing the backend selection based on the
incoming IP address on a dummy interface, so it looks like:

        if ( server.ip == "" ) {
                set req.backend = backend1;

        } elsif ( server.ip == "" ) {
                set req.backend = backend2;

and so on all the way up to but growing by a few every
year. I figure I can restructure it into something like (in

if ( server.ip is in ) {

    if ( server.ip is in ) {
    } elsif ( server.ip is in ) {

and so on, though I'll re-order things a bit to get the most-used
first. But obviously a 'case'-equiv would be much more efficient (I'm
guessing, unless it internally compiled to if/elsif/elsif/.../else).

Failing a 'case'/'switch' statement, being able to map the incoming
IP->backend via an associative array would be cool too, but I didn't
see anything remotely like that.

More information about the varnish-misc mailing list