Skip to content

Instantly share code, notes, and snippets.

@gorsuch
Last active December 26, 2015 10:29
Show Gist options
  • Save gorsuch/7137154 to your computer and use it in GitHub Desktop.
Save gorsuch/7137154 to your computer and use it in GitHub Desktop.
module SelfRestraint
class UnableToLock < StandardError; end
def lock(redis, base_key, n)
if key = lock_obtain
begin
yield
ensure
redis.del key
end
else
raise UnableToLock, "Unable to obtain lock"
end
end
# generate a set of potential lock keys
def lock_keys(base_key, n)
n.map { |n| "#{base_key}:#{n}" }
end
# attempt to obtain a lock at the specific key
def lock_obtain(redis, key)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment