VCL: returning early from a custom function

Cosimo Streppone cosimo at streppone.it
Wed Apr 11 13:59:22 UTC 2018


Hi again,

two messages in a day after a few years :-)

I have code similar to the following (also here[1]):

    sub vcl_recv {
        ...
        call rate_limit;
        ...
    }

    # Throttling based on request inspection
    sub rate_limit {

        if (req.url ~ "pattern1") {
           std.log("pattern1 requests must never be throttled");
           return; # <---- Need to return early here, but can't do it
        }

        if (req.url ~ "pattern2") {
           if (vsthrottle.is_denied("pattern2" + client.identity, 100, 10s)) {
               std.log("pattern2 throttling for ip " + client.identity);
               return(synth(429, "ETOOMANYREQUESTS"));
           }
        }

        if (vsthrottle.is_denied("ip:" + client.identity, 500, 10s)) {
            std.log("global throttling for ip " + client.identity);
            return(synth(429, "ETOOMANYREQUESTS"));
        }

    }

Ideally, I'd like to return early from rate_limit() when I know that I don't need
to enforce any rate limiting for some types of requests, but I understand
that's not implemented.

I found some alternatives, though generally they feel uglier.
Any ideas?

-- 
Cosimo

[1] https://gist.github.com/cosimo/6d3318bf173357dfb2652b7b2e81e1e0#file-gistfile1-txt-L18


More information about the varnish-misc mailing list