This gem was originally developed, incubated and maintained at ABTION. Its main goal is providing the ability to lock a resource so that no other users/lockers can access it till the lock is released or the ttl expires. It uses redis
a shared memory space to share locks across different deployments which enables easy horizontal scalability for your ruby/rails project on multiple servers.
An example usage for this gem is when you need a blog post (resource) to be only edtiable by 1 user concurrently. So the first user to lock the blog post to himself will always have access and be able to edit it. This user will need to renew the lock before the ttl
expires otherwise the post will be unlocked/released and any other users can lock it to themselves.
ActsAsLockableBy
uses redis
as a shared distributed efficient lock manager with its built-in ability to expire locks when ttl
expires.
The lock
, unlock
and renew_lock
methods in this gem are all atomic operations and running as one redis call on the redis server. Even multiple clients calling any of these methods against the same key/resource will never enter into a race condition or thread unsafety scenarios.
Add this line to your application's Gemfile:
gem 'acts_as_lockable_by'
And then execute:
$ bundle
Or install it yourself as:
$ gem install acts_as_lockable_by
You need to configure the gem as follows:
# config/initializers/acts_as_lockable_by.rb
ActsAsLockableBy.configure do |config|
config.redis = Redis.new(url: ENV['REDIS_URL']) # redis client
config.ttl = 30.seconds # global ttl
end
Setup
class User < ActiveRecord::Base
# :id is a unique identifier(attribute/method) for this object
# :ttl default to global configured ttl if not provided
acts_as_lockable_by :id, ttl: 60.seconds
end
# or if not using ActiveRecord
class Post
include ActsAsLockableBy::Lockable
acts_as_lockable_by :post_id # default to global configured ttl
def post_id
"SOME UNIQUE IDENTIFIER"
end
end
post = Post.new
Lock and unlock a post
post.lock('Tarek Elsamni') # true
post.lock('Tarek Elsamni') # false - already locked!
post.unlock('Someone Else') # false - 'Someone Else' did not lock it!
post.unlock('Tarek Elsamni') # true - 'Tarek Elsamni' locked it!
post.unlock('Tarek Elsamni') # false - It is already unlocked!
post.lock!('Tarek Elsamni') # true
post.lock!('Tarek Elsamni') # will raise LockError - already locked!
post.unlock!('Tarek Elsamni') # true - 'Tarek Elsamni' locked it!
post.unlock!('Tarek Elsamni') # will raise UnLockError - not locked!
Check if an object/resource is locked
post.locked? # false
post.lock('Tarek Elsamni') # true
post.locked? # true
Renew a lock before its ttl expires
post.lock('Tarek Elsamni') # true
post.renew_lock('Someone Else') # false - 'Someone Else' did not lock it!
post.renew_lock('Tarek Elsamni') # true - 'Tarek Elsamni' locked it!
Who locked an object?
post.lock('Tarek Elsamni') # true
post.locked? # true
post.locked_by_id # 'Tarek Elsamni'
Is a class lockable?
Post.lockable? # true - an alias to Post.is_lockable?
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then open a PR with your changes against the main gem repo. If your code is passing current tests and highly covered by new tests then one of the maintainers will review it and merge. Auto releasing to a new gem version to rubygems
is automated with travis.
Bug reports and pull requests are welcome on GitHub at https://github.com/shebang-labs/acts_as_lockable_by. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the ActsAsLockable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.