mysql_getlock 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +3 -2
- data/bin/try +18 -2
- data/lib/mysql_getlock/version.rb +1 -1
- data/lib/mysql_getlock.rb +3 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e4b268a546f480cf35e54bec236f82e2ee74f25
|
4
|
+
data.tar.gz: a11b170c1fd935e9f02b258b23185d1879fc952f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94050442d95798cd3a54571c8662f87b09c8190aa1709ffe64f48f0aaea0f1119724560f93497b9ee78293f1186f2fe2af63a86b9617daf59155e490015883ba
|
7
|
+
data.tar.gz: 44e6fc1dbd8202f89fd87f8cc79f61f81572afbaeadc1dfd8e5ecea17d69c92e21d14dd3c56b9aa62373b986369613e32af827c56dc79a3a79f3843ce95e965f
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -26,6 +26,7 @@ Note that
|
|
26
26
|
|
27
27
|
1. Before 5.7.5, only a single simultaneous lock can be acquired in a session, and `get_lock()` releases any existing lock.
|
28
28
|
* This gem raises `MysqlGetlock::Error` at `#lock` if another `get_lock()` for another key is issued in a session to prevent accidental releases of existing lock.
|
29
|
+
* MEMO: lock twice for the same key in a session does not block, only one lock is held for both mysql < 5.7.5 and >= 5.7.5.
|
29
30
|
2. The key name is global in a mysql instance. It is advised to use database-specific or application-specific lock names such as `db_name.str` or `app_name.environment.str`
|
30
31
|
|
31
32
|
## Installation
|
@@ -49,11 +50,11 @@ Or install it yourself as:
|
|
49
50
|
Similarly with ruby standard library [mutex](https://ruby-doc.org/core-2.2.0/Mutex.html), following methods are available:
|
50
51
|
|
51
52
|
* lock
|
52
|
-
* Attempts to grab the lock and waits if it isn’t
|
53
|
+
* Attempts to grab the lock and waits if it isn’t available. Returns true if successfully acquired a lock
|
53
54
|
* locked?
|
54
55
|
* Returns true if this lock is currently held by some (including myself)
|
55
56
|
* synchronize {}
|
56
|
-
* Obtains a lock, runs the block, and releases the lock when the block completes.
|
57
|
+
* Obtains a lock, runs the block, and releases the lock when the block completes. Raises `MysqlGetlock::LockError` if failed to acquire a lock
|
57
58
|
* unlock
|
58
59
|
* Releases the lock. Returns true if successfully released a lock.
|
59
60
|
* self_locked?
|
data/bin/try
CHANGED
@@ -3,8 +3,23 @@
|
|
3
3
|
require "bundler/setup"
|
4
4
|
require "mysql_getlock"
|
5
5
|
require 'logger'
|
6
|
+
require 'optparse'
|
6
7
|
|
7
|
-
|
8
|
+
opts = {
|
9
|
+
timeout: -1,
|
10
|
+
kill: false
|
11
|
+
}
|
12
|
+
OptionParser.new.tap {|op|
|
13
|
+
op.on('--timeout VALUE') {|v|
|
14
|
+
opts[:timeout] = Float(v)
|
15
|
+
}
|
16
|
+
op.on('--kill') {|v|
|
17
|
+
opts[:kill] = true
|
18
|
+
}
|
19
|
+
op.parse(ARGV)
|
20
|
+
}
|
21
|
+
|
22
|
+
if opts[:kill]
|
8
23
|
trap('INT') do
|
9
24
|
exit!
|
10
25
|
end
|
@@ -12,7 +27,8 @@ end
|
|
12
27
|
|
13
28
|
mysql2 = Mysql2::Client.new
|
14
29
|
mutex = MysqlGetlock.new(
|
15
|
-
mysql2: mysql2, key: '
|
30
|
+
mysql2: mysql2, key: 'mysql_getlock', logger: Logger.new(STDOUT),
|
31
|
+
timeout: opts[:timeout]
|
16
32
|
)
|
17
33
|
mutex.synchronize do
|
18
34
|
loop do
|
data/lib/mysql_getlock.rb
CHANGED
@@ -7,6 +7,7 @@ class MysqlGetlock
|
|
7
7
|
TIMEOUT = -1 # inifinity
|
8
8
|
|
9
9
|
class Error < ::StandardError; end
|
10
|
+
class LockError < ::StandardError; end
|
10
11
|
|
11
12
|
def initialize(mysql2:, key:, logger: nil, timeout: TIMEOUT)
|
12
13
|
self.mysql2 = mysql2
|
@@ -27,7 +28,7 @@ class MysqlGetlock
|
|
27
28
|
raise Error, "get_lock() is already issued in the same connection for '#{current_session_key}'"
|
28
29
|
end
|
29
30
|
|
30
|
-
logger.info { "#{log_head}Wait
|
31
|
+
logger.info { "#{log_head}Wait #{timeout < 0 ? '' : "#{timeout} sec "}to acquire a mysql lock '#{key}'" } if logger
|
31
32
|
results = mysql2.query(%Q[select get_lock('#{key}', #{timeout})], as: :array)
|
32
33
|
case results.to_a.first.first
|
33
34
|
when 1
|
@@ -81,7 +82,7 @@ class MysqlGetlock
|
|
81
82
|
end
|
82
83
|
|
83
84
|
def synchronize(&block)
|
84
|
-
lock
|
85
|
+
raise LockError unless lock
|
85
86
|
begin
|
86
87
|
yield
|
87
88
|
ensure
|