Module: SpeedLimiter
- Defined in:
- lib/speed_limiter.rb,
lib/speed_limiter/config.rb,
lib/speed_limiter/version.rb
Overview
Call speed limiter
Defined Under Namespace
Classes: Config
Constant Summary collapse
- VERSION =
"0.0.1"
Class Method Summary collapse
- .config ⇒ Object
- .configure {|config| ... } ⇒ Object
- .redis ⇒ Object
- .throttle(key, limit:, period:, &block) ⇒ Object
Class Method Details
.config ⇒ Object
10 11 12 |
# File 'lib/speed_limiter.rb', line 10 def config @config ||= Config.new end |
.configure {|config| ... } ⇒ Object
14 15 16 |
# File 'lib/speed_limiter.rb', line 14 def configure yield(config) end |
.redis ⇒ Object
18 19 20 |
# File 'lib/speed_limiter.rb', line 18 def redis @redis ||= config.redis || Redis.new(url: config.redis_url) end |
.throttle(key, limit:, period:, &block) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/speed_limiter.rb', line 22 def throttle(key, limit:, period:, &block) return block&.call if config.no_limit? key_name = "#{config.prefix}:#{key}" loop do count = increment(key_name, period) break(block&.call) if count <= limit wait_for_interval(key_name) end end |