rapidity 0.0.5.88265 → 0.0.6.88627
Sign up to get free protection for your applications and to get access to all the features.
- 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 +41 -0
- data/lib/rapidity/limiter.rb +38 -27
- data/lib/rapidity/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4812c1514ceb875e24c9c4a9b86ca1eaef1227eceb13f8b1ae7498c7579e6809
|
4
|
+
data.tar.gz: 0b812cebd0ed83cdfa35e52550d73b42fbabed89b60e771695c02cf0720d71e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b037c65bed50ed704dace8fcc16cfa554e39c810d72b9f1da7b430b2a1bad6f1d7799bfd2e0f8615ece8f45b6fa2457445e94f2996e3a1caac31d0f5b9ecac52
|
7
|
+
data.tar.gz: cb0ada7d5095aac7bd0c447f95d7de55628b6574cc7b07535ff548d64d3cf9dc640fa8d780772892dc59d4e90ad44d54cee1f4808799bda447929ef874677c1f
|
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
|
@@ -0,0 +1,41 @@
|
|
1
|
+
-- args: key, treshold, interval, count
|
2
|
+
-- returns: obtained count.
|
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
|
+
|
7
|
+
-- make some nicer looking variable names:
|
8
|
+
local retval = nil
|
9
|
+
|
10
|
+
-- Redis documentation recommends passing the keys separately so that Redis
|
11
|
+
-- can - in the future - verify that they live on the same shard of a cluster, and
|
12
|
+
-- raise an error if they are not. As far as can be understood this functionality is not
|
13
|
+
-- yet present, but if we can make a little effort to make ourselves more future proof
|
14
|
+
-- we should.
|
15
|
+
local key = KEYS[1]
|
16
|
+
local treshold = tonumber(ARGV[1])
|
17
|
+
local interval = tonumber(ARGV[2])
|
18
|
+
local count = tonumber(ARGV[3])
|
19
|
+
|
20
|
+
local current = 0
|
21
|
+
local to_return = 0
|
22
|
+
|
23
|
+
local redis_time = redis.call("TIME") -- Array of [seconds, microseconds]
|
24
|
+
redis.call("SET", key, treshold, "EX", interval, "NX")
|
25
|
+
current = redis.call("DECRBY", key, count)
|
26
|
+
|
27
|
+
-- If we became below zero we must return some value back
|
28
|
+
if current < 0 then
|
29
|
+
to_return = math.min(count, math.abs(current))
|
30
|
+
|
31
|
+
-- set 0 to current counter value
|
32
|
+
redis.call("SET", key, 0, 'KEEPTTL')
|
33
|
+
|
34
|
+
-- return obtained part of requested count
|
35
|
+
retval = count - to_return
|
36
|
+
else
|
37
|
+
-- return full of requested count
|
38
|
+
retval = count
|
39
|
+
end
|
40
|
+
|
41
|
+
return {retval, redis_time}
|
data/lib/rapidity/limiter.rb
CHANGED
@@ -6,6 +6,7 @@ module Rapidity
|
|
6
6
|
|
7
7
|
attr_reader :pool, :name, :interval, :threshold, :namespace
|
8
8
|
|
9
|
+
LUA_SCRIPT_CODE = File.read(File.join(__dir__, 'limiter.lua'))
|
9
10
|
|
10
11
|
# Convert message to given class
|
11
12
|
# @params pool - inititalized Redis pool
|
@@ -30,51 +31,61 @@ module Rapidity
|
|
30
31
|
# @return remaining counter value
|
31
32
|
def remains
|
32
33
|
results = @pool.with do |conn|
|
33
|
-
conn.multi do
|
34
|
-
|
35
|
-
|
34
|
+
conn.multi do |pipeline|
|
35
|
+
pipeline.set(key('remains'), threshold, ex: interval, nx: true)
|
36
|
+
pipeline.get(key('remains'))
|
36
37
|
end
|
37
38
|
end
|
38
|
-
results[1].to_i #=>
|
39
|
+
results[1].to_i #=> pipeline.get(key('remains'))
|
39
40
|
end
|
40
41
|
|
41
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
|
42
45
|
# @return count succesfuly obtained send slots
|
43
|
-
def obtain(count = 5)
|
46
|
+
def obtain(count = 5, with_time: false)
|
44
47
|
count = count.abs
|
45
48
|
|
46
|
-
|
47
|
-
|
48
|
-
conn.
|
49
|
-
|
49
|
+
result = begin
|
50
|
+
@pool.with do |conn|
|
51
|
+
conn.evalsha(@script, keys: [key('remains')], argv: [threshold, interval, count])
|
52
|
+
end
|
53
|
+
rescue Redis::CommandError => e
|
54
|
+
if e.message.include?('NOSCRIPT')
|
55
|
+
# The Redis server has never seen this script before. Needs to run only once in the entire lifetime
|
56
|
+
# of the Redis server, until the script changes - in which case it will be loaded under a different SHA
|
57
|
+
ensure_script_loaded
|
58
|
+
retry
|
59
|
+
else
|
60
|
+
raise e
|
50
61
|
end
|
51
62
|
end
|
52
63
|
|
53
|
-
taken =
|
64
|
+
taken = result[0].to_i
|
65
|
+
time = Time.at(*result[1].map(&:to_i), :millisecond)
|
54
66
|
|
55
|
-
if taken
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
results = @pool.with do |conn|
|
60
|
-
conn.multi do
|
61
|
-
conn.set(key('remains'), threshold - to_return, ex: interval, nx: true)
|
62
|
-
conn.incrby(key('remains'), to_return)
|
63
|
-
conn.ttl(key('remains'))
|
64
|
-
end
|
67
|
+
if taken == 0
|
68
|
+
ttl = @pool.with do |conn|
|
69
|
+
conn.ttl(key('remains'))
|
65
70
|
end
|
66
71
|
|
67
|
-
ttl
|
68
|
-
|
69
|
-
# reset if no ttl present
|
70
|
-
if ttl == -1
|
72
|
+
# UNKNOWN BUG? reset if no ttl present. Many years ago once upon time we meet our key without TTL
|
73
|
+
if ttl == -1
|
71
74
|
STDERR.puts "ERROR[#{Time.now}]: TTL for key #{key('remains').inspect} disappeared!"
|
72
|
-
@pool.with {|c| c.expire(key('remains'), interval) }
|
75
|
+
@pool.with {|c| c.expire(key('remains'), interval) }
|
73
76
|
end
|
77
|
+
end
|
74
78
|
|
75
|
-
|
79
|
+
if with_time
|
80
|
+
[taken, time]
|
76
81
|
else
|
77
|
-
|
82
|
+
taken
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def ensure_script_loaded
|
87
|
+
@script = @pool.with do |conn|
|
88
|
+
conn.script(:load, LUA_SCRIPT_CODE)
|
78
89
|
end
|
79
90
|
end
|
80
91
|
|
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.88627
|
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-06-
|
12
|
+
date: 2022-06-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -235,6 +235,7 @@ files:
|
|
235
235
|
- README.md
|
236
236
|
- lib/rapidity.rb
|
237
237
|
- lib/rapidity/composer.rb
|
238
|
+
- lib/rapidity/limiter.lua
|
238
239
|
- lib/rapidity/limiter.rb
|
239
240
|
- lib/rapidity/version.rb
|
240
241
|
homepage:
|