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

View File

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