Class: Rack::Idempotent::ExponentialBackoff
- Inherits:
-
Object
- Object
- Rack::Idempotent::ExponentialBackoff
- Defined in:
- lib/rack-idempotent/exponential_backoff.rb
Instance Attribute Summary collapse
-
#max_retries ⇒ Object
readonly
Returns the value of attribute max_retries.
-
#max_retry_interval ⇒ Object
readonly
Returns the value of attribute max_retry_interval.
-
#min_retry_interval ⇒ Object
readonly
Returns the value of attribute min_retry_interval.
Instance Method Summary collapse
- #call(request, response, exception) ⇒ Object
- #delay(secs) ⇒ Object
-
#initialize(options = {}) ⇒ ExponentialBackoff
constructor
A new instance of ExponentialBackoff.
Constructor Details
#initialize(options = {}) ⇒ ExponentialBackoff
Returns a new instance of ExponentialBackoff.
4 5 6 7 8 |
# File 'lib/rack-idempotent/exponential_backoff.rb', line 4 def initialize(={}) @max_retries = [:max_retries] || Rack::Idempotent::DEFAULT_RETRY_LIMIT @min_retry_interval = [:min_retry_interval] || 0.5 @max_retry_interval = [:max_retry_interval] || 1800 end |
Instance Attribute Details
#max_retries ⇒ Object (readonly)
Returns the value of attribute max_retries.
2 3 4 |
# File 'lib/rack-idempotent/exponential_backoff.rb', line 2 def max_retries @max_retries end |
#max_retry_interval ⇒ Object (readonly)
Returns the value of attribute max_retry_interval.
2 3 4 |
# File 'lib/rack-idempotent/exponential_backoff.rb', line 2 def max_retry_interval @max_retry_interval end |
#min_retry_interval ⇒ Object (readonly)
Returns the value of attribute min_retry_interval.
2 3 4 |
# File 'lib/rack-idempotent/exponential_backoff.rb', line 2 def min_retry_interval @min_retry_interval end |
Instance Method Details
#call(request, response, exception) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/rack-idempotent/exponential_backoff.rb', line 10 def call(request, response, exception) request.env["idempotent.requests.count"] ||= 0 request.env["idempotent.requests.count"] += 1 request.env["idempotent.requests.sleep"] ||= (@min_retry_interval / 2) request.env["idempotent.requests.sleep"] *= 2 if request.env["idempotent.requests.sleep"] > @max_retry_interval request.env["idempotent.requests.sleep"] = @max_retry_interval end if request.env["idempotent.requests.count"] >= @max_retries raise Rack::Idempotent::RetryLimitExceeded.new( request.env["idempotent.requests.exceptions"] ) end delay(request.env["idempotent.requests.sleep"]) end |
#delay(secs) ⇒ Object
27 28 29 |
# File 'lib/rack-idempotent/exponential_backoff.rb', line 27 def delay(secs) sleep(secs) end |