summary refs log tree commit diff
path: root/synapse/http
diff options
context:
space:
mode:
authorKegan Dougal <kegan@matrix.org>2017-05-05 10:49:19 +0100
committerKegan Dougal <kegan@matrix.org>2017-05-05 10:49:19 +0100
commit7dd3bf5e246144806c83b5ed2127733906908331 (patch)
treebb021ca308af1595a210175a48dd0be86a9a1683 /synapse/http
parentMerge pull request #2186 from matrix-org/revert-2175-erikj/prefill_state (diff)
downloadsynapse-7dd3bf5e246144806c83b5ed2127733906908331.tar.xz
Rewrite SimpleHttpClient.request to include timeouts
Fixes #2191
Diffstat (limited to 'synapse/http')
-rw-r--r--synapse/http/client.py32
1 files changed, 19 insertions, 13 deletions
diff --git a/synapse/http/client.py b/synapse/http/client.py
index 9cf797043a..68bd06abd9 100644
--- a/synapse/http/client.py
+++ b/synapse/http/client.py
@@ -75,36 +75,42 @@ class SimpleHttpClient(object):
         if hs.config.user_agent_suffix:
             self.user_agent = "%s %s" % (self.user_agent, hs.config.user_agent_suffix,)
 
+    @defer.inlineCallbacks
     def request(self, method, uri, *args, **kwargs):
         # A small wrapper around self.agent.request() so we can easily attach
         # counters to it
         outgoing_requests_counter.inc(method)
-        d = preserve_context_over_fn(
-            self.agent.request,
-            method, uri, *args, **kwargs
-        )
+
+
+        def send_request():
+            request_deferred = self.agent.request(
+                method, uri, *args, **kwargs
+            )
+
+            return self.clock.time_bound_deferred(
+                request_deferred,
+                time_out=60,
+            )
 
         logger.info("Sending request %s %s", method, uri)
 
-        def _cb(response):
+        try:
+            with logcontext.PreserveLoggingContext():
+                response = yield send_request()
+
             incoming_responses_counter.inc(method, response.code)
             logger.info(
                 "Received response to  %s %s: %s",
                 method, uri, response.code
             )
             return response
-
-        def _eb(failure):
+        except Exception as e:
             incoming_responses_counter.inc(method, "ERR")
             logger.info(
                 "Error sending request to  %s %s: %s %s",
-                method, uri, failure.type, failure.getErrorMessage()
+                method, uri, type(e).__name__, e.message
             )
-            return failure
-
-        d.addCallbacks(_cb, _eb)
-
-        return d
+            raise e
 
     @defer.inlineCallbacks
     def post_urlencoded_get_json(self, uri, args={}):