GlobalSemaphore (GlobalSemaphore v0.2.0)

Copy Markdown View Source

Cluster-wide semaphore using Erlang's :global module.

Creates a singleton GenServer per name that limits concurrency across all nodes in the cluster. Each semaphore name operates independently, allowing different limits for different resources.

Configuration

config :global_semaphore, :limits,
  pdf_api: 3,
  email_api: 5,
  payment_api: 2

Usage

GlobalSemaphore.with_permit(:pdf_api, fn ->
  # Only 3 concurrent executions allowed cluster-wide
end)

# Priority (lower number = higher priority, default 5)
GlobalSemaphore.with_permit({:pdf_api, 1}, fn -> ... end)   # high
GlobalSemaphore.with_permit({:pdf_api, 5}, fn -> ... end)   # normal (default)
GlobalSemaphore.with_permit({:pdf_api, 10}, fn -> ... end)  # low

Summary

Functions

Acquires a permit, blocking until one is available.

Returns a specification to start this module under a supervisor.

Releases a permit previously acquired with acquire/1.

Executes the given function with concurrency limit.

Functions

acquire(name_or_tuple)

Acquires a permit, blocking until one is available.

Must be paired with release/1. Prefer with_permit/2 for automatic release.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

release(name_or_tuple)

Releases a permit previously acquired with acquire/1.

start_link(name)

with_permit(name_or_tuple, fun)

Executes the given function with concurrency limit.

Acquires a permit, executes the function, then releases. Handles exceptions safely.