rapidity 0.0.5.88564 → 0.0.6.88987
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/Gemfile.lock +37 -41
- data/README.md +11 -5
- data/lib/rapidity/composer.rb +8 -3
- data/lib/rapidity/limiter.lua +5 -1
- data/lib/rapidity/limiter.rb +10 -3
- data/lib/rapidity/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 897193cd6efb866ffe133657f6a347efc350c442b3c122dd0cfe470d33d7e962
|
|
4
|
+
data.tar.gz: 669fbea11ae1b8a21f6b74a4c2364d38dd8e0652a461abb29cbd7f9c6d5ac684
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a75bff5e7518aa81dfc54ef4a841766a6eb53f00348fe9f0ded57e75338806050fd2a3195c329f45ba58008597f334f95bbb9f957ee961cb819e1844f5f7f3ed
|
|
7
|
+
data.tar.gz: 9f868af1445b4fc84c6f2b71e734cc9a42656d1099f9475d41fc8a3aa8ee2fbab390e6710dc1540f0df4d5990885d08802528e2195671dc5020ee01da24db6fa
|
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
rapidity (0.0.
|
|
4
|
+
rapidity (0.0.6)
|
|
5
5
|
activesupport
|
|
6
6
|
connection_pool
|
|
7
7
|
redis
|
|
@@ -9,13 +9,12 @@ PATH
|
|
|
9
9
|
GEM
|
|
10
10
|
remote: https://rubygems.org/
|
|
11
11
|
specs:
|
|
12
|
-
activesupport (
|
|
12
|
+
activesupport (7.0.3)
|
|
13
13
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
14
14
|
i18n (>= 1.6, < 2)
|
|
15
15
|
minitest (>= 5.1)
|
|
16
16
|
tzinfo (~> 2.0)
|
|
17
|
-
|
|
18
|
-
addressable (2.7.0)
|
|
17
|
+
addressable (2.8.0)
|
|
19
18
|
public_suffix (>= 2.0.2, < 5.0)
|
|
20
19
|
ansi (1.5.0)
|
|
21
20
|
ast (2.4.2)
|
|
@@ -28,62 +27,60 @@ GEM
|
|
|
28
27
|
byebug (11.1.3)
|
|
29
28
|
coercible (1.0.0)
|
|
30
29
|
descendants_tracker (~> 0.0.1)
|
|
31
|
-
concurrent-ruby (1.1.
|
|
30
|
+
concurrent-ruby (1.1.10)
|
|
32
31
|
connection_pool (2.2.5)
|
|
33
32
|
descendants_tracker (0.0.4)
|
|
34
33
|
thread_safe (~> 0.3, >= 0.3.1)
|
|
35
|
-
diff-lcs (1.
|
|
34
|
+
diff-lcs (1.5.0)
|
|
36
35
|
docile (1.4.0)
|
|
37
36
|
equalizer (0.0.11)
|
|
38
|
-
|
|
39
|
-
flay (2.
|
|
40
|
-
|
|
37
|
+
erubi (1.10.0)
|
|
38
|
+
flay (2.13.0)
|
|
39
|
+
erubi (~> 1.10)
|
|
41
40
|
path_expander (~> 1.0)
|
|
42
41
|
ruby_parser (~> 3.0)
|
|
43
42
|
sexp_processor (~> 4.0)
|
|
44
|
-
flog (4.6.
|
|
43
|
+
flog (4.6.5)
|
|
45
44
|
path_expander (~> 1.0)
|
|
46
45
|
ruby_parser (~> 3.1, > 3.1.0)
|
|
47
46
|
sexp_processor (~> 4.8)
|
|
48
|
-
i18n (1.
|
|
47
|
+
i18n (1.10.0)
|
|
49
48
|
concurrent-ruby (~> 1.0)
|
|
50
49
|
ice_nine (0.11.2)
|
|
51
50
|
kwalify (0.7.2)
|
|
52
51
|
launchy (2.5.0)
|
|
53
52
|
addressable (~> 2.7)
|
|
54
|
-
minitest (5.
|
|
55
|
-
parser (3.
|
|
53
|
+
minitest (5.16.1)
|
|
54
|
+
parser (3.1.2.0)
|
|
56
55
|
ast (~> 2.4.1)
|
|
57
56
|
path_expander (1.1.0)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
rainbow (3.0.0)
|
|
57
|
+
public_suffix (4.0.7)
|
|
58
|
+
rainbow (3.1.1)
|
|
61
59
|
redis (4.7.0)
|
|
62
|
-
reek (6.
|
|
60
|
+
reek (6.1.1)
|
|
63
61
|
kwalify (~> 0.7.0)
|
|
64
|
-
parser (~> 3.
|
|
65
|
-
psych (~> 3.1)
|
|
62
|
+
parser (~> 3.1.0)
|
|
66
63
|
rainbow (>= 2.0, < 4.0)
|
|
67
|
-
rspec (3.
|
|
68
|
-
rspec-core (~> 3.
|
|
69
|
-
rspec-expectations (~> 3.
|
|
70
|
-
rspec-mocks (~> 3.
|
|
64
|
+
rspec (3.11.0)
|
|
65
|
+
rspec-core (~> 3.11.0)
|
|
66
|
+
rspec-expectations (~> 3.11.0)
|
|
67
|
+
rspec-mocks (~> 3.11.0)
|
|
71
68
|
rspec-collection_matchers (1.2.0)
|
|
72
69
|
rspec-expectations (>= 2.99.0.beta1)
|
|
73
|
-
rspec-core (3.
|
|
74
|
-
rspec-support (~> 3.
|
|
75
|
-
rspec-expectations (3.
|
|
70
|
+
rspec-core (3.11.0)
|
|
71
|
+
rspec-support (~> 3.11.0)
|
|
72
|
+
rspec-expectations (3.11.0)
|
|
76
73
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
77
|
-
rspec-support (~> 3.
|
|
78
|
-
rspec-mocks (3.
|
|
74
|
+
rspec-support (~> 3.11.0)
|
|
75
|
+
rspec-mocks (3.11.1)
|
|
79
76
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
80
|
-
rspec-support (~> 3.
|
|
81
|
-
rspec-support (3.
|
|
82
|
-
rspec_junit_formatter (0.
|
|
77
|
+
rspec-support (~> 3.11.0)
|
|
78
|
+
rspec-support (3.11.0)
|
|
79
|
+
rspec_junit_formatter (0.5.1)
|
|
83
80
|
rspec-core (>= 2, < 4, != 2.12.0)
|
|
84
|
-
ruby_parser (3.
|
|
85
|
-
sexp_processor (~> 4.
|
|
86
|
-
rubycritic (4.
|
|
81
|
+
ruby_parser (3.19.1)
|
|
82
|
+
sexp_processor (~> 4.16)
|
|
83
|
+
rubycritic (4.7.0)
|
|
87
84
|
flay (~> 2.8)
|
|
88
85
|
flog (~> 4.4)
|
|
89
86
|
launchy (>= 2.0.0)
|
|
@@ -94,9 +91,9 @@ GEM
|
|
|
94
91
|
simplecov (>= 0.17.0)
|
|
95
92
|
tty-which (~> 0.4.0)
|
|
96
93
|
virtus (~> 1.0)
|
|
97
|
-
sexp_processor (4.
|
|
98
|
-
shoulda-matchers (
|
|
99
|
-
activesupport (>=
|
|
94
|
+
sexp_processor (4.16.1)
|
|
95
|
+
shoulda-matchers (5.1.0)
|
|
96
|
+
activesupport (>= 5.2.0)
|
|
100
97
|
simplecov (0.21.2)
|
|
101
98
|
docile (~> 1.1)
|
|
102
99
|
simplecov-html (~> 0.11)
|
|
@@ -106,21 +103,20 @@ GEM
|
|
|
106
103
|
simplecov
|
|
107
104
|
terminal-table
|
|
108
105
|
simplecov-html (0.12.3)
|
|
109
|
-
simplecov_json_formatter (0.1.
|
|
110
|
-
terminal-table (3.0.
|
|
106
|
+
simplecov_json_formatter (0.1.4)
|
|
107
|
+
terminal-table (3.0.2)
|
|
111
108
|
unicode-display_width (>= 1.1.1, < 3)
|
|
112
109
|
thread_safe (0.3.6)
|
|
113
110
|
timeouter (0.1.3.38794)
|
|
114
111
|
tty-which (0.4.2)
|
|
115
112
|
tzinfo (2.0.4)
|
|
116
113
|
concurrent-ruby (~> 1.0)
|
|
117
|
-
unicode-display_width (2.
|
|
114
|
+
unicode-display_width (2.2.0)
|
|
118
115
|
virtus (1.0.5)
|
|
119
116
|
axiom-types (~> 0.1)
|
|
120
117
|
coercible (~> 1.0)
|
|
121
118
|
descendants_tracker (~> 0.0, >= 0.0.3)
|
|
122
119
|
equalizer (~> 0.0, >= 0.0.9)
|
|
123
|
-
zeitwerk (2.4.2)
|
|
124
120
|
|
|
125
121
|
PLATFORMS
|
|
126
122
|
ruby
|
data/README.md
CHANGED
|
@@ -12,10 +12,12 @@
|
|
|
12
12
|
Simple but fast Redis-backed distributed rate limiter. Allows you to specify time interval and count within to limit distributed operations.
|
|
13
13
|
|
|
14
14
|
Features:
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
|
|
16
|
+
- extremly simple
|
|
17
|
+
- free from race condition through LUA scripting
|
|
18
|
+
- fast
|
|
19
|
+
|
|
20
|
+
[Article(russian) about gem.](https://blog.rnds.pro/029-rapidity/?utm_source=github&utm_medium=repo&utm_campaign=rnds)
|
|
19
21
|
|
|
20
22
|
## Usage
|
|
21
23
|
|
|
@@ -75,18 +77,22 @@ loop do
|
|
|
75
77
|
sleep 1
|
|
76
78
|
end
|
|
77
79
|
end
|
|
78
|
-
|
|
79
80
|
```
|
|
80
81
|
|
|
81
82
|
## Installation
|
|
82
83
|
|
|
83
84
|
It's a gem:
|
|
85
|
+
|
|
84
86
|
```bash
|
|
85
87
|
gem install rapidity
|
|
86
88
|
```
|
|
89
|
+
|
|
87
90
|
There's also the wonders of [the Gemfile](http://bundler.io):
|
|
91
|
+
|
|
88
92
|
```ruby
|
|
89
93
|
gem 'rapidity'
|
|
90
94
|
```
|
|
91
95
|
|
|
96
|
+
## Special Thanks
|
|
92
97
|
|
|
98
|
+
- [WeTransfer/prorate](https://github.com/WeTransfer/prorate) for LUA-examples
|
data/lib/rapidity/composer.rb
CHANGED
|
@@ -41,13 +41,18 @@ module Rapidity
|
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
43
|
|
|
44
|
-
def obtain(count = 5)
|
|
44
|
+
def obtain(count = 5, with_time: false)
|
|
45
|
+
time = nil
|
|
45
46
|
@limiters.each do |limiter|
|
|
46
|
-
count = limiter.obtain(count)
|
|
47
|
+
count, time = limiter.obtain(count, with_time: with_time)
|
|
47
48
|
break if count == 0
|
|
48
49
|
end
|
|
49
50
|
|
|
50
|
-
|
|
51
|
+
if with_time
|
|
52
|
+
[count, time]
|
|
53
|
+
else
|
|
54
|
+
count
|
|
55
|
+
end
|
|
51
56
|
end
|
|
52
57
|
|
|
53
58
|
end
|
data/lib/rapidity/limiter.lua
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
-- args: key, treshold, interval, count
|
|
2
2
|
-- returns: obtained count.
|
|
3
3
|
|
|
4
|
+
-- this is required to be able to use TIME and writes; basically it lifts the script into IO
|
|
5
|
+
redis.replicate_commands()
|
|
6
|
+
|
|
4
7
|
-- make some nicer looking variable names:
|
|
5
8
|
local retval = nil
|
|
6
9
|
|
|
@@ -17,6 +20,7 @@ local count = tonumber(ARGV[3])
|
|
|
17
20
|
local current = 0
|
|
18
21
|
local to_return = 0
|
|
19
22
|
|
|
23
|
+
local redis_time = redis.call("TIME") -- Array of [seconds, microseconds]
|
|
20
24
|
redis.call("SET", key, treshold, "EX", interval, "NX")
|
|
21
25
|
current = redis.call("DECRBY", key, count)
|
|
22
26
|
|
|
@@ -34,4 +38,4 @@ else
|
|
|
34
38
|
retval = count
|
|
35
39
|
end
|
|
36
40
|
|
|
37
|
-
return retval
|
|
41
|
+
return {retval, redis_time}
|
data/lib/rapidity/limiter.rb
CHANGED
|
@@ -40,8 +40,10 @@ module Rapidity
|
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
# Obtain values from counter
|
|
43
|
+
# @params count - try to obtain 'tokens'
|
|
44
|
+
# @params with_time - return or not Redis-service time at which request processed
|
|
43
45
|
# @return count succesfuly obtained send slots
|
|
44
|
-
def obtain(count = 5)
|
|
46
|
+
def obtain(count = 5, with_time: false)
|
|
45
47
|
count = count.abs
|
|
46
48
|
|
|
47
49
|
result = begin
|
|
@@ -59,7 +61,8 @@ module Rapidity
|
|
|
59
61
|
end
|
|
60
62
|
end
|
|
61
63
|
|
|
62
|
-
taken = result.to_i
|
|
64
|
+
taken = result[0].to_i
|
|
65
|
+
time = Time.at(*result[1].map(&:to_i), :millisecond)
|
|
63
66
|
|
|
64
67
|
if taken == 0
|
|
65
68
|
ttl = @pool.with do |conn|
|
|
@@ -73,7 +76,11 @@ module Rapidity
|
|
|
73
76
|
end
|
|
74
77
|
end
|
|
75
78
|
|
|
76
|
-
|
|
79
|
+
if with_time
|
|
80
|
+
[taken, time]
|
|
81
|
+
else
|
|
82
|
+
taken
|
|
83
|
+
end
|
|
77
84
|
end
|
|
78
85
|
|
|
79
86
|
def ensure_script_loaded
|
data/lib/rapidity/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rapidity
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.6.88987
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yurusov Vlad
|
|
@@ -9,7 +9,7 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2022-
|
|
12
|
+
date: 2022-07-01 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: activesupport
|