Hyperlinkv0.8.0-beta.28

PartitionedSemaphore

PartitionedSemaphore.withPermitsconsteffect/PartitionedSemaphore.ts:462
<K>(self: PartitionedSemaphore<K>, key: K, permits: number): <A, E, R>(
  effect: Effect.Effect<A, E, R>
) => Effect.Effect<A, E, R>
<K, A, E, R>(
  self: PartitionedSemaphore<K>,
  key: K,
  permits: number,
  effect: Effect.Effect<A, E, R>
): Effect.Effect<A, E, R>

Runs an effect after acquiring permits for a partition, then releases those permits when the effect exits.

When to use

Use to guard weighted partitioned work with automatic permit acquisition and release around an effect.

Details

Permit acquisition may wait according to take semantics. Once acquired, the permits are released even if the wrapped effect fails or is interrupted.

Gotchas

Requests for more permits than the semaphore capacity never complete. Requests for zero or a negative number of permits run the effect without acquiring anything.

export const withPermits: {
  <K>(
    self: PartitionedSemaphore<K>,
    key: K,
    permits: number
  ): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
  <K, A, E, R>(
    self: PartitionedSemaphore<K>,
    key: K,
    permits: number,
    effect: Effect.Effect<A, E, R>
  ): Effect.Effect<A, E, R>
} = ((...args: Array<any>) => {
  if (args.length === 3) {
    const [self, key, permits] = args
    return (effect: Effect.Effect<any, any, any>) => self.withPermits(key, permits)(effect)
  }
  const [self, key, permits, effect] = args
  return self.withPermits(key, permits)(effect)
}) as any