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 java.lang.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 per21 ms(amount = 1andbucketDuration = Duration.ofMillis((long) Math.ceil(1000D / 49D))) - About
5request per112 ms(amount = 5andbucketDuration = Duration.ofMillis((long) Math.ceil(5000D / 45D))). 10request per250 ms(amount = 10andbucketDuration = Duration.ofMillis((long) Math.ceil(10000D / 40D))).25request per1 sec(amount = 25andbucketDuration = Duration.ofMillis((long) Math.ceil(25000D / 25D))).
amountincreases the maximum throughput but can limit your ability to perform actions in bulk.
-
-
Constructor Summary
Constructors Constructor Description LocalRatelimiter(int amount, java.time.Duration bucketDuration)Creates a new local ratelimiter.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intgetAmount()Gets the amount available per reset interval.java.time.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.voidrequestQuota()Blocks the requesting thread until a quota becomes available.
-
-
-
Method Detail
-
getAmount
public int getAmount()
Gets the amount available per reset interval.- Returns:
- The amount.
-
getBucketDuration
public java.time.Duration getBucketDuration()
Gets the time to wait until the available quota resets.- 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 java.lang.InterruptedExceptionDescription copied from interface:RatelimiterBlocks the requesting thread until a quota becomes available.- Specified by:
requestQuotain interfaceRatelimiter- Throws:
java.lang.InterruptedException- if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
-
-