VCL: returning early from a custom function

Anheyer, Tom Tom.Anheyer at berlinonline.de
Wed Apr 11 14:43:11 UTC 2018


What about:

sub rate_limit {

        if (req.url ~ "pattern1") {
           std.log("pattern1 requests must never be throttled");
        }
        else {
            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"));
            }
        }

    }

tom


Am 11.04.2018 um 15:59 schrieb Cosimo Streppone:
> 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?
>



More information about the varnish-misc mailing list