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.
This commit is contained in:
Brian Warner 2016-03-03 13:39:09 -08:00
parent 4f5e037d39
commit 418fe9419e
2 changed files with 19 additions and 1 deletions

View File

@ -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):

View File

@ -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)