Module: HTTP
- Defined in:
- lib/HTTP/VERSION.rb,
lib/HTTP/RETRY.rb,
lib/HTTP/verbs.rb,
lib/HTTP/request.rb
Overview
HTTP/VERSION.rb HTTP::VERSION
Defined Under Namespace
Constant Summary collapse
- VERSION =
'1.0.0'
Class Method Summary collapse
- .backoff_delay(base, attempt) ⇒ Object
- .request(uri, request_object, headers = {}, options = {}, &block) ⇒ Object
- .retry_after(response) ⇒ Object
- .retry_config(options) ⇒ Object
- .with_retries(http, request_object, config) ⇒ Object
Class Method Details
.backoff_delay(base, attempt) ⇒ Object
60 61 62 |
# File 'lib/HTTP/RETRY.rb', line 60 def backoff_delay(base, attempt) base * (2 ** (attempt - 1)) * (1 + (rand - 0.5) * 0.4) end |
.request(uri, request_object, headers = {}, options = {}, &block) ⇒ Object
15 16 17 18 19 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 |
# File 'lib/HTTP/request.rb', line 15 def request(uri, request_object, headers = {}, = {}, &block) uri = uri.is_a?(URI) ? uri : URI.parse(uri) http = Net::HTTP.new(uri.host, uri.port) no_redirect = .delete(:no_redirect) config = retry_config() http. = .merge( use_ssl: ([:use_ssl] || uri.use_ssl?), verify_mode: ([:verify_mode] || OpenSSL::SSL::VERIFY_PEER) ) request_object.headers = headers request_object.basic_auth(uri.user, uri.password) if uri.user verb = request_object.method.downcase.to_sym response = ( if config[:retries] > 0 && config[:verbs].include?(verb) with_retries(http, request_object, config) else http.request(request_object) end ) if response.code =~ /^3/ if block_given? && no_redirect yield response elsif no_redirect return response end redirect_uri = uri.merge(response['location']) if response.code =~ /^30[78]$/ data = VERBS::WITH_BODY.include?(verb) ? request_object.body : {} response = send(verb, redirect_uri.to_s, data, headers, , &block) else response = get(redirect_uri.to_s, {}, {}, , &block) end end if block_given? yield response else response end end |
.retry_after(response) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/HTTP/RETRY.rb', line 65 def retry_after(response) header = response['Retry-After'] return nil unless header if header =~ /\A\d+\z/ header.to_i else # Malformed HTTP-date — fall through to caller's backoff. delta = Time.httpdate(header) - Time.now rescue nil delta && [delta, 0].max end end |
.retry_config(options) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/HTTP/RETRY.rb', line 29 def retry_config() { retries: .delete(:retries) || 0, delay: .delete(:retry_delay) || 1.0, status_codes: .delete(:retry_status_codes) || RETRY::STATUS_CODES, exceptions: .delete(:retry_exceptions) || RETRY::EXCEPTIONS, verbs: .delete(:retry_verbs) || RETRY::VERBS } end |
.with_retries(http, request_object, config) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/HTTP/RETRY.rb', line 40 def with_retries(http, request_object, config) attempt = 0 loop do begin response = http.request(request_object) if config[:status_codes].include?(response.code.to_i) && attempt < config[:retries] attempt += 1 RETRY.sleep(retry_after(response) || backoff_delay(config[:delay], attempt)) next end return response rescue *config[:exceptions] raise unless attempt < config[:retries] attempt += 1 RETRY.sleep(backoff_delay(config[:delay], attempt)) end end end |