Skip to content

Instantly share code, notes, and snippets.

@pinpox
Created April 10, 2015 15:21
Show Gist options
  • Save pinpox/baf24c13d8c777150dc0 to your computer and use it in GitHub Desktop.
Save pinpox/baf24c13d8c777150dc0 to your computer and use it in GitHub Desktop.
Ruby mechanize excerpt
def request uri, req = nil, &block
retried = false
bad_response = false
req = request_setup req || uri
connection = connection_for uri
connection_id = connection.object_id
begin
Thread.current[@request_key][connection_id] += 1
response = connection.request req, &block
if connection_close?(req) or
(response.http_version <= '1.0' and
not connection_keep_alive?(response)) or
connection_close?(response) then
connection.finish
end
rescue Net::HTTPBadResponse => e
message = error_message connection
finish connection
raise Error, "too many bad responses #{message}" if
bad_response or not can_retry? req
bad_response = true
retry
rescue *RETRIED_EXCEPTIONS => e # retried on ruby 2
request_failed e, req, connection if
retried or not can_retry? req, @retried_on_ruby_2
reset connection
retried = true
retry
rescue Errno::EINVAL, Errno::ETIMEDOUT => e # not retried on ruby 2
request_failed e, req, connection if retried or not can_retry? req
reset connection
retried = true
retry
rescue Exception => e
finish connection
raise
ensure
Thread.current[@timeout_key][connection_id] = Time.now
end
@http_versions["#{uri.host}:#{uri.port}"] ||= response.http_version
response
end
@pinpox
Copy link
Author

pinpox commented Apr 10, 2015

Zeile 50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment