8

该文档指出:

请注意,您始终可以将其放置在每个 URL 路由上,以启用对不同资源的不同请求率(例如,如果 /my/slow/database 之类的一条路由比 /my/fast/memcache 更容易被淹没)。

我很难找出如何准确地实现这一点。

基本上,我想以与我的 API 不同的节流率来提供静态文件。

4

1 回答 1

11

使用 restify 为某些端点设置节流(速率限制器)。

    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();
        }
    );

或者像这样。

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

即使在对每个端点进行限制时,仍可能需要全局限制,因此可以这样完成。

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

(参考)Throttle 是 restify 的插件之一。

于 2014-06-21T12:09:45.043 回答