-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add .retriable feature to Http - Rebased (#775)
* Delay by default backs backs off over time * Maximum delay time * Exceptions to retry from * Status codes to retry from * Custom retry logic * Respect Retry-After header if present * on_retry callback --------- Co-authored-by: Alexey Zapparov <[email protected]> Co-authored-by: Bert Goethals <[email protected]>
- Loading branch information
1 parent
cb13273
commit d6313d7
Showing
12 changed files
with
731 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
require "http/retriable/performer" | ||
|
||
module HTTP | ||
module Retriable | ||
# Retriable version of HTTP::Client. | ||
# | ||
# @see http://www.rubydoc.info/gems/http/HTTP/Client | ||
class Client < HTTP::Client | ||
# @param [Performer] performer | ||
# @param [HTTP::Options, Hash] options | ||
def initialize(performer, options) | ||
@performer = performer | ||
super(options) | ||
end | ||
|
||
# Overriden version of `HTTP::Client#make_request`. | ||
# | ||
# Monitors request/response phase with performer. | ||
# | ||
# @see http://www.rubydoc.info/gems/http/HTTP/Client:perform | ||
def perform(req, options) | ||
@performer.perform(self, req) { super(req, options) } | ||
end | ||
|
||
private | ||
|
||
# Overriden version of `HTTP::Chainable#branch`. | ||
# | ||
# @return [HTTP::Retriable::Client] | ||
def branch(options) | ||
Retriable::Client.new(@performer, options) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
module HTTP | ||
module Retriable | ||
# @api private | ||
class DelayCalculator | ||
def initialize(opts) | ||
@max_delay = opts.fetch(:max_delay, Float::MAX).to_f | ||
if (delay = opts[:delay]).respond_to?(:call) | ||
@delay_proc = opts.fetch(:delay) | ||
else | ||
@delay = delay | ||
end | ||
end | ||
|
||
def call(iteration, response) | ||
delay = if response && (retry_header = response.headers["Retry-After"]) | ||
delay_from_retry_header(retry_header) | ||
else | ||
calculate_delay_from_iteration(iteration) | ||
end | ||
|
||
ensure_dealy_in_bounds(delay) | ||
end | ||
|
||
RFC2822_DATE_REGEX = /^ | ||
(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat),\s+ | ||
(?:0[1-9]|[1-2]?[0-9]|3[01])\s+ | ||
(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+ | ||
(?:19[0-9]{2}|[2-9][0-9]{3})\s+ | ||
(?:2[0-3]|[0-1][0-9]):(?:[0-5][0-9]):(?:60|[0-5][0-9])\s+ | ||
GMT | ||
$/x | ||
|
||
# Spec for Retry-After header | ||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After | ||
def delay_from_retry_header(value) | ||
value = value.to_s.strip | ||
|
||
case value | ||
when RFC2822_DATE_REGEX then DateTime.rfc2822(value).to_time - Time.now.utc | ||
when /^\d+$/ then value.to_i | ||
else 0 | ||
end | ||
end | ||
|
||
def calculate_delay_from_iteration(iteration) | ||
if @delay_proc | ||
@delay_proc.call(iteration) | ||
elsif @delay | ||
@delay | ||
else | ||
delay = (2**(iteration - 1)) - 1 | ||
delay_noise = rand | ||
delay + delay_noise | ||
end | ||
end | ||
|
||
def ensure_dealy_in_bounds(delay) | ||
delay.clamp(0, @max_delay) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module HTTP | ||
# Retriable performance ran out of attempts | ||
class OutOfRetriesError < Error | ||
attr_accessor :response | ||
|
||
attr_writer :cause | ||
|
||
def cause | ||
@cause || super | ||
end | ||
end | ||
end |
Oops, something went wrong.