Last active
December 26, 2015 10:29
-
-
Save gorsuch/7137154 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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