0.0
The project is in a healthy, maintained state
Simple Deadlock implementation using Ruby.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 13.0
~> 3.0
~> 1.21

Runtime

>= 3.0
 Project Readme

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

  1. Update the version number in lib/simple_lock/version.rb.
  2. 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.

  1. Fork the project.
  2. Create a new branch (git checkout -b feature-branch).
  3. Commit your changes (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature-branch).
  5. Create a new Pull Request.

License

The gem is available as open source under the terms of the MIT License.