Skip to content

Instantly share code, notes, and snippets.

@romanbsd
Created May 27, 2011 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romanbsd/995458 to your computer and use it in GitHub Desktop.
Save romanbsd/995458 to your computer and use it in GitHub Desktop.
Retry HTTP request if connection was reset
require 'net/http'
class Net::HTTP
def transport_request_with_retry(req)
# First attempt will be after 100ms
delay = 0.1
begin
transport_request_without_retry(req)
rescue Errno::ECONNRESET => e
if delay < 4
Kernel.sleep(delay)
# Subsequent attempts will be delayed exponentially
delay *= 2
retry
else
raise e
end
end
end
alias_method_chain :transport_request, :retry
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment