pesto 0.0.2

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/pesto.rb +130 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1ce15447de05fc17b3781bebcad99931e92402b6
4
+ data.tar.gz: 61e949e0378bbd297025262840a11ac7eb84e751
5
+ SHA512:
6
+ metadata.gz: 3ef5b0f54bb4f2ea50b2ddf49dbd5e7904187683f571c27e492f8ff972dfe71a3477233e92fe5d92c229e5301148bcf9dc0b83a8116fd0e51f4a290f291a24c0
7
+ data.tar.gz: a4cae37f9e05a9fa9386b2c9e4f1898906dc019560f9491e9562f7fd0596d1f18dff63d9681b4af5fe69128e4f500ec23fb765755c5060524bc2dbfc938c50b3
data/lib/pesto.rb ADDED
@@ -0,0 +1,130 @@
1
+ module Pesto
2
+ class Lock
3
+
4
+ def initialize(ctx = {}, opts = {})
5
+ @ctx = ctx
6
+
7
+ raise 'ERR_REDIS_NOTFOUND' if @ctx[:redis].nil?
8
+
9
+ @conf = {
10
+ :lock_expire => false,
11
+ :timeout_lock_expire => 300,
12
+ :timeout_lock => 90,
13
+ :interval_check => 0.05,
14
+ :concurrency_limit => 0,
15
+ :concurrency_count => false
16
+ }.merge(opts)
17
+
18
+ end
19
+
20
+ def rc
21
+ @ctx[:redis]
22
+ end
23
+
24
+ def lock(_names, _opts = {})
25
+ opts = {}.merge(
26
+ @conf.select{ |k| [
27
+ :timeout_lock_expire, :timeout_lock,
28
+ :interval_check, :concurrency_limit
29
+ ].include?(k)
30
+ }
31
+ ).merge(_opts || {})
32
+
33
+ _names = [_names] if _names.is_a?(String)
34
+ names = _names.uniq
35
+
36
+ opts[:timeout_lock_expire] = (opts[:timeout_lock_expire] + (opts[:timeout_lock] * names.size)).to_i
37
+
38
+ t_start = Time.now
39
+ locked = 0
40
+
41
+ while true
42
+ locked = 0
43
+
44
+ res = rc.pipelined do
45
+ names.each do |n|
46
+ chash = lock_hash(n)
47
+ rc.setnx chash, 1
48
+ end
49
+ end
50
+
51
+ locks = []
52
+
53
+ names.each_with_index do |n, ix|
54
+ l = res[ix]
55
+ next if !l
56
+ locked += 1
57
+ locks << n
58
+ end
59
+
60
+ if locked == names.size
61
+ locked = 1
62
+
63
+ if @conf[:lock_expire]
64
+ res = rc.pipelined do
65
+ names.each do |n|
66
+ chash = lock_hash(n)
67
+ rc.expire chash, opts[:timeout_lock_expire]
68
+ end
69
+ end
70
+ end
71
+ else
72
+ locked = 0
73
+ end
74
+
75
+ break if locked == 1 || (Time.now - t_start) > opts[:timeout_lock]
76
+
77
+ unlock(locks)
78
+ sleep opts[:interval_check]
79
+ end
80
+
81
+ locked == 0 ? 0 : 1
82
+ end
83
+
84
+ def locki(name = 'global', _opts = {})
85
+ opts = (_opts || {}).merge({ :timeout_lock => 0 })
86
+ lock(name, opts)
87
+ end
88
+
89
+ def lockx(name = 'global', opts = {}, err = 'ERR_LOCKING')
90
+ locked = lock(name, opts)
91
+ if locked == 1
92
+ return 1
93
+ end
94
+ raise "#{err} (#{name})"
95
+ end
96
+
97
+ def lockm(_names = [], opts = {})
98
+ lock(_names, opts)
99
+ end
100
+
101
+ def unlock(_names)
102
+ _names = [_names] if _names.is_a?(String)
103
+ names = _names.uniq
104
+
105
+ res = rc.pipelined do
106
+ names.each do |n|
107
+ rc.del(lock_hash(n))
108
+ end
109
+ end
110
+
111
+ val = 0
112
+ res.each do |v|
113
+ val += v
114
+ end
115
+
116
+ val > 0 ? 1 : 0
117
+ end
118
+
119
+ def unlockm(_names)
120
+ unlock(_names)
121
+ end
122
+
123
+ private
124
+
125
+ def lock_hash(name)
126
+ "pesto:lock:#{name}"
127
+ end
128
+
129
+ end
130
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pesto
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - bfx devs
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: dlock
14
+ email: info@bitfinex.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/pesto.rb
20
+ homepage: https://www.bitfinex.com
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubyforge_project:
39
+ rubygems_version: 2.4.8
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: dlock
43
+ test_files: []