redis_getlock 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -3
- data/README.md +7 -7
- data/lib/redis_getlock.rb +10 -10
- data/lib/redis_getlock/version.rb +1 -1
- 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: 010459a0c19e3b361f1f0a72d654c017e24f61a1
|
4
|
+
data.tar.gz: 9b08bf52a858125339c95893d0d76ca2eed1e8a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 08686f08d50978d37900b3cc402583587585b16bf5a45b45611bb8497c0d1b750c4886130bd88aed053e2f203dbc56731d7f3eea2b37036c8a3eed7a68526e42
|
7
|
+
data.tar.gz: efb8e7b53b4c1afbc16c2e9869ded9ee78338ebfec2108e05c9060a63ab49b9dbe8a2a7437161bc723a9a0dc8518a34f2203427df27055b02f58690f686331ca
|
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,15 @@
|
|
1
|
-
# 0.
|
1
|
+
# 0.2.0 (2016-08-28)
|
2
|
+
|
3
|
+
Changes:
|
4
|
+
|
5
|
+
* Rename `timeout` option to `expire` option
|
6
|
+
|
7
|
+
# 0.1.1 (2016-08-28)
|
2
8
|
|
3
9
|
Changes:
|
4
10
|
|
5
11
|
* Trivial change of log format
|
6
12
|
|
7
|
-
# 0.1.0 (2016-
|
13
|
+
# 0.1.0 (2016-08-28)
|
8
14
|
|
9
15
|
First version
|
10
|
-
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@ Simple ruby codes which [http://redis.io/commands/set](http://redis.io/commands/
|
|
10
10
|
|
11
11
|
```ruby
|
12
12
|
loop do
|
13
|
-
break if redis.set(key, 'anystring', {nx: true, ex:
|
13
|
+
break if redis.set(key, 'anystring', {nx: true, ex: expire})
|
14
14
|
sleep 1
|
15
15
|
end
|
16
16
|
puts 'get lock'
|
@@ -21,8 +21,8 @@ ensure
|
|
21
21
|
end
|
22
22
|
```
|
23
23
|
|
24
|
-
The problem here is the value of `
|
25
|
-
The expiration time `
|
24
|
+
The problem here is the value of `expire`.
|
25
|
+
The expiration time `expire` is necessary so that a lock will eventually be released even if a process is crashed or killed by SIGKILL before deleting the key.
|
26
26
|
However, how long should we set if we are uncertain how long a job takes?
|
27
27
|
|
28
28
|
This gem takes a following approach to resolve this problem.
|
@@ -30,7 +30,7 @@ This gem takes a following approach to resolve this problem.
|
|
30
30
|
1. Expiration time is set to `2` (default) seconds
|
31
31
|
2. Extend the lock in each `1` (default) sencond interval invoking another thread
|
32
32
|
|
33
|
-
This way ensures to release orphaned lock in 2 seconds. We are released from caring of the value of `
|
33
|
+
This way ensures to release orphaned lock in 2 seconds. We are released from caring of the value of `expire`!!
|
34
34
|
|
35
35
|
Simple ruby codes to explain how this gem works are as follows:
|
36
36
|
|
@@ -91,10 +91,10 @@ Options of `RedisGetlock.new` are:
|
|
91
91
|
* Key name for a distributed lock
|
92
92
|
* logger
|
93
93
|
* Provide a logger for RedisGetlock (for debug)
|
94
|
-
*
|
95
|
-
* The expiration
|
94
|
+
* expire
|
95
|
+
* The expiration seconds of the lock. The default is `2` second. Users usually do not need to care of this because the expiration is automatically extended in a invoked thread.
|
96
96
|
* interval
|
97
|
-
* Interval to extend lock expiration. Must be `
|
97
|
+
* Interval to extend lock expiration. Must be `expire > interval`. The default is `1` second.
|
98
98
|
|
99
99
|
### Example
|
100
100
|
|
data/lib/redis_getlock.rb
CHANGED
@@ -3,16 +3,16 @@ require "redis_getlock/version"
|
|
3
3
|
require 'securerandom'
|
4
4
|
|
5
5
|
class RedisGetlock
|
6
|
-
attr_reader :redis, :key, :logger, :
|
6
|
+
attr_reader :redis, :key, :logger, :expire, :interval
|
7
7
|
|
8
|
-
|
8
|
+
EXPIRE = 2
|
9
9
|
INTERVAL = 1
|
10
10
|
|
11
|
-
def initialize(redis:, key:, logger: nil,
|
11
|
+
def initialize(redis:, key:, logger: nil, expire: EXPIRE, interval: INTERVAL)
|
12
12
|
@redis = redis
|
13
13
|
@key = key
|
14
14
|
@logger = logger
|
15
|
-
@
|
15
|
+
@expire = expire
|
16
16
|
@interval = interval
|
17
17
|
end
|
18
18
|
|
@@ -64,14 +64,14 @@ class RedisGetlock
|
|
64
64
|
def lock_with_set_options
|
65
65
|
uuid = SecureRandom.uuid
|
66
66
|
loop do
|
67
|
-
break if redis.set(key, uuid, {nx: true, ex:
|
67
|
+
break if redis.set(key, uuid, {nx: true, ex: expire}) # key does not exist
|
68
68
|
sleep interval
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
72
|
def keeplock_with_set_options
|
73
73
|
loop do
|
74
|
-
redis.expire(key,
|
74
|
+
redis.expire(key, expire) # extend expiration
|
75
75
|
sleep interval
|
76
76
|
end
|
77
77
|
end
|
@@ -81,13 +81,13 @@ class RedisGetlock
|
|
81
81
|
def lock_without_set_options
|
82
82
|
loop do
|
83
83
|
current = Time.now.to_f
|
84
|
-
if redis.setnx(key, (current +
|
85
|
-
redis.expire(key,
|
84
|
+
if redis.setnx(key, (current + expire).to_s) # key does not exist
|
85
|
+
redis.expire(key, expire)
|
86
86
|
break # acquire lock
|
87
87
|
end
|
88
88
|
expired = redis.get(key)
|
89
89
|
if expired.to_f < current # key exists, but expired
|
90
|
-
compared = redis.getset(key, (current +
|
90
|
+
compared = redis.getset(key, (current + expire).to_s)
|
91
91
|
break if expired == compared # acquire lock
|
92
92
|
end
|
93
93
|
sleep interval
|
@@ -97,7 +97,7 @@ class RedisGetlock
|
|
97
97
|
def keeplock_without_set_options
|
98
98
|
loop do
|
99
99
|
current = Time.now.to_f
|
100
|
-
redis.setex(key,
|
100
|
+
redis.setex(key, expire, (current + expire).to_s) # extend expiration
|
101
101
|
sleep interval
|
102
102
|
end
|
103
103
|
end
|