番石榴的RateLimiter每分钟而不是秒? [英] Guava’s RateLimiter per minutes instead of seconds?

查看:1840
本文介绍了番石榴的RateLimiter每分钟而不是秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试限制用户可以使用我的REST API创建的帐户数量。

I'm trying to rate-limit the the number of accounts a user can create with my REST API.

我本来希望使用Guava的 RateLimiter 只允许IP在10分钟内创建5个帐户,但 RateLimiter.create 方法只需要 double 指定许可数每秒。

I would have liked to use Guava's RateLimiter to only allow an IP to create, let's say, 5 accounts within 10 minutes, but the RateLimiter.create method only takes a double specifying the number of permits "per second".

有没有办法配置RateLimiter以释放许可证粒度大于一秒?

Is there a way to configure RateLimiter to release permits at a granularity greater than one second?

推荐答案

RateLimiter.create javadoc:


当传入请求率超过permitPerSecond时,速率限制器将每释放一个许可证(1.0 / permitsPerSecond) 秒。

所以你可以设置 permitsPerSecond 小于 1.0 每秒发布许可证的次数少于每秒一次。

So you can set permitsPerSecond to less than 1.0 to release a permit less often than once per second.

在您的具体情况下,五个帐户十分钟内的s简化为每两分钟一个帐户,即每120秒一个帐户。您将 1.0 / 120 传递给 permitsPerSecond

In your specific case, five accounts in ten minutes simplifies to one account per two minutes, which is one account per 120 seconds. You'd pass 1.0/120 for permitsPerSecond.

在您的用例中,您可能希望适应帐户创建的突发请求。 RateLimiter 规范似乎没有定义未使用的许可证会发生什么,但默认实现 SmoothRateLimiter 似乎允许许可累积到最大值以满足爆发。这个类不公开,所以没有javadoc文档,但 SmoothRateLimiter 来源有一个冗长的评论,详细讨论了当前的行为。

In your use case you probably want to accommodate bursty requests for account creations. The RateLimiter specification doesn't seem to define what happens to unused permits, but the default implementation, SmoothRateLimiter, seems to let permits accrue up to some maximum to satisfy bursts. This class is not public, so there's no javadoc documentation, but the SmoothRateLimiter source has a lengthy comment with a detailed discussion of the current behavior.

这篇关于番石榴的RateLimiter每分钟而不是秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆