Ruby Net::HTTP second request when timeout
Asked Answered
S

1

5

I noticed that when a http request times out, Ruby (2.6.1) makes a second request. This causes problems with one of our endpoints, because a second worker is triggered which takes up resources.

You can see an example here: Go to https://beeceptor.com/console/timeout and run the following code

require "net/http"

http = Net::HTTP.new("timeout.free.beeceptor.com", 443)
http.read_timeout = 1
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.request(Net::HTTP::Get.new("/Time"))

You can see that there are 2 requests to /Time, so I was wondering:

  1. Whats the purpose of this default behaviour? When I do the same query with a curl command, I don't get a second request curl --max-time 1 https://timeout.free.beeceptor.com
  2. How can I influence this behaviour?
  3. Or am I doing something wrong?
Surprising answered 3/7, 2019 at 6:38 Comment(0)
K
6

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"))
Kathikathiawar answered 3/7, 2019 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.