mysql_getlock 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e4b268a546f480cf35e54bec236f82e2ee74f25
4
- data.tar.gz: a11b170c1fd935e9f02b258b23185d1879fc952f
3
+ metadata.gz: 88dca1f22a91ecae47c4cc919cf9ebbb7469cb7e
4
+ data.tar.gz: 63f0e9adfdd7c2901f69c45fbd2518569397e461
5
5
  SHA512:
6
- metadata.gz: 94050442d95798cd3a54571c8662f87b09c8190aa1709ffe64f48f0aaea0f1119724560f93497b9ee78293f1186f2fe2af63a86b9617daf59155e490015883ba
7
- data.tar.gz: 44e6fc1dbd8202f89fd87f8cc79f61f81572afbaeadc1dfd8e5ecea17d69c92e21d14dd3c56b9aa62373b986369613e32af827c56dc79a3a79f3843ce95e965f
6
+ metadata.gz: e6adaa01efcd040b6ae82fe31332ebf28c9558d31ec3be4d690c8858b1f5aab447e130bef82b70dd7621fcbd17913f36645e4d2b2a001be54f72033c11e527d5
7
+ data.tar.gz: 374c9497d072e02722d2883acfe865cbb7eaf46f15024aba43d3e955f7471e11b5beffa304c63a2c2eb2de12cab5988cf39c9de9a66870f777c1839e6bf5616c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.2.2 (2016-08-29)
2
+
3
+ Enhancements:
4
+
5
+ * Before MySQL 5.5.8, a negative timeout value did not mean infinite timeout on platforms except Windows. Apply large number of timeout (4294967295) if timeout < 0 is given to support infinite timeout spuriously for MySQL < 5.5.8.
6
+
1
7
  # 0.2.1 (2016-08-28)
2
8
 
3
9
  Enhancements:
data/lib/mysql_getlock.rb CHANGED
@@ -5,22 +5,25 @@ class MysqlGetlock
5
5
  attr_reader :mysql2, :key, :logger, :timeout
6
6
 
7
7
  TIMEOUT = -1 # inifinity
8
+ PSEUDO_INFINITE_TIMEOUT = 4294967295 # for < 5.5.8
8
9
 
9
10
  class Error < ::StandardError; end
10
11
  class LockError < ::StandardError; end
11
12
 
12
13
  def initialize(mysql2:, key:, logger: nil, timeout: TIMEOUT)
13
14
  self.mysql2 = mysql2
14
- @key = Mysql2::Client.escape(key)
15
- @logger = logger
16
- @timeout = timeout
15
+ set_key(key)
16
+ set_logger(logger)
17
+ set_timeout(timeout)
17
18
  end
18
19
 
19
20
  # Use this setter if you reconnect mysql2 (which means renew Mysql2::Client instance),
20
21
  # but still want to use same MysqlGetlock instance
21
22
  def mysql2=(mysql2)
22
23
  @mysql2 = mysql2
24
+ @mysql_version = nil
23
25
  @multiple_lockable = nil
26
+ @infinite_timeoutable = nil
24
27
  end
25
28
 
26
29
  def lock
@@ -28,7 +31,7 @@ class MysqlGetlock
28
31
  raise Error, "get_lock() is already issued in the same connection for '#{current_session_key}'"
29
32
  end
30
33
 
31
- logger.info { "#{log_head}Wait #{timeout < 0 ? '' : "#{timeout} sec "}to acquire a mysql lock '#{key}'" } if logger
34
+ logger.info { "#{log_head}Wait #{timeout < -1 ? '' : "#{timeout} sec "}to acquire a mysql lock '#{key}'" } if logger
32
35
  results = mysql2.query(%Q[select get_lock('#{key}', #{timeout})], as: :array)
33
36
  case results.to_a.first.first
34
37
  when 1
@@ -92,6 +95,23 @@ class MysqlGetlock
92
95
 
93
96
  private
94
97
 
98
+ def set_timeout(timeout)
99
+ if infinite_timeoutable?
100
+ @timeout = timeout
101
+ else
102
+ # To support MySQL < 5.5.8, put large number of seconds to express infinite timeout spuriously
103
+ @timeout = (timeout < 0 ? PSEUDO_INFINITE_TIMEOUT : timeout)
104
+ end
105
+ end
106
+
107
+ def set_key(key)
108
+ @key = Mysql2::Client.escape(key)
109
+ end
110
+
111
+ def set_logger(logger)
112
+ @logger = logger
113
+ end
114
+
95
115
  def log_head
96
116
  "PID-#{::Process.pid} TID-#{::Thread.current.object_id.to_s(36)}: "
97
117
  end
@@ -99,10 +119,23 @@ class MysqlGetlock
99
119
  # From MySQL 5.7.5, multiple simultaneous locks can be acquired
100
120
  def multiple_lockable?
101
121
  return @multiple_lockable unless @multiple_lockable.nil?
122
+ major, minor, patch = mysql_version
123
+ @multiple_lockable = (major > 5) || (major == 5 && minor > 7) || (major == 5 && minor == 7 && patch >= 5)
124
+ end
125
+
126
+ # Before MySQL 5.5.8, a negative timeout value means infinite timeout on Windows. As of 5.5.8, this behavior applies on all platforms.
127
+ def infinite_timeoutable?
128
+ return @infinite_timeoutable unless @infinite_timeoutable.nil?
129
+ major, minor, patch = mysql_version
130
+ @infinite_timeoutable = (major > 5) || (major == 5 && minor > 5) || (major == 5 && minor == 5 && patch >= 8)
131
+ end
132
+
133
+ def mysql_version
134
+ return @mysql_version if @mysql_version
102
135
  results = mysql2.query('select version()', as: :array)
103
136
  version = results.to_a.first.first
104
137
  major, minor, patch = version.split('.').map(&:to_i)
105
- @multiple_lockable = (major > 5) || (major == 5 && minor > 7) || (major == 5 && minor == 7 && patch >= 5)
138
+ @mysql_version = [major, minor, patch]
106
139
  end
107
140
 
108
141
  # Before MySQL 5.7.5, only a single simultaneous lock can be acquired
@@ -1,3 +1,3 @@
1
1
  class MysqlGetlock
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysql_getlock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-28 00:00:00.000000000 Z
11
+ date: 2016-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2