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: 2Usage
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.
Must be paired with release/1. Prefer with_permit/2 for automatic release.
Returns a specification to start this module under a supervisor.
See Supervisor.
Releases a permit previously acquired with acquire/1.
Executes the given function with concurrency limit.
Acquires a permit, executes the function, then releases. Handles exceptions safely.