SimpleLock
SimpleLock is a blazing fast and lightweight implementation of distributed locking using Ruby and Redis. With zero external dependencies like activesupport
, SimpleLock relies solely on the redis gem, making it extremely lean and compatible with any Ruby environment you’re working in.
Installation
Add the gem to your Gemfile
:
gem "simple_lock"
Then run the following command:
bundle install
Or, if you prefer installing it directly via terminal:
gem install simple_lock
Usage
Client Configuration
To use SimpleLock, you need to configure the Redis client. This can be done by setting the client directly:
SimpleLock.client = SimpleLock::Redis.new(url: "redis://localhost:6379/1")
Alternatively, pass a URL string directly:
SimpleLock.client = "redis://localhost:6379/1"
This step is essential to ensure the lock can communicate with the Redis server.
Simple Locking
The main goal of SimpleLock is to provide a distributed locking mechanism. You can use the gem to ensure that only one process or thread can access a critical resource or operation at a time.
Basic Example
# Using the simple lock with a key and expiration time (TTL)
locked = SimpleLock.lock("my_unique_lock_key", 3000)
if locked
# Execute the critical operation
puts "Lock acquired! Executing critical operation."
else
# Lock couldn't be acquired, another process already has it
puts "Failed to acquire lock, try again later."
end
Unlocking
You can manually release the lock using the unlock
method:
SimpleLock.unlock("my_unique_lock_key")
Using with Block
You can also use SimpleLock with a block, ensuring the lock will be automatically released when the block finishes, even if an exception occurs:
SimpleLock.lock("my_unique_lock_key", 3000) do |locked|
if locked
# Critical operation
puts "Safe operation is running."
else
# Couldn't acquire the lock
puts "Failed to acquire lock."
end
end
Configuration
You can configure various behaviors of the gem, such as the key prefix, retry count, retry delay, and more.
Example Configuration:
SimpleLock.config.key_prefix = "simple_lock:"
SimpleLock.config.retry_count = 3
SimpleLock.config.retry_delay = 200 # in milliseconds
SimpleLock.config.retry_jitter = 50 # in milliseconds
SimpleLock.config.retry_proc = Proc.new { |attempt| attempt * 100 } # Optional
Features
- Distributed locking and unlocking with Redis.
- Automatic retry with increasing delay and jitter, useful for high-concurrency systems.
- Safe unlocking even in case of failure.
- Low latency and easy integration.
Development
After cloning the repository, install dependencies:
bin/setup
Run tests:
rake spec
To interact with the code in the console:
bin/console
To install the gem locally:
bundle exec rake install
Releasing a New Version
- Update the version number in
lib/simple_lock/version.rb
. - Run the command to release the version:
bundle exec rake release
This will create a Git tag, push the code, and upload the .gem
file to RubyGems.
Contributing
Contributions are welcome! Open an issue or submit a pull request on the GitHub repository.
- Fork the project.
- Create a new branch (
git checkout -b feature-branch
). - Commit your changes (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature-branch
). - Create a new Pull Request.
License
The gem is available as open source under the terms of the MIT License.