better_rate_limit 0.1.5 → 0.1.8
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 +17 -17
- data/README.md +9 -0
- data/lib/better_rate_limit/configuration.rb +11 -0
- data/lib/better_rate_limit/redis_connection.rb +7 -1
- data/lib/better_rate_limit/throttle.rb +9 -6
- data/lib/better_rate_limit/version.rb +1 -1
- data/lib/better_rate_limit.rb +17 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9eedeebfe87d1f95a4541b994bcabec39ac02250827174c12da04b612209e4f6
|
4
|
+
data.tar.gz: 0aee3a1cf32a3e59c8b1307dd6364f6610272366267d4c59644741df3d9006b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 162d31096ed6a08dc8ca38b18becb34512f25abd99297bd9b03ac7dd0208499dea1fd8ba2871584a9929ba45555734e8a8611c2b3d64df663b3a60128f007ae3
|
7
|
+
data.tar.gz: b28194386480e1ec62fca06ea62ba8de6ec41eedae07d97710e9afb1bf5af09923fe5f1465a93f3cd85c167f5c18300f74b8242e35eeffed95af66a165ec7c8a
|
data/Gemfile.lock
CHANGED
@@ -1,44 +1,44 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
better_rate_limit (0.1.
|
4
|
+
better_rate_limit (0.1.6)
|
5
5
|
actionpack (>= 5.0)
|
6
6
|
redis (>= 3.3)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
actionpack (5.2.
|
12
|
-
actionview (= 5.2.
|
13
|
-
activesupport (= 5.2.
|
11
|
+
actionpack (5.2.6.2)
|
12
|
+
actionview (= 5.2.6.2)
|
13
|
+
activesupport (= 5.2.6.2)
|
14
14
|
rack (~> 2.0, >= 2.0.8)
|
15
15
|
rack-test (>= 0.6.3)
|
16
16
|
rails-dom-testing (~> 2.0)
|
17
17
|
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
18
|
-
actionview (5.2.
|
19
|
-
activesupport (= 5.2.
|
18
|
+
actionview (5.2.6.2)
|
19
|
+
activesupport (= 5.2.6.2)
|
20
20
|
builder (~> 3.1)
|
21
21
|
erubi (~> 1.4)
|
22
22
|
rails-dom-testing (~> 2.0)
|
23
23
|
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
24
|
-
activesupport (5.2.
|
24
|
+
activesupport (5.2.6.2)
|
25
25
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
26
26
|
i18n (>= 0.7, < 2)
|
27
27
|
minitest (~> 5.1)
|
28
28
|
tzinfo (~> 1.1)
|
29
29
|
builder (3.2.4)
|
30
30
|
coderay (1.1.3)
|
31
|
-
concurrent-ruby (1.1.
|
31
|
+
concurrent-ruby (1.1.9)
|
32
32
|
crass (1.0.6)
|
33
|
-
erubi (1.
|
34
|
-
i18n (1.
|
33
|
+
erubi (1.10.0)
|
34
|
+
i18n (1.9.1)
|
35
35
|
concurrent-ruby (~> 1.0)
|
36
|
-
loofah (2.
|
36
|
+
loofah (2.14.0)
|
37
37
|
crass (~> 1.0.2)
|
38
38
|
nokogiri (>= 1.5.9)
|
39
39
|
method_source (1.0.0)
|
40
40
|
mini_portile2 (2.4.0)
|
41
|
-
minitest (5.
|
41
|
+
minitest (5.15.0)
|
42
42
|
mocha (1.11.2)
|
43
43
|
mock_redis (0.25.0)
|
44
44
|
nokogiri (1.10.10)
|
@@ -47,8 +47,8 @@ GEM
|
|
47
47
|
coderay (~> 1.1)
|
48
48
|
method_source (~> 1.0)
|
49
49
|
rack (2.2.3)
|
50
|
-
rack-test (
|
51
|
-
rack (>= 1.0)
|
50
|
+
rack-test (1.1.0)
|
51
|
+
rack (>= 1.0, < 3)
|
52
52
|
rails-controller-testing (1.0.5)
|
53
53
|
actionpack (>= 5.0.1.rc1)
|
54
54
|
actionview (>= 5.0.1.rc1)
|
@@ -56,13 +56,13 @@ GEM
|
|
56
56
|
rails-dom-testing (2.0.3)
|
57
57
|
activesupport (>= 4.2.0)
|
58
58
|
nokogiri (>= 1.6)
|
59
|
-
rails-html-sanitizer (1.
|
59
|
+
rails-html-sanitizer (1.4.2)
|
60
60
|
loofah (~> 2.3)
|
61
61
|
rake (12.3.3)
|
62
|
-
redis (4.
|
62
|
+
redis (4.3.1)
|
63
63
|
thread_safe (0.3.6)
|
64
64
|
timecop (0.9.1)
|
65
|
-
tzinfo (1.2.
|
65
|
+
tzinfo (1.2.9)
|
66
66
|
thread_safe (~> 0.1)
|
67
67
|
|
68
68
|
PLATFORMS
|
data/README.md
CHANGED
@@ -41,6 +41,15 @@ ActiveSupport::Notifications.subscribe /rate_limit/ do |_name, _start, _finish,
|
|
41
41
|
end
|
42
42
|
```
|
43
43
|
|
44
|
+
### Configuration
|
45
|
+
You can ignore the throttle by using the following configuration
|
46
|
+
```ruby
|
47
|
+
BetterRateLimit.configure do |config|
|
48
|
+
config.ignore = true
|
49
|
+
end
|
50
|
+
```
|
51
|
+
Useful to ignore the throttle on tests.
|
52
|
+
|
44
53
|
## Development
|
45
54
|
|
46
55
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
require 'redis'
|
4
4
|
|
5
|
+
class MissingRedisConfigError < StandardError
|
6
|
+
def initialize
|
7
|
+
super 'Redis client not set'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
5
11
|
module BetterRateLimit
|
6
12
|
module RedisConnection
|
7
13
|
def self.included(host)
|
@@ -10,7 +16,7 @@ module BetterRateLimit
|
|
10
16
|
|
11
17
|
module ClassMethods
|
12
18
|
def redis_client
|
13
|
-
@redis_client ||=
|
19
|
+
@redis_client ||= BetterRateLimit.configuration.redis_client
|
14
20
|
end
|
15
21
|
end
|
16
22
|
end
|
@@ -10,21 +10,24 @@ module BetterRateLimit
|
|
10
10
|
|
11
11
|
class << self
|
12
12
|
def throttle(key, limit:, time_window:)
|
13
|
+
return true if BetterRateLimit.configuration.ignore
|
14
|
+
raise MissingRedisConfigError unless redis_client
|
15
|
+
|
13
16
|
now = Time.now.utc
|
14
17
|
timestamps_count = redis_client.llen key
|
15
18
|
|
16
19
|
if timestamps_count < limit
|
17
|
-
redis_client.multi do
|
18
|
-
|
19
|
-
|
20
|
+
redis_client.multi do |pipeline|
|
21
|
+
pipeline.rpush key, now
|
22
|
+
pipeline.expire key, time_window.to_i
|
20
23
|
end
|
21
24
|
true
|
22
25
|
else
|
23
26
|
first = redis_client.lpop(key)
|
24
27
|
|
25
|
-
redis_client.multi do
|
26
|
-
|
27
|
-
|
28
|
+
redis_client.multi do |pipeline|
|
29
|
+
pipeline.rpush key, now
|
30
|
+
pipeline.expire key, time_window.to_i
|
28
31
|
end
|
29
32
|
|
30
33
|
return false unless first
|
data/lib/better_rate_limit.rb
CHANGED
@@ -1,4 +1,21 @@
|
|
1
1
|
require 'action_controller'
|
2
|
+
require 'better_rate_limit/configuration'
|
3
|
+
|
4
|
+
module BetterRateLimit
|
5
|
+
class << self
|
6
|
+
def configuration
|
7
|
+
@configuration ||= Configuration.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def configure
|
11
|
+
yield configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
def reset_configuration
|
15
|
+
@configuration = Configuration.new
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
2
19
|
|
3
20
|
module ActionController
|
4
21
|
autoload :BetterRateLimit, 'action_controller/better_rate_limit'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: better_rate_limit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pablo Fonseca
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2022-07-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|
@@ -58,6 +58,7 @@ files:
|
|
58
58
|
- bin/setup
|
59
59
|
- lib/action_controller/better_rate_limit.rb
|
60
60
|
- lib/better_rate_limit.rb
|
61
|
+
- lib/better_rate_limit/configuration.rb
|
61
62
|
- lib/better_rate_limit/limit.rb
|
62
63
|
- lib/better_rate_limit/redis_connection.rb
|
63
64
|
- lib/better_rate_limit/throttle.rb
|
@@ -83,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
84
|
- !ruby/object:Gem::Version
|
84
85
|
version: '0'
|
85
86
|
requirements: []
|
86
|
-
rubygems_version: 3.
|
87
|
+
rubygems_version: 3.2.22
|
87
88
|
signing_key:
|
88
89
|
specification_version: 4
|
89
90
|
summary: Rate limit requests
|