final class AsyncSemaphore extends GenericSemaphore[Cancelable]
The AsyncSemaphore is an asynchronous semaphore implementation that
limits the parallelism on Future execution.
The following example instantiates a semaphore with a maximum parallelism of 10:
val semaphore = AsyncSemaphore(maxParallelism = 10) def makeRequest(r: HttpRequest): Future[HttpResponse] = ??? // For such a task no more than 10 requests // are allowed to be executed in parallel. val future = semaphore.greenLight(() => makeRequest(???))
- Source
- AsyncSemaphore.scala
- Alphabetic
- By Inheritance
- AsyncSemaphore
- GenericSemaphore
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- def acquire(): CancelableFuture[Unit]
Acquires a single permit.
Acquires a single permit. Alias for
acquireN(1).- Annotations
- @UnsafeBecauseImpure()
- See also
withPermit, the preferred way to acquire and release
acquireN for a version that can acquire multiple permits
- def acquireN(n: Long): CancelableFuture[Unit]
Acquires
npermits.Acquires
npermits.The returned effect semantically blocks until all requested permits are available. Note that acquires are satisfied in strict FIFO order, so given an
AsyncSemaphorewith 2 permits available, anacquireN(3)will always be satisfied before a later call toacquireN(1).- n
number of permits to acquire - must be >= 0
- returns
a future that will complete when the acquisition has succeeded or that can be cancelled, removing the listener from the queue (to prevent memory leaks in race conditions)
- Annotations
- @UnsafeBecauseImpure()
- See also
withPermit, the preferred way to acquire and release
acquire for a version acquires a single permit
- def available(): Long
Returns the number of permits currently available.
Returns the number of permits currently available. Always non-negative.
The protocol is unsafe, the semaphore is used in concurrent settings and thus the value returned isn't stable or reliable. Use with care.
- Annotations
- @UnsafeProtocol() @UnsafeBecauseImpure()
- def awaitAvailable(n: Long): CancelableFuture[Unit]
Returns a future that will be complete when the specified number of permits are available.
Returns a future that will be complete when the specified number of permits are available.
The protocol is unsafe because by the time the returned future completes, some other process might have already acquired the available permits and thus usage of
awaitAvailablecan lead to fragile concurrent logic. Use with care.Can be useful for termination logic, for example to execute a piece of logic once all available permits have been released.
- n
is the number of permits waited on
- Annotations
- @UnsafeProtocol() @UnsafeBecauseImpure()
- def count(): Long
Obtains a snapshot of the current count.
Obtains a snapshot of the current count. Can be negative.
Like available when permits are available but returns the number of permits callers are waiting for when there are no permits available.
- Annotations
- @UnsafeProtocol() @UnsafeBecauseImpure()
- def release(): Unit
Releases a permit, returning it to the pool.
Releases a permit, returning it to the pool.
If there are consumers waiting on permits being available, then the first in the queue will be selected and given a permit immediately.
- Annotations
- @UnsafeBecauseImpure()
- See also
withPermit, the preferred way to acquire and release
- def releaseN(n: Long): Unit
Releases
npermits, potentially unblocking up tonoutstanding acquires.Releases
npermits, potentially unblocking up tonoutstanding acquires.- n
number of permits to release - must be >= 0
- Annotations
- @UnsafeBecauseImpure()
- See also
withPermit, the preferred way to acquire and release
- def tryAcquire(): Boolean
Alias for
tryAcquireN(1).Alias for
tryAcquireN(1).The protocol is unsafe, because with the "try*" methods the user needs a firm grasp of what race conditions are and how they manifest and usage of such methods can lead to very fragile logic.
- Annotations
- @UnsafeProtocol() @UnsafeBecauseImpure()
- See also
tryAcquireN for the version that can acquire multiple permits
acquire for the version that can wait for acquisition
withPermit the preferred way to acquire and release
- def tryAcquireN(n: Long): Boolean
Acquires
npermits now and returnstrue, or returnsfalseimmediately.Acquires
npermits now and returnstrue, or returnsfalseimmediately. Error ifn < 0.The protocol is unsafe, because with the "try*" methods the user needs a firm grasp of what race conditions are and how they manifest and usage of such methods can lead to very fragile logic.
- n
number of permits to acquire - must be >= 0
- Annotations
- @UnsafeProtocol() @UnsafeBecauseImpure()
- See also
tryAcquire for the alias that acquires a single permit
acquireN for the version that can wait for acquisition
withPermit, the preferred way to acquire and release
- def withPermit[A](f: () => Future[A]): CancelableFuture[A]
Returns a new future, ensuring that the given source acquires an available permit from the semaphore before it is executed.
Returns a new future, ensuring that the given source acquires an available permit from the semaphore before it is executed.
The returned future also takes care of resource handling, releasing its permit after being complete.
- f
is a function returning the
Futureinstance we want to evaluate after we get the permit from the semaphore
- Annotations
- @UnsafeBecauseImpure()
- def withPermitN[A](n: Long)(f: () => Future[A]): CancelableFuture[A]
Returns a new future, ensuring that the given source acquires
navailable permits from the semaphore before it is executed.Returns a new future, ensuring that the given source acquires
navailable permits from the semaphore before it is executed.The returned future also takes care of resource handling, releasing its permits after being complete.
- n
is the number of permits required for the given function to be executed
- f
is a function returning the
Futureinstance we want to evaluate after we get the permit from the semaphore
- Annotations
- @UnsafeBecauseImpure()

This is the API documentation for the Monix library.
Package Overview
monix.execution exposes lower level primitives for dealing with asynchronous execution:
Atomictypes, as alternative tojava.util.concurrent.atomicmonix.catnap exposes pure abstractions built on top of the Cats-Effect type classes:
monix.eval is for dealing with evaluation of results, thus exposing Task and Coeval.
monix.reactive exposes the
Observablepattern:Observableimplementationsmonix.tail exposes Iterant for purely functional pull based streaming:
BatchandBatchCursor, the alternatives to Scala'sIterableandIteratorrespectively that we are using within Iterant's encodingYou can control evaluation with type you choose - be it Task, Coeval, cats.effect.IO or your own as long as you provide correct cats-effect or cats typeclass instance.