Class: SpeedLimiter::Throttle

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/speed_limiter/throttle.rb

Overview

with actual throttle limits

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, config:, **params) ⇒ Throttle

Returns a new instance of Throttle.

Parameters:

  • key (String, #to_s)

    Throttle key name

  • params (Hash)

    a customizable set of options

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)



21
22
23
24
25
26
# File 'lib/speed_limiter/throttle.rb', line 21

def initialize(key, config:, **params)
  params[:key] = key.to_s

  @config = config
  @params = ThrottleParams.new(config: config, **params)
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



27
28
29
# File 'lib/speed_limiter/throttle.rb', line 27

def block
  @block
end

#configObject (readonly)

Returns the value of attribute config.



27
28
29
# File 'lib/speed_limiter/throttle.rb', line 27

def config
  @config
end

#paramsObject (readonly)

Returns the value of attribute params.



27
28
29
# File 'lib/speed_limiter/throttle.rb', line 27

def params
  @params
end

Instance Method Details

#call {|state| ... } ⇒ any

Returns block return value.

Yields:

  • (state)

Yield Parameters:

Returns:

  • (any)

    block return value



38
39
40
41
42
43
44
# File 'lib/speed_limiter/throttle.rb', line 38

def call(&block)
  if use_retryable?
    Retryable.retryable(**retryable_options) { run_block(&block) }
  else
    run_block(&block)
  end
end