Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throttle per url in node.js restify

The documentation states:

Note that you can always place this on per-URL routes to enable different request rates to different resources (if for example, one route, like /my/slow/database is much easier to overwhlem than /my/fast/memcache).

I am having trouble finding out how to implement this exactly.

Basically, I want to serve static files at a different throttle rate than my API.

like image 736
ilovett Avatar asked Aug 16 '13 22:08

ilovett


1 Answers

Setup throttling (rate limiter) with restify for some endpoints like this.

    var rateLimit = restify.throttle({burst:100,rate:50,ip:true});
    server.get('/my/endpoint',
        rateLimit,
        function(req, res, next) {
            // Do something here
            return next();
        }
    );
    server.post('/another/endpoint',
        rateLimit,
        function(req, res, next) {
            // Do something here
            return next();
        }
    );

Or like this.

    server.post('/my/endpoint',
        restify.throttle({burst:100,rate:50,ip:true}),
        function(req, res, next) {
            // Do something here
            return next();
        }
    );

Even when throttling per endpoint a global throttle may still be desired, so that can be done like this.

    server.use(restify.throttle({burst:100,rate:50,ip:true});

(reference) Throttle is one of restify's plugins.

like image 118
Mark Maruska Avatar answered Nov 03 '22 01:11

Mark Maruska