Project

lockless

0.0
No release in over a year
Allows for safe concurrent updates to a single record without the need for locks.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.18
~> 4.7.3
~> 0.13.0
~> 1.1
~> 13.0
~> 3.10

Runtime

 Project Readme

Lockless

Gem Version CI Status

Allows for safe concurrent updates to a single record without the need for locks.

This is done by only updating the record when the lockless_uuid is unchanged and updating the lockless_uuid with each update. Since the SQL update command is atomic we can scope the update to the old lockless_uuid and update it to a new value in a single update command.

Installation

Add gem

Add this line to your application's Gemfile:

gem 'lockless'

And then execute:

$ bundle install

Or install it yourself:

$ gem install lockless

Usage

Classify model as "Lockless"

class User < ActiveRecord::Base
  include Lockless::Model
end

Migration to add lockless_uuid column

Manually add migration file or generate base with:

bundle exec rails g migration AddLocklessUuidTo<ModelName> lockless_uuid:string
# e.g.
bundle exec rails g migration AddLocklessUuidToUser lockless_uuid:string

Update migration to add default value and and make non-nullable

class AddLocklessUuidToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :lockless_uuid, :string, default: "lockless", null: false
  end
end

Custom column name

class User < ActiveRecord::Base
  include Lockless::Model
  self.lockless_column = :custom_uuid
end

Sample Usage

user1 = User.first # => #<User id: 1, ...>
user1.name = "new name1"

# other process/service/application
user2 = User.first # => #<User id: 1, ...>
user2.name = "new name2"

# Save user2 before saving user1
user2.lockless_save! # => true

# user1 fails to save as it's been updated earlier by user2
user1.lockless_save! # => false

# when lockless_save doesn't work you can either ignore the update and continue
#   or reload the record and try again
#   or calling `.save!`/`save` will forcefully save changes without respecting lockless
#   `.save!` will cause the UUID to be changed again

user1.reload
user1.name # => "new name2"
user1.name = "new name3"
user1.lockless_save! # => true
user1.reload.name # => "new name3"

user1.reload
user1.name = "new name1"
User.all.update_all(name: "new name4")

# user1 fails to save as it's been updated earlier in `update_all`
user1.lockless_save! # => false
user1.reload.name # => "new name4"

Development

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 run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Testing

Run all specs + standardrb

bundle exec rake

Run only standardrb

bundle exec rake standard

Apply standardrb auto fixes

bundle exec rake standard:fix

Run specs using guard

bundle exec guard

This will auto run the related unit tests while you develop and save files.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/cianmce/lockless. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

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

Code of Conduct

Everyone interacting in the Lockless project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

TODO

  • Allow for custom primary key column name
  • Allow a boolean to be passed to allow for validation to be skipped like in .save