{"id":13507523,"url":"https://github.com/tsharju/elixir_locker","last_synced_at":"2026-02-22T10:34:24.095Z","repository":{"id":31977926,"uuid":"35548106","full_name":"tsharju/elixir_locker","owner":"tsharju","description":null,"archived":false,"fork":false,"pushed_at":"2015-11-26T14:31:25.000Z","size":163,"stargazers_count":16,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-10T21:24:36.312Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tsharju.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-13T12:36:41.000Z","updated_at":"2023-11-06T11:00:17.000Z","dependencies_parsed_at":"2022-08-29T07:42:17.810Z","dependency_job_id":null,"html_url":"https://github.com/tsharju/elixir_locker","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsharju%2Felixir_locker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsharju%2Felixir_locker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsharju%2Felixir_locker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsharju%2Felixir_locker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tsharju","download_url":"https://codeload.github.com/tsharju/elixir_locker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246301963,"owners_count":20755512,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T02:00:35.656Z","updated_at":"2025-10-21T17:49:02.823Z","avatar_url":"https://github.com/tsharju.png","language":"Elixir","funding_links":[],"categories":["Caching"],"sub_categories":[],"readme":"Locker\n======\n\n[![Build Status](https://travis-ci.org/tsharju/elixir_locker.svg?branch=master)](https://travis-ci.org/tsharju/elixir_locker)\n\nA quote from https://github.com/wooga/locker:\n\n\u003e `locker` is a distributed de-centralized consistent in-memory key-value store written in Erlang. An entry expires\n\u003e after a certain amount of time, unless the lease is extended. This makes it a good practical option for locks,\n\u003e mutexes and leader election in a distributed system.\n\n`Locker`is a Elixir wrapper for the `locker` Erlang library that provides some useful libraries that should make using `locker` a bit easier.\n\nThe `Locker` module implements an OTP application that runs `locker`. In addition to that is also provides a wrapper for the `locker` API, just for convenience.\n\nIdea of `Locker` is to provide an abstraction for globally registering `GenServer` or `:gen_fsm` processes to `:locker` key-value store in a cluster of Erlang servers. These are implemented in `Locker.Server` and `Locker.Fsm` modules. In the simplest case, all you have to do is replace `use GenServer` with `use Locker.Server` and your process is automatically registered to global `:locker` cluster.\n\nInstalling\n----------\n\nYou can install `Locker` by adding it as a dependency to your project's `mix.exs` file:\n\n```elixir\ndefp deps do\n  [\n    {:elixir_locker, \"~\u003e 0.1.4\"}\n  ]\nend\n```\n\nAlso, remember to add `:elixir_locker` to your `:applications` list if you wish that the `Locker` application is started automatically.\n\nExamples\n--------\n\n### Globally registered processes\n\nYou can use `Locker` as a global process registry. The `Locker.Registry` module handles the API for registering processes in a way that `GenServer` or `:gen_fsm` can use it directly. Here is an example of an `GenServer` process that will be registered to `Locker` on startup:\n\n```elixir\ndefmodule MyServer do\n  use Locker.Server, lease_length: 60000\n  \n  def init(_) do\n    {:ok, %{}}\n  end\n  \n  def handle_info(_info, state) do\n    {:noreply, state}\n  end\n  \nend\n```\n\nYou can start your process with name `\"my_server_process\"` the way you would start any `GenServer` process. For example, without supervision:\n\n```elixir\niex\u003e MyServer.start([], name: \"my_server_process\")\n{:ok, #PID\u003c0.166.0\u003e}\n```\n\nOnce the process has been started, you can query the process id using `Locker.Registry` like this:\n\n```elixir\niex\u003e Locker.Registry.whereis_name(\"my_server_process\")\n#PID\u003c0.166.0\u003e\n```\n\nIf `Locker` cluster has been properly configured, you can query the process name from any node on your cluster. What `Locker.Server` does is that it updates the lease on the registered process name within the given `:lease_length`. If the lease expires, the process cannot be found from the registry anymore. Also, `Locker.Server` releases the registered name on `terminate/2`.\n\n### Locking resources\n\nIt should not come as a surprise that you can use `Locker` to lock resources globally. For this, you can use `Locker` module that provides a direct mapping to the `:locker` Erlang module. Naturally, you can also use `:locker` directly. Here is how you can create a lock that expires in one minute and wait that the lock gets released.\n\n```elixir\niex(1)\u003e Locker.lock(\"my_lock\", self, 60000)\n{:ok, 1, 1, 1}\niex(2)\u003e Locker.wait_for_release(\"my_lock\", 2 * 60000)\n{:ok, :released}\n```\n\nSo what we did here was that we registered a lock called `\"my_lock\"` and set the value to our own process id and timeout to 60000 milliseconds, i.e. one minute. Then we called `wait_for_release` that will block until the lock is released.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsharju%2Felixir_locker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftsharju%2Felixir_locker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsharju%2Felixir_locker/lists"}