Attempt on handling timeout errors

This code retries to load sessions three times. If not an error will be
thrown. There are other solutions (Like not throwing the error), but
I would like to see errors in production usage to see, if this helps or
not.
This commit is contained in:
Finn Stutzenstein 2021-03-04 08:43:40 +01:00
parent 45948c47fb
commit 39fb2fadec
No known key found for this signature in database
GPG Key ID: 9042F605C6324654

View File

@ -1,4 +1,5 @@
# type: ignore
from time import sleep
from asgiref.sync import async_to_sync
from django.conf import settings
@ -33,7 +34,16 @@ class SessionStore(SessionBase):
return salted_hmac(key_salt, value).hexdigest()
def load(self):
return async_to_sync(self._load)()
retries = 0
while True:
try:
return async_to_sync(self._load)()
except TimeoutError as e:
if retries < 3:
sleep(0.1)
else:
raise e
retries += 1
async def _load(self):
async with get_connection(read_only=True) as redis: