Improve HTTP cleanup and multi-response lock

This commit is contained in:
Andrew Godwin 2016-02-14 18:48:53 +00:00
parent 011ec23333
commit ba2bbffd20
2 changed files with 18 additions and 3 deletions

View File

@ -69,7 +69,7 @@ class WebRequest(http.Request):
protocol.dataReceived(data)
# Remove our HTTP reply channel association
logger.debug("Upgraded connection %s to WebSocket %s", self.reply_channel, protocol.reply_channel)
self.factory.reply_protocols[self.reply_channel] = None
del self.factory.reply_protocols[self.reply_channel]
self.reply_channel = None
# Boring old HTTP.
else:
@ -101,11 +101,20 @@ class WebRequest(http.Request):
"""
Cleans up reply channel on close.
"""
if self.reply_channel:
if self.reply_channel and self.reply_channel in self.channel.factory.reply_protocols:
del self.channel.factory.reply_protocols[self.reply_channel]
logger.debug("HTTP disconnect for %s", self.reply_channel)
http.Request.connectionLost(self, reason)
def finish(self):
"""
Cleans up reply channel on close.
"""
if self.reply_channel:
del self.channel.factory.reply_protocols[self.reply_channel]
logger.debug("HTTP close for %s", self.reply_channel)
http.Request.finish(self)
def serverResponse(self, message):
"""
Writes a received HTTP response back out to the transport.

View File

@ -1,8 +1,11 @@
import logging
import time
from twisted.internet import reactor
from .http_protocol import HTTPFactory
logger = logging.getLogger(__name__)
class Server(object):
@ -27,10 +30,13 @@ class Server(object):
channels = self.factory.reply_channels()
# Quit if reactor is stopping
if not reactor.running:
logging.debug("Backend reader quitting due to reactor stop")
return
# Don't do anything if there's no channels to listen on
if channels:
channel, message = self.channel_layer.receive_many(channels, block=True)
channel, message = self.channel_layer.receive_many(channels, block=False)
if channel:
logging.debug("Server got message on %s", channel)
else:
time.sleep(0.1)
continue