speed_limiter 0.2.0 → 0.2.1
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/lib/speed_limiter/config.rb +4 -0
- data/lib/speed_limiter/throttle.rb +5 -7
- data/lib/speed_limiter/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: dcefe51efe099102f75381a902006faa8655f7207c7c971e0e0b0f356dc9c5e0
|
4
|
+
data.tar.gz: c3a3647ea48bb31a95dcd1bc70ddcb72c0402e4474c459e88271666021ba878f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bcfa6c8194738f83b7f688cc8211a79d10d8cee564a9b5099694c2730e71a1305c3d78dedad1b06654105d7da4b9070c9abb9d98aee97eca566eeaff32aed531
|
7
|
+
data.tar.gz: cb88e0be3f91a53919d019009c35cadad7e50a46e719f06405b13912f24f195030a06686e7d28241d30a26bdf516946b7f1ca37cb429473bda189815f85cc1c7
|
data/lib/speed_limiter/config.rb
CHANGED
@@ -22,6 +22,8 @@ module SpeedLimiter
|
|
22
22
|
end
|
23
23
|
attr_reader :config, :params, :block
|
24
24
|
|
25
|
+
delegate %i[redis_client] => :config
|
26
|
+
|
25
27
|
delegate %i[key redis_key limit period on_throttled create_state] => :params
|
26
28
|
|
27
29
|
# @yield [state]
|
@@ -58,7 +60,7 @@ module SpeedLimiter
|
|
58
60
|
return block.call(create_state) if config.no_limit?
|
59
61
|
|
60
62
|
loop do
|
61
|
-
count, ttl =
|
63
|
+
count, ttl = redis_client.increment(redis_key, period)
|
62
64
|
|
63
65
|
break(block.call(create_state(count: count, ttl: ttl))) if count <= limit
|
64
66
|
|
@@ -67,20 +69,16 @@ module SpeedLimiter
|
|
67
69
|
end
|
68
70
|
|
69
71
|
def wait_for_interval(count)
|
70
|
-
ttl =
|
72
|
+
ttl = redis_client.ttl(redis_key)
|
71
73
|
return if ttl.negative?
|
72
74
|
|
73
75
|
config.on_throttled.call(create_state(count: count, ttl: ttl)) if config.on_throttled.respond_to?(:call)
|
74
76
|
on_throttled.call(create_state(count: count, ttl: ttl)) if on_throttled.respond_to?(:call)
|
75
77
|
|
76
|
-
ttl =
|
78
|
+
ttl = redis_client.ttl(redis_key)
|
77
79
|
return if ttl.negative?
|
78
80
|
|
79
81
|
sleep ttl
|
80
82
|
end
|
81
|
-
|
82
|
-
def redis
|
83
|
-
@redis ||= SpeedLimiter::Redis.new(config.redis || ::Redis.new(url: config.redis_url))
|
84
|
-
end
|
85
83
|
end
|
86
84
|
end
|