2015-04-13 19:42:50 -07:00
2015-04-12 13:40:53 -07:00
2015-04-13 19:37:34 -07:00
2015-04-13 19:42:50 -07:00
2015-04-12 13:40:53 -07:00
2015-04-12 13:40:53 -07:00
2015-04-12 15:28:35 -07:00
2015-04-12 23:45:09 -07:00
2015-04-12 13:40:53 -07:00
2015-04-12 15:30:12 -07:00
2015-04-12 15:31:19 -07:00
2015-04-12 22:33:19 -07:00
2015-04-13 10:12:26 -07:00

Suo Build Status Gem Version

🔒 Distributed semaphores using Memcached or Redis in Ruby.

Suo provides a very performant distributed lock solution using Compare-And-Set (CAS) commands in Memcached, and WATCH/MULTI in Redis.

Installation

Add this line to your applications Gemfile:

gem 'suo'

Usage

Basic

# Memcached
suo = Suo::Client::Memcached.new(connection: "127.0.0.1:11211")

# Redis
suo = Suo::Client::Redis.new(connection: {host: "10.0.1.1"})

# Pre-existing client
suo = Suo::Client::Memcached.new(client: some_dalli_client)

suo.lock("some_key") do
  # critical code here
  @puppies.pet!
end

Thread.new { suo.lock("other_key", 2) { puts "One"; sleep 2 } }
Thread.new { suo.lock("other_key", 2) { puts "Two"; sleep 2 } }
Thread.new { suo.lock("other_key", 2) { puts "Three" } }

# will print "One" "Two", but not "Three", as there are only 2 resources

# custom acquisition timeouts (time to acquire)
suo = Suo::Client::Memcached.new(client: some_dalli_client, acquisition_timeout: 1) # in seconds

# manually locking/unlocking
suo.lock("a_key")
foo.baz!
suo.unlock("a_key")

# custom stale lock cleanup (cleaning of dead clients)
suo = Suo::Client::Redis.new(client: some_redis_client, stale_lock_expiration: 60*5)

TODO

  • better stale key handling (refresh blocks)
  • more race condition tests

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

Description
Distributed locks (mutexes & semaphores) using Memcached or Redis
Readme 119 KiB
Languages
Ruby 99.7%
Shell 0.3%