I have been working on an opensource solution for these kind of problems. Limitd is a "server" for limits. The limits are implemented using the Token Bucket Algorithm . Basically you define limits in the service configuration like this:
buckets:
"request to service a":
per_minute: 10
"request to service b":
per_minute: 5
The service is run as a daemon listening on a TCP/IP port. Then your application does something along these lines:
var limitd = new Limitd('limitd://my-limitd-address');
limitd.take('request to service a', 'app1' 1, function (err, result) {
if (result.conformant) {
console.log('everything is okay - this should be allowed');
} else {
console.error('too many calls to this thing');
}
});
We are currently using this for rate-limiting and debouncing some application events. The server is on: https://github.com/auth0/limitd We are planning to work on several SDKs but for now we only have node.js and partially implemented go: https://github.com/limitd
5条答案
按热度按时间ddhy6vgd1#
Bucket4j是“token-bucket”速率限制算法的java实现。它在本地和分布式(在JCache之上)都能工作。对于分布式用例,您可以自由选择任何JCache实现,如Hazelcast或Apache Ignite。请参见在集群中使用Bucket 4j的示例。
odopli942#
I have been working on an opensource solution for these kind of problems.
Limitd is a "server" for limits. The limits are implemented using the Token Bucket Algorithm .
Basically you define limits in the service configuration like this:
The service is run as a daemon listening on a TCP/IP port.
Then your application does something along these lines:
We are currently using this for rate-limiting and debouncing some application events.
The server is on:
https://github.com/auth0/limitd
We are planning to work on several SDKs but for now we only have node.js and partially implemented go:
https://github.com/limitd
lhcgjxsq3#
https://github.com/jdwyah/ratelimit-java提供了分布式速率限制,应该做到这一点。您可以将您的限制配置为S每秒/分钟等,并选择突发大小/重新填充速率的漏桶是在封面。
g52tjvyc4#
Java中的简单速率限制,您希望每3秒实现3个事务的并发。如果您希望将此集中化,则将令牌数组存储在elasticache或任何数据库中。并且,您还必须实现一个锁定标志来代替同步块。
yhived7q5#
因此,对于所有分布式速率限制架构,您需要一个后端存储作为true的单一来源来跟踪请求的数量。出于方便,您可以使用zookeeper作为内存中的数据存储,尽管也有更好的选择,如Redis。