From 418fe9419e83fd2e34fe192ea522e06fc51bd8d5 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Thu, 3 Mar 2016 13:39:09 -0800 Subject: [PATCH] twisted: use persistent connections, slight speedup This uses a single TCP connection to the relay server for all requests (although it probably uses a second one for the downstream EventSource feed). This should squeeze out some of the round-trip times. --- src/wormhole/test/test_twisted.py | 12 ++++++++++++ src/wormhole/twisted/transcribe.py | 8 +++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/wormhole/test/test_twisted.py b/src/wormhole/test/test_twisted.py index fde7d1d..7ab2f1f 100644 --- a/src/wormhole/test/test_twisted.py +++ b/src/wormhole/test/test_twisted.py @@ -29,6 +29,7 @@ class Channel(ServerBase, unittest.TestCase): self._channel = c d.addCallback(_connected) d.addCallback(lambda _: self._channel.deallocate(u"happy")) + d.addCallback(lambda _: cm.shutdown()) return d def test_messages(self): @@ -60,6 +61,9 @@ class Channel(ServerBase, unittest.TestCase): self.failUnlessEqual(len(self._relay_server._apps), 0) d.addCallback(_gone) + d.addCallback(lambda _: cm1.shutdown()) + d.addCallback(lambda _: cm2.shutdown()) + return d def test_get_multiple_phases(self): @@ -96,6 +100,9 @@ class Channel(ServerBase, unittest.TestCase): self.failUnlessEqual(phase_and_body, (u"phase2", b"msg2"))) + d.addCallback(lambda _: cm1.shutdown()) + d.addCallback(lambda _: cm2.shutdown()) + return d def test_appid_independence(self): @@ -117,6 +124,11 @@ class Channel(ServerBase, unittest.TestCase): d.addCallback(lambda msg: self.failUnlessEqual(msg, b"msg1a")) d.addCallback(lambda _: c2b.get(u"phase1")) d.addCallback(lambda msg: self.failUnlessEqual(msg, b"msg1b")) + + d.addCallback(lambda _: cm1a.shutdown()) + d.addCallback(lambda _: cm2a.shutdown()) + d.addCallback(lambda _: cm1b.shutdown()) + d.addCallback(lambda _: cm2b.shutdown()) return d class Basic(ServerBase, unittest.TestCase): diff --git a/src/wormhole/twisted/transcribe.py b/src/wormhole/twisted/transcribe.py index acbab6f..d428d95 100644 --- a/src/wormhole/twisted/transcribe.py +++ b/src/wormhole/twisted/transcribe.py @@ -176,7 +176,8 @@ class ChannelManager: self._appid = appid self._side = side self._handle_welcome = handle_welcome - self._agent = web_client.Agent(reactor) + self._pool = web_client.HTTPConnectionPool(reactor, True) # persistent + self._agent = web_client.Agent(reactor, pool=self._pool) @inlineCallbacks def allocate(self): @@ -198,6 +199,8 @@ class ChannelManager: return Channel(self._relay, self._appid, channelid, self._side, self._handle_welcome, self._agent) + def shutdown(self): + return self._pool.closeCachedConnections() def close_on_error(meth): # method decorator # Clients report certain errors as "moods", so the server can make a @@ -473,4 +476,7 @@ class Wormhole: _sent = self._timing.add_event("close") yield c.deallocate(mood) self._timing.finish_event(_sent) + _sent = self._timing.add_event("pool shutdown") + yield self._channel_manager.shutdown() + self._timing.finish_event(_sent)