Module: SpeedLimiter

Defined in:
lib/speed_limiter.rb,
lib/speed_limiter/redis.rb,
lib/speed_limiter/state.rb,
lib/speed_limiter/config.rb,
lib/speed_limiter/version.rb,
lib/speed_limiter/throttle.rb,
lib/speed_limiter/throttle_params.rb

Overview

Call speed limiter

Defined Under Namespace

Classes: Config, Redis, State, Throttle, ThrottleParams

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.configObject



11
12
13
# File 'lib/speed_limiter.rb', line 11

def config
  @config ||= Config.new
end

.configure {|config| ... } ⇒ Object

Yields:



15
16
17
# File 'lib/speed_limiter.rb', line 15

def configure
  yield(config)
end

.redisObject



19
20
21
# File 'lib/speed_limiter.rb', line 19

def redis
  @redis ||= config.redis || Redis.new(url: config.redis_url)
end

.throttle(key, **params) {|count| ... } ⇒ Object

Parameters:

  • key (String)

    key name

  • limit (Integer)

    limit count per period

  • period (Integer)

    period time (seconds)

  • on_throttled (Proc)

    Block called when limit exceeded, with ttl(Float) and key as argument

Yields:

  • (count)

    Block called to not reach limit

Yield Parameters:

  • count (Integer)

    count of period

Yield Returns:

  • (any)

    block return value



30
31
32
# File 'lib/speed_limiter.rb', line 30

def throttle(key, **params, &block)
  Throttle.new(config: config, key: key, **params, &block).throttle
end