Package org.javacord.api.util.ratelimit
Class LocalRatelimiter
- java.lang.Object
-
- org.javacord.api.util.ratelimit.LocalRatelimiter
-
- All Implemented Interfaces:
Ratelimiter
public class LocalRatelimiter extends Object implements Ratelimiter
An implementation ofRatelimiterthat allows for simple local ratelimits.To compensate for misalignment with the real global ratelimit bucket, it is recommended to not set the value to the exact global ratelimit but a lower value. If the global ratelimit for your bot is 50 requests / 1 second (the default), you should choose a value that prevents overlapping with the real bucket. This can be achieved with the following rules:
- Choose an
amountthat is between1and half of the real amount (in this example1-25). - Calculate the
bucketDurationusing(amount * realDuration) / (realAmount - amount).
- About
1request per20.5 ms(amount = 1andbucketDuration = Duration.ofMillis((long) Math.ceil(1D / 49D))) - About
5request per111.1 ms(amount = 5andbucketDuration = Duration.ofMillis((long) Math.ceil(5D / 45D))). 10request per250 ms(amount = 10andbucketDuration = Duration.ofMillis((long) Math.ceil(10D / 40D))).25request per1 sec(amount = 25andbucketDuration = Duration.ofMillis((long) Math.ceil(10D / 40D))).
amountincreases the maximum throughput but can limits your ability to perform actions in bulk.
-
-
Constructor Summary
Constructors Constructor Description LocalRatelimiter(int amount, int seconds)Deprecated.UseLocalRatelimiter(int, Duration)instead.LocalRatelimiter(int amount, Duration bucketDuration)Creates a new local ratelimiter.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description intgetAmount()Gets the amount available per reset interval.DurationgetBucketDuration()Gets the time to wait until the available quota resets.longgetNextResetNanos()Gets the next time the quota resets.intgetRemainingQuota()Gets the remaining quota in the current reset interval.intgetSeconds()Deprecated.UsegetBucketDuration()instead.voidrequestQuota()Blocks the requesting thread until a quota becomes available.
-
-
-
Constructor Detail
-
LocalRatelimiter
@Deprecated public LocalRatelimiter(int amount, int seconds)
Deprecated. UseLocalRatelimiter(int, Duration)instead.Creates a new local ratelimiter.- Parameters:
amount- The amount available per reset interval.seconds- The time to wait until the available quota resets.
-
LocalRatelimiter
public LocalRatelimiter(int amount, Duration bucketDuration)Creates a new local ratelimiter.- Parameters:
amount- The amount available per reset interval.bucketDuration- The time to wait until the available quota resets.
-
-
Method Detail
-
getAmount
public int getAmount()
Gets the amount available per reset interval.- Returns:
- The amount.
-
getBucketDuration
public Duration getBucketDuration()
Gets the time to wait until the available quota resets.- Returns:
- The time to wait until the available quota resets.
-
getSeconds
@Deprecated public int getSeconds()
Deprecated. UsegetBucketDuration()instead.Gets the time to wait until the available quota resets in seconds.- Returns:
- The time to wait until the available quota resets.
-
getNextResetNanos
public long getNextResetNanos()
Gets the next time the quota resets.Use
System.nanoTime()to calculate the absolute difference.- Returns:
- The next time the quota resets. Can be in the past.
-
getRemainingQuota
public int getRemainingQuota()
Gets the remaining quota in the current reset interval.- Returns:
- The remaining quota.
-
requestQuota
public void requestQuota() throws InterruptedExceptionDescription copied from interface:RatelimiterBlocks the requesting thread until a quota becomes available.- Specified by:
requestQuotain interfaceRatelimiter- Throws:
InterruptedException- if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
-
-