Created
January 7, 2019 18:50
-
-
Save raj454raj/5c80cdf0f0ebcd99436aac2f412faaa9 to your computer and use it in GitHub Desktop.
Show net/http module retries if the first one timed out over typhoeus
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
require 'net/http' | |
require 'uri' | |
require 'typhoeus' | |
def get_response(url, request_timeout) | |
# Raises exception on timeout | |
url = URI(url) | |
http = Net::HTTP.new(url.host, url.port) | |
request = Net::HTTP::Get.new url | |
http.read_timeout = request_timeout | |
http.request request | |
end | |
def get_response_typhoeus(url, request_timeout) | |
# Does not raise an exception but returns an object which tells us if the request timedout | |
response = Typhoeus.get(url, timeout: request_timeout) | |
puts "Request timeout out ? - #{response.timed_out?}" | |
return response | |
end | |
local_dummy_server_url = "http://localhost:4569/" | |
timeout = 5 | |
response1 = get_response(local_dummy_server_url, timeout) | |
puts response1.body | |
response2 = get_response_typhoeus(local_dummy_server_url, timeout) | |
puts response2.body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use this as the dummy server - https://gist.github.com/raj454raj/2dd897b5b23d15b3d6ef7484091e2cf7