robust broadcast websocket error hanndling

This commit is contained in:
chris meyers
2020-02-21 13:51:47 -05:00
committed by Ryan Petrello
parent d6594ab602
commit 8350bb3371

View File

@@ -2,11 +2,13 @@
import os import os
import json import json
import logging import logging
import aiohttp
import asyncio import asyncio
import datetime import datetime
import sys import sys
import aiohttp
from aiohttp import client_exceptions
from channels_redis.core import RedisChannelLayer from channels_redis.core import RedisChannelLayer
from channels.layers import get_channel_layer from channels.layers import get_channel_layer
@@ -107,12 +109,18 @@ class WebsocketTask():
logger.warn(f"{self.name} connection to {self.remote_host} cancelled") logger.warn(f"{self.name} connection to {self.remote_host} cancelled")
self.stats.record_connection_lost() self.stats.record_connection_lost()
raise raise
except client_exceptions.ClientConnectorError as e:
logger.warn(f"Failed to connect to {self.remote_host}: '{e}'. Reconnecting ...")
self.stats.record_connection_lost()
self.start(attempt=attempt+1)
except asyncio.TimeoutError:
logger.warn(f"Timeout while trying to connect to {self.remote_host}. Reconnecting ...")
self.stats.record_connection_lost()
self.start(attempt=attempt+1)
except Exception as e: except Exception as e:
# Early on, this is our canary. I'm not sure what exceptions we can really encounter. # Early on, this is our canary. I'm not sure what exceptions we can really encounter.
# Does aiohttp throws an exception if a disconnect happens? logger.warn(f"Websocket broadcast client exception {type(e)} {e}")
logger.warn(f"Websocket broadcast client exception {str(e)}")
self.stats.record_connection_lost() self.stats.record_connection_lost()
# Reconnect
self.start(attempt=attempt+1) self.start(attempt=attempt+1)
def start(self, attempt=0): def start(self, attempt=0):