class AsyncMeter extends AnyRef
An asynchronous meter.
Processes can create an asynchronously awaiting future, a "waiter" to wait
until the meter allows it to continue, which is when the meter can give it as
many permits as it asked for. Up to burstSize
permits are issued every
burstDuration
. If maxWaiters
waiters are enqueued simultaneously, it
will reject further attempts to wait, until some of the tasks have been
executed.
It may be appropriate to use this to smooth out bursty traffic, or if using a resource that's rate-limited based on time. However, to avoid overwhelming a constrained resource that doesn't exert coordination controls like backpressure, it's safer to limit based on AsyncSemaphore, since it can speed up if that resource speeds up, and slow down if that resource slows down.
// create a meter that allows 1 operation per second, and a max of 1000 waiting val meter = new AsyncMeter(1, 1.second, 1000) def notMoreThanOncePerSecond[A](f: => Future[A]): Future[A] = { meter.await(1).handle { case t: RejectedExecutionException => // do something else when too many waiters }.before { f } }
- Alphabetic
- By Inheritance
- AsyncMeter
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
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
- def await(permits: Int): Future[Unit]
Provides a com.twitter.util.Future that waits to be issued
permits
permits until after the previously scheduled waiters have had their permits issued.Provides a com.twitter.util.Future that waits to be issued
permits
permits until after the previously scheduled waiters have had their permits issued. Permits are spaced out evenly, so that they aren't issued in big batches all at once.If a waiter is scheduled, but the existing queue is empty, it is delayed until sufficient permits have built up. If enough time has passed since the last waiter was permitted so that permits would have built up while it was waiting, it will be permitted immediately.
If the returned com.twitter.util.Future is interrupted, we try to cancel it. If it's successfully cancelled, the com.twitter.util.Future is satisfied with a
java.util.concurrent.CancellationException
, and the permits will not be issued, so a subsequent waiter can take advantage of the permits.If
await
is invoked when there are alreadymaxWaiters
waiters waiting for permits, the com.twitter.util.Future is immediately satisfied with ajava.util.concurrent.RejectedExecutionException
.If more permits are requested than
burstSize
then it returns a failedjava.lang.IllegalArgumentException
com.twitter.util.Future immediately. - def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def numWaiters(): Int
Returns the current number of outstanding waiters in the queue
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- 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()