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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b360a3c2df8c37b8ac47eec08222e784ef475966c5938c93f1206e56c2ec353d
4
- data.tar.gz: 34a890cdbdf74a0fcb25b2140c124ec6f6487ebf2aec4115084fabbc2a437ddd
3
+ metadata.gz: dcefe51efe099102f75381a902006faa8655f7207c7c971e0e0b0f356dc9c5e0
4
+ data.tar.gz: c3a3647ea48bb31a95dcd1bc70ddcb72c0402e4474c459e88271666021ba878f
5
5
  SHA512:
6
- metadata.gz: 0ed1f8c6f934e2955b5fd398f6cead553eff69e3e93cb2816a6a3cb41f61fe0848980b0fe1310d17f531ce8f4266de80b0599b7416d1c4d4d8bbda9a61f92c43
7
- data.tar.gz: 6ef932d30fc72b180b2e968b3f57e75621b15d36b1afc33488cd80a2fa1e0c26802650db1902f97fac29c4294905f12abe17fdb1c99b7200772f77f7c5bab89e
6
+ metadata.gz: bcfa6c8194738f83b7f688cc8211a79d10d8cee564a9b5099694c2730e71a1305c3d78dedad1b06654105d7da4b9070c9abb9d98aee97eca566eeaff32aed531
7
+ data.tar.gz: cb88e0be3f91a53919d019009c35cadad7e50a46e719f06405b13912f24f195030a06686e7d28241d30a26bdf516946b7f1ca37cb429473bda189815f85cc1c7
@@ -14,5 +14,9 @@ module SpeedLimiter
14
14
  end
15
15
 
16
16
  alias no_limit? no_limit
17
+
18
+ def redis_client
19
+ @redis_client ||= SpeedLimiter::Redis.new(@redis || ::Redis.new(url: redis_url))
20
+ end
17
21
  end
18
22
  end
@@ -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 = redis.increment(redis_key, period)
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 = redis.ttl(redis_key)
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 = redis.ttl(redis_key)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SpeedLimiter
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: speed_limiter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - yuhei mukoyama