Foundation.Semaphore.Limiter (foundation v0.2.1)

Copy Markdown View Source

Blocking counting semaphore with exponential backoff.

Summary

Functions

Return the semaphore name for the default key and max connections.

Return the semaphore name for a key and max connections.

Execute fun while holding a semaphore for max_connections.

Execute fun while holding a keyed semaphore with options.

Types

semaphore_name()

@type semaphore_name() :: {Foundation.Semaphore.Limiter, term(), pos_integer()}

Functions

get_semaphore(max_connections)

@spec get_semaphore(pos_integer()) :: semaphore_name()

Return the semaphore name for the default key and max connections.

get_semaphore(key, max_connections)

@spec get_semaphore(term(), pos_integer()) :: semaphore_name()

Return the semaphore name for a key and max connections.

with_semaphore(max_connections, fun)

@spec with_semaphore(pos_integer(), (-> result)) :: result when result: any()

Execute fun while holding a semaphore for max_connections.

with_semaphore(key, max_connections, opts, fun)

@spec with_semaphore(term(), pos_integer(), keyword(), (-> result)) :: result
when result: any()

Execute fun while holding a keyed semaphore with options.

Options:

  • :registry - counting semaphore registry (default: Counting.default_registry/0)
  • :backoff - backoff options or Backoff.Policy (default: exponential 2ms-50ms, 25% jitter)
  • :sleep_fun - sleep function (default: &Process.sleep/1)
  • :max_backoff_exponent - cap for exponential backoff attempts (default: 20)