Git Product home page Git Product logo

relocker's Introduction

Relocker

A library for holding a lock in redis. The locking algorithm is the one described on http://redis.io/topics/distlock as "Correct implementation with a single instance".

Usage

Easiest way to keep a lock is to start either a Relocker.Server or Relocker.Fsm process with a registered name. When the process exits the lock will be freed.

For example:

defmodule Game do
  use Relocker.Server

  def init(opts) do
    super(opts)
    {:ok, %{}}
  end

  # some meaningful code here

  def handle_cast(:stop, state) do
    {:stop, :normal, state}
  end

  def terminate(reason, state) do
    super(reason, state)
  end
end

This will create a lock with name "player_1"

iex> {:ok, pid} = Game.start_link([], name: "player_1")

If you try to start a new one while the old one is running you get:

iex> {:error, {:already_started, ...}} = Game.start_link [], name: "player_1"

When a process has been registered, you can send messages via registry just by using the lock name

iex> GenServer.cast({:via, Relocker.Registry, "player_1"}, :stop)

Configure Redis

You need to give the library a connection string in the application config:

# config.exs

config :relocker,
  locker: Relocker.Locker.Redis,
  redis: "redis://192.168.33.11:6379"

Or if you want to use a connection pool with Redis:

# config.exs

config :relocker,
  locker: Relocker.Locker.Pool,
  redis: "redis://192.168.33.11:6379",
  pool: [
    size: 5
  ]

The pool options are directly fed to poolboy. If you omit pool from config the library will use this default config:

[
  name: { :local, Relocker.Locker.Pool },
  worker_module: Relocker.Locker.Pool,
  size: 5,
  max_overflow: 10
]

License notice

Parts of the codebase like lib/server.ex and lib/fsm.ex heavily borrow from this project: https://github.com/tsharju/elixir_locker.

All other code (c) Grand Cru. See LICENSE for details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.