about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2018-11-29 19:22:52 +0100
committerThibaut Girka <thib@sitedethib.com>2018-11-29 19:22:52 +0100
commitad7a3974cc48946b5cb8182da4f9e236aa5e39a1 (patch)
tree4ec696e3bfd7406b0fbe54f71aed649241663ecf /app/lib
parent6a264c9379a50cb94afc8dd369bfc5b626a9c4d1 (diff)
parentfa9f28f52d38b5cf3184d6bc77f5554b74fca348 (diff)
Merge branch 'master' into glitch-soc/merge-upstream
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/request.rb22
1 files changed, 20 insertions, 2 deletions
diff --git a/app/lib/request.rb b/app/lib/request.rb
index bb6ef4661..4a81773e3 100644
--- a/app/lib/request.rb
+++ b/app/lib/request.rb
@@ -4,6 +4,16 @@ require 'ipaddr'
 require 'socket'
 require 'resolv'
 
+# Monkey-patch the HTTP.rb timeout class to avoid using a timeout block
+# around the Socket#open method, since we use our own timeout blocks inside
+# that method
+class HTTP::Timeout::PerOperation
+  def connect(socket_class, host, port, nodelay = false)
+    @socket = socket_class.open(host, port)
+    @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay
+  end
+end
+
 class Request
   REQUEST_TARGET = '(request-target)'
 
@@ -95,7 +105,11 @@ class Request
   end
 
   def timeout
-    { connect: nil, read: 10, write: 10 }
+    # We enforce a 1s timeout on DNS resolving, 10s timeout on socket opening
+    # and 5s timeout on the TLS handshake, meaning the worst case should take
+    # about 16s in total
+
+    { connect: 5, read: 10, write: 10 }
   end
 
   def http_client
@@ -163,7 +177,11 @@ class Request
           end
         end
 
-        raise outer_e if outer_e
+        if outer_e
+          raise outer_e
+        else
+          raise SocketError, "No address for #{host}"
+        end
       end
 
       alias new open