Class LocalRatelimiter

  • All Implemented Interfaces:
    Ratelimiter


    public class LocalRatelimiter
    extends Object
    implements Ratelimiter
    An implementation of Ratelimiter that allows simple local ratelimits.
    • Constructor Detail

      • LocalRatelimiter

        public LocalRatelimiter​(int amount,
                                int seconds)
        Creates a new local ratelimiter.
        Parameters:
        amount - The amount available per reset interval.
        seconds - 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.
      • getSeconds

        public int getSeconds​()
        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 InterruptedException
        Description copied from interface: Ratelimiter
        Blocks the requesting thread until a quota becomes available.
        Specified by:
        requestQuota in interface Ratelimiter
        Throws:
        InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.