Class: SpeedLimiter::ThrottleParams

Inherits:
Object
  • Object
show all
Defined in:
lib/speed_limiter/throttle_params.rb

Overview

Throttle params model

Constant Summary collapse

KNOWN_OPTIONS =
%i[on_throttled retry raise_on_throttled].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, key:, limit:, period:, **options) ⇒ ThrottleParams

Returns a new instance of ThrottleParams.

Parameters:

  • config (SpeedLimiter::Config)
  • key (String)
  • limit (Integer)

    limit count per period

  • period (Integer)

    period time (seconds)

  • options (Hash)

    options

Options Hash (**options):

  • :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)

Raises:

  • (ArgumentError)


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

def initialize(config:, key:, limit:, period:, **options)
  @config = config
  @key = key
  @limit = limit
  @period = period
  @options = options

  return unless (unknown_options = options.keys - KNOWN_OPTIONS).any?

  raise ArgumentError, "Unknown options: #{unknown_options.join(', ')}"
end

Instance Attribute Details

#configSpeedLimiter::Config (readonly)



42
43
44
# File 'lib/speed_limiter/throttle_params.rb', line 42

def config
  @config
end

#keyString (readonly)

Returns Throttle key name.

Returns:

  • (String)

    Throttle key name



42
# File 'lib/speed_limiter/throttle_params.rb', line 42

attr_reader :config, :key, :limit, :period

#limitInteger (readonly)

Returns limit count per period.

Returns:

  • (Integer)

    limit count per period



42
# File 'lib/speed_limiter/throttle_params.rb', line 42

attr_reader :config, :key, :limit, :period

#periodObject (readonly)

Returns the value of attribute period.



42
# File 'lib/speed_limiter/throttle_params.rb', line 42

attr_reader :config, :key, :limit, :period

Instance Method Details

#create_state(count: nil, ttl: nil) ⇒ SpeedLimiter::State

Parameters:

  • count (Integer, nil) (defaults to: nil)
  • ttl (Float, nil) (defaults to: nil)

Returns:



80
81
82
# File 'lib/speed_limiter/throttle_params.rb', line 80

def create_state(count: nil, ttl: nil)
  State.new(params: self, count: count, ttl: ttl)
end

#on_throttledObject



44
45
46
# File 'lib/speed_limiter/throttle_params.rb', line 44

def on_throttled
  @options[:on_throttled]
end

#raise_on_throttledBoolean, Class

Returns:

  • (Boolean, Class)


49
50
51
# File 'lib/speed_limiter/throttle_params.rb', line 49

def raise_on_throttled
  @options[:raise_on_throttled]
end

#raise_on_throttled?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/speed_limiter/throttle_params.rb', line 54

def raise_on_throttled?
  !!raise_on_throttled
end

#raise_on_throttled_classClass

Returns:

  • (Class)


59
60
61
62
63
64
65
# File 'lib/speed_limiter/throttle_params.rb', line 59

def raise_on_throttled_class
  if raise_on_throttled.is_a?(Class)
    raise_on_throttled
  else
    SpeedLimiter::Errors::ThrottledError
  end
end

#redis_keyString

Returns:

  • (String)


73
74
75
# File 'lib/speed_limiter/throttle_params.rb', line 73

def redis_key
  "#{config.prefix}:#{key}"
end

#retryBoolean, Hash

Returns:

  • (Boolean, Hash)


68
69
70
# File 'lib/speed_limiter/throttle_params.rb', line 68

def retry
  @options[:retry]
end