Skip to content

Instantly share code, notes, and snippets.

View maplebed's full-sized avatar

Ben Hartshorne maplebed

View GitHub Profile
@maplebed
maplebed / zookeeper distributed lock example.rb
Last active December 12, 2015 01:39 — forked from isterin/gist:1328677
This gist (forked from https://gist.github.com/1328677) shows a simple example of using zookeeper to create a distributed lock. Run multiple instances of this script to see them fighting for the lock.
require 'zookeeper'
class Lock
def initialize(host, root="/my-app")
@zk = Zookeeper.new(host)
@root = root
end
def with_lock(app, timeout, timeout_callback, &block)
new_lock_res = @zk.create(:path => "#{@root}/#{app}-", :sequence => true, :ephemeral => true)