Take note of backpressure

This commit is contained in:
Andrew Godwin 2016-05-07 13:00:09 -07:00
parent 905b71a745
commit 2d777e75f9
2 changed files with 55 additions and 36 deletions

View File

@ -111,6 +111,7 @@ class WebRequest(http.Request):
self.client_addr = None
self.server_addr = None
# Send message
try:
self.factory.channel_layer.send("http.request", {
"reply_channel": self.reply_channel,
# TODO: Correctly say if it's 1.1 or 1.0
@ -124,6 +125,9 @@ class WebRequest(http.Request):
"client": self.client_addr,
"server": self.server_addr,
})
except self.factory.channel_layer.ChannelFull:
# Channel is too full; reject request with 503
self.basic_error(503, b"Service Unavailable", "Request queue full.")
@classmethod
def unquote(cls, value):
@ -140,9 +144,12 @@ class WebRequest(http.Request):
Sends a disconnect message on the http.disconnect channel.
Useful only really for long-polling.
"""
try:
self.factory.channel_layer.send("http.disconnect", {
"reply_channel": self.reply_channel,
})
except self.factory.channel_layer.ChannelFull:
pass
def connectionLost(self, reason):
"""

View File

@ -82,7 +82,11 @@ class WebSocketProtocol(WebSocketServerProtocol):
def onOpen(self):
# Send news that this channel is open
logger.debug("WebSocket open for %s", self.reply_channel)
try:
self.channel_layer.send("websocket.connect", self.request_info)
except self.channel_layer.ChannelFull:
# We don't drop the connection here as you don't _have_ to consume websocket.connect
pass
self.factory.log_action("websocket", "connected", {
"path": self.request.path,
"client": "%s:%s" % tuple(self.client_addr) if self.client_addr else None,
@ -92,6 +96,7 @@ class WebSocketProtocol(WebSocketServerProtocol):
logger.debug("WebSocket incoming packet on %s", self.reply_channel)
self.packets_received += 1
self.last_data = time.time()
try:
if isBinary:
self.channel_layer.send("websocket.receive", {
"reply_channel": self.reply_channel,
@ -106,6 +111,10 @@ class WebSocketProtocol(WebSocketServerProtocol):
"order": self.packets_received,
"text": payload.decode("utf8"),
})
except self.channel_layer.ChannelFull:
# We don't drop the connection here as you don't _have_ to consume websocket.receive
# TODO: Maybe add an option to drop if this is backlogged?
pass
def serverSend(self, content, binary=False):
"""
@ -128,11 +137,14 @@ class WebSocketProtocol(WebSocketServerProtocol):
if hasattr(self, "reply_channel"):
logger.debug("WebSocket closed for %s", self.reply_channel)
del self.factory.reply_protocols[self.reply_channel]
try:
self.channel_layer.send("websocket.disconnect", {
"reply_channel": self.reply_channel,
"path": self.unquote(self.path),
"order": self.packets_received + 1,
})
except self.channel_layer.ChannelFull:
pass
self.factory.log_action("websocket", "disconnected", {
"path": self.request.path,
"client": "%s:%s" % tuple(self.client_addr) if self.client_addr else None,