rails-action_throttling 0.1.1 → 0.1.2
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 -1
- data/README.md +6 -6
- data/lib/action_throttling.rb +16 -4
- data/lib/action_throttling/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 160f1ac8a6f56f24b75028edd56a27877bb8dc5d42750a86f6202dd0c142890d
|
4
|
+
data.tar.gz: 77c4d7913576a41054fce1e026753fd35af8fdce3407b6d345a0ed1a6df42b3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ac62c6c1e7d5c2f50fd97de965175003b0f60fba58b1cd8f223c37fb54930e91756aaefbe953ea976552a0a69dd1addb21be8896e15827b795e7356bb9cc93c
|
7
|
+
data.tar.gz: 82b31893a480cdbfabcc36250e2293cdf4f87eb95ec7a20c3f416d1f8f1032b3185090cba3e627e22423681ff1b62f12b07a0c870f5226233c736a2ef8d1c4f6
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -11,13 +11,13 @@ Configure your bucket. There are no default values for this, since we can't know
|
|
11
11
|
```ruby
|
12
12
|
ActionThrottling.configure do |config|
|
13
13
|
# The bucket_key is evaluated inside your application context
|
14
|
-
config.bucket_key = Proc.new { current_user.
|
14
|
+
config.bucket_key = Proc.new { current_user.id }
|
15
15
|
|
16
16
|
# Set the interval in which the bucket is regenerated
|
17
|
-
config.regenerate_interval =
|
17
|
+
config.regenerate_interval = Proc.new { current_user.regenerate_interval }
|
18
18
|
|
19
|
-
# Sets the number of tokens to be
|
20
|
-
config.regenerate_amount =
|
19
|
+
# Sets the number of tokens to be put back into the bucket
|
20
|
+
config.regenerate_amount = Proc.new { current_user.regenerate_amount }
|
21
21
|
|
22
22
|
# (optional) If you're not running on a completely vanilla redis connection,
|
23
23
|
# you can supply your own redis instance here.
|
@@ -33,7 +33,7 @@ def show
|
|
33
33
|
# Based on the configuration above, this will allow the user to call this
|
34
34
|
# endpoint 100 times every minute.
|
35
35
|
#
|
36
|
-
# This will call the `deduct(1)` method on the configured `config.
|
36
|
+
# This will call the `deduct(1)` method on the configured `config.bucket_key` (see
|
37
37
|
# above)
|
38
38
|
cost 1
|
39
39
|
# ...
|
@@ -44,7 +44,7 @@ If you have something like a create method, or some complex method that's costly
|
|
44
44
|
|
45
45
|
```ruby
|
46
46
|
def complex_method
|
47
|
-
# This will call `deduct(25)` on the `config.
|
47
|
+
# This will call `deduct(25)` on the `config.bucket_key` (see above)
|
48
48
|
cost 25
|
49
49
|
# ...
|
50
50
|
end
|
data/lib/action_throttling.rb
CHANGED
@@ -11,7 +11,7 @@ module ActionThrottling
|
|
11
11
|
def cost(price)
|
12
12
|
# If last time the bucket was called is older than the regeneration limit
|
13
13
|
# regenerate the bucket.
|
14
|
-
regenerate if
|
14
|
+
regenerate if expired?
|
15
15
|
|
16
16
|
# Deduct the price from the bucket. If it's below zero we raiss a
|
17
17
|
# HttpError::ToManyRequests exception/
|
@@ -20,17 +20,29 @@ module ActionThrottling
|
|
20
20
|
end
|
21
21
|
|
22
22
|
# Store when the cost was last called so we can calculate regeneration
|
23
|
-
|
23
|
+
update_call_time
|
24
24
|
end
|
25
25
|
|
26
26
|
private
|
27
27
|
|
28
|
+
def update_call_time
|
29
|
+
redis.set last_call_key, Time.now.httpdate
|
30
|
+
end
|
31
|
+
|
32
|
+
def expired?
|
33
|
+
last_called < regeneration_time
|
34
|
+
end
|
35
|
+
|
28
36
|
def last_call_key
|
29
37
|
"#{bucket_key}-last-call"
|
30
38
|
end
|
31
39
|
|
32
40
|
def regenerate
|
33
|
-
redis.set bucket_key,
|
41
|
+
redis.set bucket_key, regenerate_amount
|
42
|
+
end
|
43
|
+
|
44
|
+
def regenerate_amount
|
45
|
+
instance_eval &ActionThrottling.configuration.regenerate_amount
|
34
46
|
end
|
35
47
|
|
36
48
|
def last_called
|
@@ -47,7 +59,7 @@ module ActionThrottling
|
|
47
59
|
end
|
48
60
|
|
49
61
|
def regeneration_time
|
50
|
-
ActionThrottling.configuration.regenerate_interval.ago
|
62
|
+
instance_eval(&ActionThrottling.configuration.regenerate_interval).ago
|
51
63
|
end
|
52
64
|
|
53
65
|
def redis
|