Skip to content

devall-org/global_semaphore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GlobalSemaphore

CI

Cluster-wide semaphore with a dead-simple API.

# config :global_semaphore, :limits, pdf_api: 3

GlobalSemaphore.with_permit(:pdf_api, fn ->
  generate_pdf()  # max 3 concurrent calls across the entire cluster
end)

Why?

  • Cluster-wide: Works across all nodes, not just one
  • Simple: One config line, one function call. No complex concepts.
  • No dependencies: Uses Erlang's built-in :global module

Installation

def deps do
  [{:global_semaphore, "~> 0.1.0"}]
end

Usage

# config/runtime.exs
config :global_semaphore, :limits,
  pdf_api: 3,
  email_api: 5
# Recommended: automatic acquire/release
GlobalSemaphore.with_permit(:pdf_api, fn ->
  call_pdf_api()
end)

# Manual control when needed
GlobalSemaphore.acquire(:pdf_api)
try do
  call_pdf_api()
after
  GlobalSemaphore.release(:pdf_api)
end

Limitations

  • Uses :global which has known issues during network partitions (rare in practice)
  • If the node hosting the semaphore dies, queued requests fail (next request auto-recovers)

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages