Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/discord/semaphore
Fast semaphore using ETS.
https://github.com/discord/semaphore
elixir erlang locks semaphore
Last synced: about 2 hours ago
JSON representation
Fast semaphore using ETS.
- Host: GitHub
- URL: https://github.com/discord/semaphore
- Owner: discord
- License: mit
- Created: 2017-02-15T20:59:33.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-07T09:10:33.000Z (8 months ago)
- Last Synced: 2025-01-15T14:21:27.016Z (7 days ago)
- Topics: elixir, erlang, locks, semaphore
- Language: Elixir
- Homepage:
- Size: 12.7 KB
- Stars: 745
- Watchers: 191
- Forks: 49
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Semaphore
[![Master](https://travis-ci.org/discordapp/semaphore.svg?branch=master)](https://travis-ci.org/discordapp/semaphore)
[![Hex.pm Version](http://img.shields.io/hexpm/v/semaphore.svg?style=flat)](https://hex.pm/packages/semaphore)Programming in Erlang and Elixir usually allows for no locking since the VM essentially handles it for you when
communicating between processes. However, what about the situation when you have thousands of processes attempting
to interact with a single resource such as a process? Usually they will overload the process and explode the
message queue. ETS is the Swiss Army knife of the Erlang VM and can be applied to this problem. By using `:ets.update_counter`
and `:write_concurrency` we can achieve a **fast** low contention semaphore on ETS.## Usage
Add it to `mix.exs`
```elixir
defp deps do
[{:semaphore, "~> 1.3"}]
end
```Then just use it like a semaphore in any other language.
```elixir
if Semaphore.acquire(:test, 1) do
IO.puts "acquired"
Semaphore.release(:test)
endcase Semaphore.call(:test, 1, fn -> :ok end) do
:ok ->
IO.puts "success"
{:error, :max} ->
IO.puts "too many callers"
end
```## License
Semaphore is released under [the MIT License](LICENSE).
Check [LICENSE](LICENSE) file for more information.