pesto 0.0.11 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -3
- data/examples/aggressive.rb +3 -4
- data/examples/simple.rb +42 -0
- data/lib/pesto.rb +0 -1
- data/lib/pesto/lock.rb +23 -22
- data/lib/pesto/version.rb +1 -1
- data/pesto.gemspec +0 -1
- metadata +3 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0802911e97e5cd42de96021e46103e4d8730bef6'
|
4
|
+
data.tar.gz: a6a6a9f48f7b52e4574705dc220c265fc04183cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb17c467e1848403b5e590f430605d193052abe2ff7b1ea35cb6bbb8ce5e888c3a04787ffdb0dd83ab87ddf16dc6a29d59cd10cec7e0ccf044e33dbc35e9873f
|
7
|
+
data.tar.gz: ff214b249fa8e1513a82542654a20a29ff8833f12e85f655dc556f5046bf91a65367f04a111dcf33478c1185ac9d7a3d67421bc4be7ad527c94e9480c5bda0c0
|
data/Gemfile.lock
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
pesto (0.0.
|
4
|
+
pesto (0.0.15)
|
5
5
|
connection_pool (~> 2.2)
|
6
|
-
hiredis (~> 0.6)
|
7
6
|
redis (~> 4.0)
|
8
7
|
|
9
8
|
GEM
|
@@ -13,7 +12,6 @@ GEM
|
|
13
12
|
diff-lcs (1.3)
|
14
13
|
docile (1.1.5)
|
15
14
|
fakeredis (0.1.4)
|
16
|
-
hiredis (0.6.1)
|
17
15
|
json (2.1.0)
|
18
16
|
redis (4.0.1)
|
19
17
|
rspec (3.6.0)
|
data/examples/aggressive.rb
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
require 'connection_pool'
|
2
|
-
require 'hiredis'
|
3
2
|
require 'redis'
|
4
3
|
require 'securerandom'
|
5
4
|
require_relative '../lib/pesto.rb'
|
6
5
|
|
7
6
|
$use_sleep = false
|
8
|
-
$key_num =
|
7
|
+
$key_num = 5
|
9
8
|
$concurrency = 10
|
10
9
|
|
11
10
|
def lock(ctx, pfx, pid = 0)
|
12
|
-
pl = Pesto::Lock.new({ :pool => ctx[:pool]
|
11
|
+
pl = Pesto::Lock.new({ :pool => ctx[:pool] })
|
13
12
|
keys = []
|
14
13
|
|
15
14
|
for i in 0..$key_num
|
@@ -46,7 +45,7 @@ Signal.trap('TERM') { killall(children) }
|
|
46
45
|
for pid in 0..$concurrency
|
47
46
|
puts "[#{pid}] fork"
|
48
47
|
children << fork do
|
49
|
-
redis = ConnectionPool.new(size:
|
48
|
+
redis = ConnectionPool.new(size: 3, :timeout => 10) { Redis.new }
|
50
49
|
while true do
|
51
50
|
lock({ :pool => redis }, pfx, pid)
|
52
51
|
if $use_sleep
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'connection_pool'
|
2
|
+
require 'redis'
|
3
|
+
require 'securerandom'
|
4
|
+
require_relative '../lib/pesto.rb'
|
5
|
+
|
6
|
+
$key_num = 5
|
7
|
+
|
8
|
+
def lock(ctx, pfx, pid = 0)
|
9
|
+
pl = Pesto::Lock.new({ :pool => ctx[:pool], :verbose => true })
|
10
|
+
keys = []
|
11
|
+
|
12
|
+
for i in 0..$key_num
|
13
|
+
keys << "pesto:#{pfx}:#{i}"
|
14
|
+
end
|
15
|
+
|
16
|
+
keys.shuffle!
|
17
|
+
|
18
|
+
d1 = Time.now
|
19
|
+
|
20
|
+
locked = pl.lock(keys, timeout_lock: 0.05, interval_check: 0.005 )
|
21
|
+
|
22
|
+
if locked == 1
|
23
|
+
pl.unlock(keys)
|
24
|
+
puts "[#{pid}] lock acquired/dismissed (took: #{(Time.now - d1) * 1000}ms)"
|
25
|
+
else
|
26
|
+
puts "[#{pid}] lock failed"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
pfx = SecureRandom.hex(10)
|
31
|
+
|
32
|
+
def killall()
|
33
|
+
Process.exit
|
34
|
+
end
|
35
|
+
|
36
|
+
Signal.trap('INT') { killall }
|
37
|
+
Signal.trap('TERM') { killall }
|
38
|
+
|
39
|
+
redis = ConnectionPool.new(size: 5, :timeout => 10) { Redis.new }
|
40
|
+
while true do
|
41
|
+
lock({ :pool => redis }, pfx, 0)
|
42
|
+
end
|
data/lib/pesto.rb
CHANGED
data/lib/pesto/lock.rb
CHANGED
@@ -7,7 +7,6 @@ module Pesto
|
|
7
7
|
raise 'ERR_REDIS_NOTFOUND' if @ctx[:pool].nil?
|
8
8
|
|
9
9
|
@conf = {
|
10
|
-
:lock_expire => true,
|
11
10
|
:timeout_lock_expire => 5,
|
12
11
|
:timeout_lock => 1,
|
13
12
|
:interval_check => 0.05
|
@@ -20,19 +19,20 @@ module Pesto
|
|
20
19
|
cp.with do |rc|
|
21
20
|
@script_sha = rc.script(
|
22
21
|
:load,
|
23
|
-
"local
|
24
|
-
|
25
|
-
|
26
|
-
if redis.call('setnx', KEYS[1], 1) == 1 then \
|
27
|
-
if tonumber(ARGV[1]) > -1 then \
|
28
|
-
redis.call('expire', KEYS[1], timeout) \
|
29
|
-
end
|
30
|
-
return 1 \
|
31
|
-
else \
|
22
|
+
"local ret = 0 \
|
23
|
+
local timeout = tonumber(ARGV[1]) \
|
24
|
+
if type(timeout) ~= 'number' then \
|
32
25
|
return 0 \
|
33
|
-
end
|
26
|
+
end \
|
27
|
+
local res = redis.call('setnx', KEYS[1], 1) \
|
28
|
+
if res == 1 then \
|
29
|
+
redis.call('expire', KEYS[1], ARGV[1]) \
|
30
|
+
ret = 1 \
|
31
|
+
else \
|
32
|
+
ret = 0 \
|
33
|
+
end \
|
34
|
+
return ret"
|
34
35
|
)
|
35
|
-
puts @script_sha
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
@@ -58,6 +58,7 @@ module Pesto
|
|
58
58
|
opts[:timeout_lock_expire] += (opts[:timeout_lock] * names.size).ceil.to_i
|
59
59
|
|
60
60
|
t_start = Time.now
|
61
|
+
stop = false
|
61
62
|
|
62
63
|
while true
|
63
64
|
res, locks, stop = get_locks names, {
|
@@ -78,23 +79,23 @@ module Pesto
|
|
78
79
|
locks = []
|
79
80
|
res = []
|
80
81
|
|
81
|
-
timeout_lock_expire =
|
82
|
+
timeout_lock_expire = opts[:timeout_lock_expire]
|
82
83
|
|
83
84
|
cp.with do |rc|
|
84
|
-
res = rc.
|
85
|
+
res = rc.multi do
|
85
86
|
names.each do |n|
|
86
|
-
rc.evalsha(@script_sha, {
|
87
|
+
res << rc.evalsha(@script_sha, {
|
87
88
|
:keys => [lock_hash(n)],
|
88
89
|
:argv => [timeout_lock_expire]
|
89
90
|
})
|
90
91
|
end
|
91
92
|
end
|
93
|
+
end
|
92
94
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
end
|
95
|
+
names.each_with_index do |n, ix|
|
96
|
+
next if res[ix] != 1
|
97
|
+
locked += 1
|
98
|
+
locks.push n
|
98
99
|
end
|
99
100
|
|
100
101
|
return [res, locks, locked == names.size]
|
@@ -117,14 +118,14 @@ module Pesto
|
|
117
118
|
res = []
|
118
119
|
|
119
120
|
cp.with do |rc|
|
120
|
-
res = rc.
|
121
|
+
res = rc.multi do
|
121
122
|
names.each do |n|
|
122
123
|
rc.del(lock_hash(n))
|
123
124
|
end
|
124
125
|
end
|
125
126
|
end
|
126
127
|
|
127
|
-
val = res.reduce(0){|sum,n| sum+n}
|
128
|
+
val = res.reduce(0){|sum, n| sum + n}
|
128
129
|
|
129
130
|
val > 0 ? 1 : 0
|
130
131
|
end
|
data/lib/pesto/version.rb
CHANGED
data/pesto.gemspec
CHANGED
@@ -13,7 +13,6 @@ Gem::Specification.new do |s|
|
|
13
13
|
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
14
14
|
s.require_paths = ["lib"]
|
15
15
|
|
16
|
-
s.add_runtime_dependency "hiredis", "~> 0.6"
|
17
16
|
s.add_runtime_dependency "redis", "~> 4.0"
|
18
17
|
s.add_runtime_dependency "connection_pool", "~> 2.2"
|
19
18
|
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pesto
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- bfx devs
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-01
|
11
|
+
date: 2018-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: hiredis
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0.6'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0.6'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: redis
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -106,6 +92,7 @@ files:
|
|
106
92
|
- Gemfile.lock
|
107
93
|
- README.md
|
108
94
|
- examples/aggressive.rb
|
95
|
+
- examples/simple.rb
|
109
96
|
- lib/pesto.rb
|
110
97
|
- lib/pesto/lock.rb
|
111
98
|
- lib/pesto/version.rb
|