transactional_lock 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +1 -0
- data/.travis.yml +10 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +63 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/transactional_lock.rb +12 -0
- data/lib/transactional_lock/active_record_patches.rb +17 -0
- data/lib/transactional_lock/advisory_lock.rb +97 -0
- data/lib/transactional_lock/configuration.rb +27 -0
- data/lib/transactional_lock/transaction_wrapper.rb +28 -0
- data/lib/transactional_lock/version.rb +3 -0
- data/transactional_lock.gemspec +30 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6ccd535b4afbf82dcdc3a4087dffc69a2e2a30be72d469702264d6acd684352b
|
4
|
+
data.tar.gz: 0bdb00b140fe468d9b12651d00c682ab38f833c78876c3419d68f29f291e82de
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1696fcc2cf3f12a8de0871fa975547d6d8f78f8792b2cd6689b7196ee3f25b785debb8538e159a486e7874e2a24774b67887b1169189de7f9374c093cbb3023c
|
7
|
+
data.tar.gz: 67507c3683c8248cd84f71b57eeafd6c37980fcbb464235b6ff202e8bf2ed8b188512bd4b396e313c3ceb9f866011c2200efc7988458a77b01336e3aa386b0bf
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Jan Sandbrink
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
[![build status](https://travis-ci.org/finnlabs/transactional_lock.svg)](https://travis-ci.org/finnlabs/transactional_lock)
|
2
|
+
|
3
|
+
# TransactionalLock
|
4
|
+
|
5
|
+
TransactionalLock is providing access to database advisory locks that will be automatically released
|
6
|
+
upon the end of a transaction (`COMMIT` or `ROLLBACK`).
|
7
|
+
|
8
|
+
As of now this gem only targets MySQL databases, where such locks are not available, thus they
|
9
|
+
are emulated by ensuring that the outmost ActiveRecord transaction will release all\* locks.
|
10
|
+
|
11
|
+
\* "all" refers to "one", because in MySQL a session can only hold a single advisory lock.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'transactional_lock'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install transactional_lock
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
While inside a transaction acquire your lock and you can be sure, that it will be released at
|
32
|
+
the end of the transaction:
|
33
|
+
|
34
|
+
````ruby
|
35
|
+
ActiveRecord::Base.transaction do
|
36
|
+
TransactionalLock::AdvisoryLock.new('your_lock').acquire
|
37
|
+
# do your work
|
38
|
+
end
|
39
|
+
# the lock has been released at this point
|
40
|
+
````
|
41
|
+
|
42
|
+
It will also work for nested AR transactions (that do not really map to your SQL `COMMIT` or `ROLLBACK`):
|
43
|
+
|
44
|
+
````ruby
|
45
|
+
ActiveRecord::Base.transaction do
|
46
|
+
ActiveRecord::Base.transaction do
|
47
|
+
TransactionalLock::AdvisoryLock.new('your_lock').acquire
|
48
|
+
end
|
49
|
+
|
50
|
+
# lock is still acquired here...
|
51
|
+
end # actual SQL COMMIT
|
52
|
+
# the lock has been released at this point
|
53
|
+
````
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/finnlabs/transactional_lock. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
58
|
+
|
59
|
+
|
60
|
+
## License
|
61
|
+
|
62
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
63
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "transactional_lock"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'transactional_lock/active_record_patches'
|
2
|
+
require 'transactional_lock/configuration'
|
3
|
+
require 'transactional_lock/version'
|
4
|
+
|
5
|
+
module TransactionalLock
|
6
|
+
class << self
|
7
|
+
def initialize(&block)
|
8
|
+
::TransactionalLock::ActiveRecordPatches.perform!
|
9
|
+
::TransactionalLock::Configuration.initialize(&block)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'transactional_lock/transaction_wrapper'
|
2
|
+
|
3
|
+
module TransactionalLock
|
4
|
+
module ActiveRecordPatches
|
5
|
+
def self.perform!
|
6
|
+
::ActiveRecord::Base.extend(ActiveRecordBasePatches)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ActiveRecordBasePatches
|
10
|
+
def transaction(*args)
|
11
|
+
TransactionalLock::TransactionWrapper.new.wrap do
|
12
|
+
super
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'transactional_lock/configuration'
|
2
|
+
|
3
|
+
module TransactionalLock
|
4
|
+
class LockAcquireError < StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
class LockConflict < LockAcquireError
|
8
|
+
def initialize(new_lock, old_lock)
|
9
|
+
super("Can't acquire lock '#{new_lock.name}'. Need to release lock '#{old_lock.name}' first.")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Represents a database advisory lock.
|
14
|
+
# N.B. currently quite MySQL specific, except that its interface allows more than one active lock
|
15
|
+
# MySQL will only ever support one lock at a time (releasing earlier locks implicitly)
|
16
|
+
class AdvisoryLock
|
17
|
+
class << self
|
18
|
+
def acquired_locks
|
19
|
+
acquired_locks_changeable.dup.freeze
|
20
|
+
end
|
21
|
+
|
22
|
+
def release_all_locks
|
23
|
+
acquired_locks.each do |lock|
|
24
|
+
lock.release
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def push_lock(lock)
|
29
|
+
acquired_locks_changeable << lock
|
30
|
+
end
|
31
|
+
|
32
|
+
def delete_lock(lock)
|
33
|
+
acquired_locks_changeable.delete(lock)
|
34
|
+
end
|
35
|
+
|
36
|
+
def forget_locks!
|
37
|
+
@acquired_locks = []
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def acquired_locks_changeable
|
43
|
+
@acquired_locks ||= []
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
attr_reader :name, :timeout
|
48
|
+
|
49
|
+
def initialize(name, timeout: ::TransactionalLock::Configuration.default_timeout)
|
50
|
+
@name = name
|
51
|
+
@timeout = timeout
|
52
|
+
@acquired = false
|
53
|
+
end
|
54
|
+
|
55
|
+
def acquire
|
56
|
+
return if already_locked?
|
57
|
+
raise_on_lock_conflicts!
|
58
|
+
|
59
|
+
result = ActiveRecord::Base.connection.execute(
|
60
|
+
"SELECT GET_LOCK('#{sql_name}', #{sql_timeout})")
|
61
|
+
|
62
|
+
unless result.first.first == 1
|
63
|
+
raise LockAcquireError.new "Could not acquire lock '#{@name}'."
|
64
|
+
end
|
65
|
+
|
66
|
+
self.class.push_lock(self)
|
67
|
+
end
|
68
|
+
|
69
|
+
def release
|
70
|
+
ActiveRecord::Base.connection.execute("SELECT RELEASE_LOCK('#{sql_name}')")
|
71
|
+
ensure
|
72
|
+
# In any case consider this lock being released (avoiding inability for new acquires)
|
73
|
+
self.class.delete_lock(self)
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def already_locked?
|
79
|
+
self.class.acquired_locks.any? { |lock| lock.name == name }
|
80
|
+
end
|
81
|
+
|
82
|
+
def raise_on_lock_conflicts!
|
83
|
+
conflict_lock = self.class.acquired_locks.detect { |lock| lock.name != name }
|
84
|
+
if conflict_lock
|
85
|
+
raise LockConflict.new self, conflict_lock
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def sql_name
|
90
|
+
ActiveRecord::Base.connection.quote_string(@name)
|
91
|
+
end
|
92
|
+
|
93
|
+
def sql_timeout
|
94
|
+
Integer(@timeout)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module TransactionalLock
|
2
|
+
class Configuration
|
3
|
+
class << self
|
4
|
+
def initialize
|
5
|
+
config = default_configuration
|
6
|
+
|
7
|
+
if block_given?
|
8
|
+
yield config
|
9
|
+
end
|
10
|
+
|
11
|
+
@config = config
|
12
|
+
end
|
13
|
+
|
14
|
+
def default_timeout
|
15
|
+
@config[:default_timeout]
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def default_configuration
|
21
|
+
{
|
22
|
+
default_timeout: 30
|
23
|
+
}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'transactional_lock/advisory_lock'
|
2
|
+
|
3
|
+
module TransactionalLock
|
4
|
+
class TransactionWrapper
|
5
|
+
# If the root wrapper is not yet set, the specified wrapper will become the root wrapper.
|
6
|
+
def self.try_assign_root_wrapper(wrapper)
|
7
|
+
@root_wrapper ||= wrapper
|
8
|
+
end
|
9
|
+
|
10
|
+
# If the specified wrapper is the root wrapper, it will be deassigned and the block passed
|
11
|
+
# to this function will be executed
|
12
|
+
def self.deassign_root_wrapper(wrapper)
|
13
|
+
if wrapper == @root_wrapper
|
14
|
+
@root_wrapper = nil
|
15
|
+
yield
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def wrap
|
20
|
+
self.class.try_assign_root_wrapper(self)
|
21
|
+
yield
|
22
|
+
ensure
|
23
|
+
self.class.deassign_root_wrapper(self) do
|
24
|
+
::TransactionalLock::AdvisoryLock.release_all_locks
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'transactional_lock/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "transactional_lock"
|
8
|
+
spec.version = TransactionalLock::VERSION
|
9
|
+
spec.authors = ["Jan Sandbrink"]
|
10
|
+
spec.email = ["j.sandbrink@finn.de"]
|
11
|
+
|
12
|
+
spec.summary = %q{Adds advisory locks that are automatically released at the end of a transaction.}
|
13
|
+
spec.homepage = 'https://github.com/finnlabs/transactional_lock'
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# N.B. Though our SQL commands are depending on a MySQL-backend, we do not depend on any
|
22
|
+
# specific SQL-gem. We only need activerecord to do our work, in the hope that our SQL code
|
23
|
+
# makes any sense to the active backend.
|
24
|
+
spec.add_dependency "activerecord", ">= 4.0"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.3"
|
29
|
+
spec.add_development_dependency "sqlite3"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: transactional_lock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan Sandbrink
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- j.sandbrink@finn.de
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- lib/transactional_lock.rb
|
101
|
+
- lib/transactional_lock/active_record_patches.rb
|
102
|
+
- lib/transactional_lock/advisory_lock.rb
|
103
|
+
- lib/transactional_lock/configuration.rb
|
104
|
+
- lib/transactional_lock/transaction_wrapper.rb
|
105
|
+
- lib/transactional_lock/version.rb
|
106
|
+
- transactional_lock.gemspec
|
107
|
+
homepage: https://github.com/finnlabs/transactional_lock
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubygems_version: 3.0.1
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Adds advisory locks that are automatically released at the end of a transaction.
|
130
|
+
test_files: []
|