Class: SpeedLimiter::ThrottleParams
- Inherits:
-
Object
- Object
- SpeedLimiter::ThrottleParams
- 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
- #config ⇒ SpeedLimiter::Config readonly
-
#key ⇒ String
readonly
Throttle key name.
-
#limit ⇒ Integer
readonly
Limit count per period.
-
#period ⇒ Object
readonly
Returns the value of attribute period.
Instance Method Summary collapse
- #create_state(count: nil, ttl: nil) ⇒ SpeedLimiter::State
-
#initialize(config:, key:, limit:, period:, **options) ⇒ ThrottleParams
constructor
A new instance of ThrottleParams.
- #on_throttled ⇒ Object
- #raise_on_throttled ⇒ Boolean, Class
- #raise_on_throttled? ⇒ Boolean
- #raise_on_throttled_class ⇒ Class
- #redis_key ⇒ String
- #retry ⇒ Boolean, Hash
Constructor Details
#initialize(config:, key:, limit:, period:, **options) ⇒ ThrottleParams
Returns a new instance of ThrottleParams.
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:, **) @config = config @key = key @limit = limit @period = period @options = return unless ( = .keys - KNOWN_OPTIONS).any? raise ArgumentError, "Unknown options: #{.join(', ')}" end |
Instance Attribute Details
#config ⇒ SpeedLimiter::Config (readonly)
42 43 44 |
# File 'lib/speed_limiter/throttle_params.rb', line 42 def config @config end |
#key ⇒ String (readonly)
Returns Throttle key name.
42 |
# File 'lib/speed_limiter/throttle_params.rb', line 42 attr_reader :config, :key, :limit, :period |
#limit ⇒ Integer (readonly)
Returns limit count per period.
42 |
# File 'lib/speed_limiter/throttle_params.rb', line 42 attr_reader :config, :key, :limit, :period |
#period ⇒ Object (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
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_throttled ⇒ Object
44 45 46 |
# File 'lib/speed_limiter/throttle_params.rb', line 44 def on_throttled @options[:on_throttled] end |
#raise_on_throttled ⇒ 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
54 55 56 |
# File 'lib/speed_limiter/throttle_params.rb', line 54 def raise_on_throttled? !!raise_on_throttled end |
#raise_on_throttled_class ⇒ 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_key ⇒ String
73 74 75 |
# File 'lib/speed_limiter/throttle_params.rb', line 73 def redis_key "#{config.prefix}:#{key}" end |
#retry ⇒ Boolean, Hash
68 69 70 |
# File 'lib/speed_limiter/throttle_params.rb', line 68 def retry @options[:retry] end |