How to specify a read timeout for a Net::HTTP::Post.new request in Ruby 2
Solved via this stackoverflow answer
I've changed my
response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
line to be
response = Net::HTTP.start(url.host, url.port, :read_timeout => 500) {|http| http.request(request)}
and this seems to have got around this problem.
How to set timeout for Net::HTTP.start?
As Ascar pointed out, this is wrong syntax.
But :read_timeout alone doesnt fix the issue, I also needed to add :open_timeout if the host didnt respond at all.
Net::HTTP.start(uri.host, uri.port, {read_timeout: 5, open_timeout: 5})
Set custom timeout in Net::HTTP::Get.new with Rails
You need to set the read_timeout attribute.
link = URI.parse(url)
request = Net::HTTP::Get.new(link.path)
begin
response = Net::HTTP.start(link.host, link.port) {|http|
http.read_timeout = 100 #Default is 60 seconds
http.request(request)
}
rescue Net::ReadTimeout => e
puts e.message
end
Change default timeout
The default timeout of an HttpClient
is 100 seconds.
HttpClient Timeout
You can adjust to your HttpClient
and set a custom timeout duration inside of your HttpService
.
httpClient.Timeout = 5000;
HttpClient Request Timeout
You could alternatively define a timeout via a cancellation token CancellationTokenSource
using (var cts = new CancellationTokenSource(new TimeSpan(0, 0, 5))
{
await httpClient.GetAsync(url, cts.Token).ConfigureAwait(false);
}
A few notes:
- Making changes inside of the
HttpClient
will affect all requests. If you want to make it per request you will need to pass through your desired timeout duration as a parameter. - Passing an instance of
CancellationTokenSource
will work if it's timeout is lower thanTimeout
set by theHttpClient
andHttpClient
's timeout is not infinite. Otherwise, theHttpClient
's timeout will take place.
Set timeout for HTTPClient get() request
There are two different ways to configure this behavior in Dart
Set a per request timeout
You can set a timeout on any Future using the Future.timeout
method. This will short-circuit after the given duration has elapsed by throwing a TimeoutException
.
try {
final request = await client.get(...);
final response = await request.close()
.timeout(const Duration(seconds: 2));
// rest of the code
...
} on TimeoutException catch (_) {
// A timeout occurred.
} on SocketException catch (_) {
// Other exception
}
Set a timeout on HttpClient
You can also set a timeout on the HttpClient itself using HttpClient.connectionTimeout
. This will apply to all requests made by the same client, after the timeout was set. When a request exceeds this timeout, a SocketException
is thrown.
final client = new HttpClient();
client.connectionTimeout = const Duration(seconds: 5);
Ruby Net::HTTP second request when timeout
It's a feature of Net::HTTP
that it retries idempotent requests. You can limit number of retries by setting max_retries
(in your case to 0).
More on that issue on Ruby redmine
require "net/http"
http = Net::HTTP.new("timeout.free.beeceptor.com", 443)
http.read_timeout = 1
http.max_retries = 0 # <<<<<<<< the change
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.request(Net::HTTP::Get.new("/Time"))
Related Topics
Iterate Through Array of Hashes in Ruby
How to Set the Mechanize Page Encoding
Rvm and Gems, Bundle Show and Gem List
Spring Doesn't Work. [ Uninitialized Constant Spring::Sid::Dl ]
Why Is the Timezone Off in Delayed_Job
How to Sort an Alphanumeric Array in Ruby
How to Find the Most Recently Modified Folder in a Directory Using Ruby
Ruby Symbol#To_Proc Leaks References in 1.9.2-P180
Rails Syntax Error: Unexpected Keyword_Ensure, Expecting Keyword_End
Why Do I Get "Undefined Method 'Paginate'" Error in Production
Can't Start Rails Server - Could Not Find a JavaScript Runtime
Can't Find a Route with an Underscore or Doesn't Treat It Properly
How to Create Zip File Only in Memory in Ruby
Error in Install Ruby-Debug-Base on Ruby 2.0.0P247
Count Iteration on the Enumerable Cycle
How to Escape the Vertical Bar When Doing Command Line with Ruby