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,
lib/speed_limiter/errors/throttled_error.rb,
lib/speed_limiter/errors/limit_exceeded_error.rb

Overview

Call speed limiter

Defined Under Namespace

Modules: Errors Classes: Config, Redis, State, Throttle, ThrottleParams

Constant Summary collapse

VERSION =
"0.3.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

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

Return value of block if argument contains block, otherwise Throttle instance

Parameters:

  • key (String, #to_s)

    Throttle key name

Options Hash (**params):

  • :limit (Integer)

    limit count per period

  • :period (Integer)

    period time (seconds)

  • :on_throttled (Proc, #call)

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

  • :raise_on_throttled (true, Class)

    Raise error when limit exceeded. If Class is given, it will be raised instead of SpeedLimiter::ThrottledError. If you want to specify a custom error class, please specify a class that inherits from SpeedLimiter::LimitExceededError or a class that accepts SpeedLimiter::State as an argument.

  • :retry (true, Hash)

    Retry options. (see Retryable.retryable for details)

Yields:

  • (state)

Yield Parameters:

Returns:

  • Return value of block if argument contains block, otherwise Throttle instance



24
25
26
27
28
29
30
31
32
# File 'lib/speed_limiter.rb', line 24

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

  if block
    throttle.call(&block)
  else
    throttle
  end
end