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
  }
}
Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. AsyncMeter
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. 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 already maxWaiters waiters waiting for permits, the com.twitter.util.Future is immediately satisfied with a java.util.concurrent.RejectedExecutionException.

    If more permits are requested than burstSize then it returns a failed java.lang.IllegalArgumentException com.twitter.util.Future immediately.

  6. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  7. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  8. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  9. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  10. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  11. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  12. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  13. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  14. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  15. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  16. def numWaiters(): Int

    Returns the current number of outstanding waiters in the queue

  17. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  18. def toString(): String
    Definition Classes
    AnyRef → Any
  19. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped