Class: ThrottleMachines::RackMiddleware::Throttle
- Inherits:
-
Object
- Object
- ThrottleMachines::RackMiddleware::Throttle
- Defined in:
- lib/throttle_machines/rack_middleware/throttle.rb
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
readonly
Returns the value of attribute algorithm.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#period ⇒ Object
readonly
Returns the value of attribute period.
Instance Method Summary collapse
-
#initialize(name, options, &block) ⇒ Throttle
constructor
A new instance of Throttle.
- #matched_by?(request) ⇒ Boolean
Constructor Details
#initialize(name, options, &block) ⇒ Throttle
Returns a new instance of Throttle.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/throttle_machines/rack_middleware/throttle.rb', line 8 def initialize(name, , &block) @name = name @block = block raise ArgumentError, 'Must pass :limit option' unless [:limit] raise ArgumentError, 'Must pass :period option' unless [:period] @limit = [:limit] @period = [:period].to_i @algorithm = [:algorithm] || :fixed_window end |
Instance Attribute Details
#algorithm ⇒ Object (readonly)
Returns the value of attribute algorithm.
6 7 8 |
# File 'lib/throttle_machines/rack_middleware/throttle.rb', line 6 def algorithm @algorithm end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
6 7 8 |
# File 'lib/throttle_machines/rack_middleware/throttle.rb', line 6 def block @block end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
6 7 8 |
# File 'lib/throttle_machines/rack_middleware/throttle.rb', line 6 def limit @limit end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/throttle_machines/rack_middleware/throttle.rb', line 6 def name @name end |
#period ⇒ Object (readonly)
Returns the value of attribute period.
6 7 8 |
# File 'lib/throttle_machines/rack_middleware/throttle.rb', line 6 def period @period end |
Instance Method Details
#matched_by?(request) ⇒ Boolean
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/throttle_machines/rack_middleware/throttle.rb', line 20 def matched_by?(request) discriminator = discriminator_for(request) return false unless discriminator key = "#{@name}:#{discriminator}" current_limit = limit_for(request) current_period = period_for(request) # Use ThrottleMachines limiter limiter = ThrottleMachines.limiter( key, limit: current_limit, period: current_period, algorithm: @algorithm ) # Try to consume the request atomically throttled = false begin # This will either succeed and consume a request, or raise ThrottledError limiter.throttle! # If we get here, request was allowed rescue ThrottledError # Request was throttled throttled = true end # Get current state for instrumentation data = { discriminator: discriminator, count: current_limit - limiter.remaining, period: current_period, limit: current_limit, retry_after: limiter.retry_after } annotate_request_with_throttle_data(request, data) if throttled annotate_request_with_matched_data(request, data) ThrottleMachines::RackMiddleware.instrument(request) end throttled end |