class RateLimiter extends AnyRef
Utility class that allows clients to keep track of a rate limit. Clients need to tell an instance whenever they intend to start a new task. The instance will inform the client whether the task can be executed while still meeting the rate limit.
Guarantees:
- Maximum burst size: if
checkAndUpdateRate
is calledn
times in parallel, at mostmax(2, maxTasksPerSecond / 5)
calls may returntrue
. The upper bound mainly comes from the assumption thatSystem.nanoTime
has a resolution of100.millis
. - Average rate: if
checkAndUpdateRate
is called at a rate of at leastmaxTasksPerSecond
duringn
seconds, then the number of calls that returntrue
divided byn
is roughlymaxTasksPerSecond
whenn
goes towards infinity. See the unit tests to get a better understanding of the term "roughly".
Linear Supertypes
Ordering
- Alphabetic
- By Inheritance
Inherited
- RateLimiter
- AnyRef
- Any
- Hide All
- Show All
Visibility
- Public
- Protected
Instance Constructors
- new RateLimiter(maxTasksPerSecond: NonNegativeInt)
- maxTasksPerSecond
the maximum number of tasks per second
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- final def checkAndUpdateRate(): Boolean
Call this before starting a new task.
Call this before starting a new task.
- returns
whether the tasks can be executed while still meeting the rate limit
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- val maxTasksPerSecond: NonNegativeInt
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])