Hi,
I added an additional Redis client within the Channels Consumer, to check if a user is currently connected via WebSockets or not. If not, I may save some expensive DB calls to generate a message he is not even getting, since he is not connected anyway.
class MyMessengerConsumer(AsyncJsonWebsocketConsumer):
__HOST: Optional[str] = None
__PORT: Optional[int] = None
__UNIX: Optional[str] = None
__DB: int = 0
__CLIENT: Optional[Redis] = None
def __init__(self, *args, **kwargs):
super().__init__(args, kwargs)
if self.__CLIENT is None:
self.create_client()
@classmethod
def create_client(cls) -> None:
if cls.__UNIX is None and cls.__HOST is None:
config = parse_in_memory_db_config_file()
if config.unix_socket is None:
cls.__HOST = config.host
cls.__PORT = config.port
else:
cls.__UNIX = config.unix_socket
cls.__DB = config.db
if cls.__UNIX is None:
cls.__CLIENT = Redis(host=cls.__HOST, port=cls.__PORT, db=cls.__DB)
else:
cls.__CLIENT = Redis(unix_socket_path=cls.__UNIX, db=cls.__DB)
# Ommited addtional functionallity
The question is: Do I need to close this client, if the process of this ASGI server instance is terminated (a typical scenario would be: A “restart”/“stop” command from systemd)? If yes, how may I do this, if I spawn multiple processes with this ASGI server (for example via Hypercorn). I would need to close every Redis client for each process.
NOTE: The Redis DOCs say a TCP connection is closed after 5 minutes. How about UNIX sockets? Is it handled the same?