Project

slock

0.0
No release in over a year
Gem provide Semaphore lock via Redis
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

>= 4.0
 Project Readme

Slock

Code Climate Inline docs Gem Version

Slock implements Semaphore via Redis.

Installation

Add this line to your application's Gemfile:

gem 'slock'

Usage

Singleton Class

class MySemaphore
  include Slock::Semaphore::Singleton

  SIZE     = 2   # max count of simultaneous locks
  LIFETIME = 600 # max time that lock lives after acquring (in seconds)
  TIMEOUT  = 900 # max time that semaphore waits for lock to acquire before raising an error

  def semaphore_opts
    {
      redis:    Redis.new(url: ENV['REDIS_URL']),
      size:     SIZE,
      lifetime: LIFETIME,
      timeout:  TIMEOUT
    }
  end
end

MySemaphore.acquire { do_something }

Simple

sempahore = Slock::Semaphore.new 'uniq_semaphore_key',
  redis: Redis.new(ENV['REDIS_URL']),
  lifetime: 600,
  timeout: 900

semaphore.acquire { do_something }