ruby net/http opening connection very slow
It was an issue with DNS server. Switched to google public DNS (8.8.8.8, 8.8.4.4). Thanks @Ineu.
Net::HTTP request connection too slow?
This was solved by a date resynchro. Since all servers' hours are the same, it works like a charm.
How can I handle Connection timed out error in ruby Net/HTTP?
The Timeout::Error is raised by the Net::HTTP.connect
method that is executed by start
, not by request
.
It means that in order to rescue the timeout, the whole Net::HTTP
call should be inside the begin block.
begin
Net::HTTP.new('example.com', nil, '140.113.182.81', '808').start do |http|
response = http.request
p response
end
rescue Timeout::Error
p 'timed out'
end
Increase connect(2) timeout in RestClient / Net::HTTP on AWS Linux
First of all, you could just increase the tcp_syn_retries
configuration updating the /proc/sys/net/ipv4/tcp_syn_retries
file. Reference here.
If if doesn't work, I think you will need to activate the SO_KEEPALIVE
or TCP_USER_TIMEOUT
options. But probably there is no interface for that in rest-client
.
So maybe you'll need to make a fork or create the Socket
and Socket::Option
by yourself.
Mike Perham wrote about it in his blog.
Net::HTTP extremely slow responses for HTTPS requests
The issue was not Ruby or OpenSSL or any of the above libraries. The problem is that IPv6 addresses were not resolving on my MacBook. DNS lookup returned the IPv6 address first, so the libraries tried to connect to that until it timed out and then they connected to the IPv4 addresses which worked just fine.
Disabling IPv6 for OS X Yosemite 10.10.2 worked for me. It's not ideal, but until I can determine another solution, it works.
networksetup -setv6off "Wi-Fi"
Thanks @SteffenUllrich for pointing me in that direction.
Related Topics
How to Prevent Rails Controller Generator to Modify Config/Routes.Rb
Using Fork in Windows with Ruby
How Is Ruby Tcpsocket Timeout Defined
How to Render a Layout Directly from Routes.Rb, Without a Controller
Instance_Eval's Block Argument(S)- Documented? Purpose
Ruby Installation (2.2.2) Fails in MACos Big Sur
Ruby Minitest Assert_Output Syntax
How to Define a Method in Ruby Using Splat and an Optional Hash at the Same Time
What's the Nature of "Property" in a Ruby Class
Remote Form_Tag in Rails3 Without a Named Route
Interface Builder Not Seeing Outlets with MACruby
Does It Matter If a Conditional Statement Comes Before or After the Expression
How to Mock an Instance Method of an Already Mocked Object
Cocoapods Folder Does Not Exist/Permissions Error