Exception: Pecorino::Throttle::Throttled
- Inherits:
-
StandardError
- Object
- StandardError
- Pecorino::Throttle::Throttled
- Defined in:
- lib/pecorino/throttle.rb
Overview
Pecorino::Throttle will raise this exception from ‘request!`. The exception can be used to do matching, for setting appropriate response headers, and for distinguishing between multiple different throttles.
Instance Attribute Summary collapse
-
#state ⇒ Throttle::State
readonly
Returns the throttle state based on which the exception is getting raised.
-
#throttle ⇒ Throttle
readonly
Returns the throttle which raised the exception.
Instance Method Summary collapse
-
#initialize(from_throttle, state) ⇒ Throttled
constructor
A new instance of Throttled.
-
#retry_after ⇒ Integer
Returns the ‘retry_after` value in seconds, suitable for use in an HTTP header.
Constructor Details
#initialize(from_throttle, state) ⇒ Throttled
Returns a new instance of Throttled.
78 79 80 81 82 |
# File 'lib/pecorino/throttle.rb', line 78 def initialize(from_throttle, state) @throttle = from_throttle @state = state super("Block in effect until #{state.blocked_until.iso8601}") end |
Instance Attribute Details
#state ⇒ Throttle::State (readonly)
Returns the throttle state based on which the exception is getting raised. This can be used for caching the exception, because the state can tell when the block will be lifted. This can be used to shift the throttle verification into a faster layer of the system (like a blocklist in a firewall) or caching the state in an upstream cache. A block in Pecorino is set once and is active until expiry. If your service is under an attack and you know that the call is blocked until a certain future time, the block can be lifted up into a faster/cheaper storage destination, like Rails cache:
76 77 78 |
# File 'lib/pecorino/throttle.rb', line 76 def state @state end |
#throttle ⇒ Throttle (readonly)
Returns the throttle which raised the exception. Can be used to disambiguiate between multiple Throttled exceptions when multiple throttles are applied in a layered fashion:
46 47 48 |
# File 'lib/pecorino/throttle.rb', line 46 def throttle @throttle end |
Instance Method Details
#retry_after ⇒ Integer
Returns the ‘retry_after` value in seconds, suitable for use in an HTTP header
87 88 89 |
# File 'lib/pecorino/throttle.rb', line 87 def retry_after (@state.blocked_until - Time.now).ceil end |