about summary refs log tree commit diff
path: root/app/lib/request.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2018-11-22 20:12:04 +0100
committerGitHub <noreply@github.com>2018-11-22 20:12:04 +0100
commitfd8145d2322d8b13b6aaa9cd2a61331ff660c9dd (patch)
tree7466262da5fde059add6d1779df2ed4db57f11e8 /app/lib/request.rb
parent824497fbced31cb293fa1baee96105c666e34916 (diff)
Fix connect timeout not being enforced (#9329)
* Fix connect timeout not being enforced

The loop was catching the timeout exception that should stop execution, so the next IP would no longer be within a timed block, which led to requests taking much longer than 10 seconds.

* Use timeout on each IP attempt, but limit to 2 attempts

* Fix code style issue

* Do not break Request#perform if no block given

* Update method stub in spec for Request

* Move timeout inside the begin/rescue block

* Use Resolv::DNS with timeout of 1 to get IP addresses

* Update Request spec to stub Resolv::DNS instead of Addrinfo

* Fix Resolve::DNS stubs in Request spec
Diffstat (limited to 'app/lib/request.rb')
-rw-r--r--app/lib/request.rb32
1 files changed, 23 insertions, 9 deletions
diff --git a/app/lib/request.rb b/app/lib/request.rb
index fdaaf3636..bb6ef4661 100644
--- a/app/lib/request.rb
+++ b/app/lib/request.rb
@@ -2,6 +2,7 @@
 
 require 'ipaddr'
 require 'socket'
+require 'resolv'
 
 class Request
   REQUEST_TARGET = '(request-target)'
@@ -45,7 +46,7 @@ class Request
     end
 
     begin
-      yield response.extend(ClientLimit)
+      yield response.extend(ClientLimit) if block_given?
     ensure
       http_client.close
     end
@@ -94,7 +95,7 @@ class Request
   end
 
   def timeout
-    { connect: 10, read: 10, write: 10 }
+    { connect: nil, read: 10, write: 10 }
   end
 
   def http_client
@@ -139,16 +140,29 @@ class Request
   class Socket < TCPSocket
     class << self
       def open(host, *args)
-        return super host, *args if thru_hidden_service? host
+        return super(host, *args) if thru_hidden_service?(host)
+
         outer_e = nil
-        Addrinfo.foreach(host, nil, nil, :SOCK_STREAM) do |address|
-          begin
-            raise Mastodon::HostValidationError if PrivateAddressCheck.private_address? IPAddr.new(address.ip_address)
-            return super address.ip_address, *args
-          rescue => e
-            outer_e = e
+
+        Resolv::DNS.open do |dns|
+          dns.timeouts = 1
+
+          addresses = dns.getaddresses(host).take(2)
+          time_slot = 10.0 / addresses.size
+
+          addresses.each do |address|
+            begin
+              raise Mastodon::HostValidationError if PrivateAddressCheck.private_address?(IPAddr.new(address.to_s))
+
+              ::Timeout.timeout(time_slot, HTTP::TimeoutError) do
+                return super(address.to_s, *args)
+              end
+            rescue => e
+              outer_e = e
+            end
           end
         end
+
         raise outer_e if outer_e
       end