class
RateLimiter extends AnyRef
Instance Constructors
-
new
RateLimiter(maxFreqHz: Int)
Value Members
-
final
def
!=(arg0: AnyRef): Boolean
-
final
def
!=(arg0: Any): Boolean
-
final
def
##(): Int
-
final
def
==(arg0: AnyRef): Boolean
-
final
def
==(arg0: Any): Boolean
-
final
def
asInstanceOf[T0]: T0
-
def
clone(): AnyRef
-
final
def
eq(arg0: AnyRef): Boolean
-
def
equals(arg0: Any): Boolean
-
def
finalize(): Unit
-
final
def
getClass(): Class[_]
-
def
getMaxFreqHz(): Int
-
def
hashCode(): Int
-
final
def
isInstanceOf[T0]: Boolean
-
def
limit[T](workFunction: ⇒ T): T
-
val
maxFreqHz: Int
-
final
def
ne(arg0: AnyRef): Boolean
-
final
def
notify(): Unit
-
final
def
notifyAll(): Unit
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
-
def
toString(): String
-
final
def
wait(): Unit
-
final
def
wait(arg0: Long, arg1: Int): Unit
-
final
def
wait(arg0: Long): Unit
Inherited from AnyRef
Inherited from Any
Used to rate-limit calls to a work function, e.g. that writes to a db.
Note you can disable the rate limiter by setting the frequency to 0.
The math is as follows (mod some conversions between seconds and nano-seconds):
After n samples, we can computer Ri as the "immediate rate" like:
Ri = (n-1) / (timestamp(n) - timestamp(0))
which is essentially the 1/average interval.
Given a target rate, Rt ("maxFreqHz"), we can compute it like:
Rt = n / (S + (timestamp(n) - timestamp(0))
S here is how long to sleep before we invoke the next operation. Solving for S:
S = n / Rt - (timestamp(n) - timestamp(0))