Hyperlinkv0.8.0-beta.28

Semaphore

Semaphore.makeconsteffect/Semaphore.ts:329
(permits: number): Effect.Effect<Semaphore>

Creates a Semaphore initialized with the specified total number of permits.

When to use

Use to create a semaphore inside Effect code for bounding concurrency with automatic or manual permit management.

Example (Creating a semaphore)

import { Effect, Semaphore } from "effect"

const program = Effect.gen(function*() {
  const semaphore = yield* Semaphore.make(2)

  const task = (id: number) =>
    semaphore.withPermits(1)(
      Effect.gen(function*() {
        yield* Effect.log(`Task ${id} acquired permit`)
        yield* Effect.sleep("1 second")
        yield* Effect.log(`Task ${id} releasing permit`)
      })
    )

  // Run 4 tasks, but only 2 can run concurrently
  yield* Effect.all([task(1), task(2), task(3), task(4)])
})
constructors
export const make = (permits: number): Effect.Effect<Semaphore> => internal.sync(() => new SemaphoreImpl(permits))
Referenced by 1 symbols