From 20278eeee745779389162ce1c23caaf85a09444b Mon Sep 17 00:00:00 2001 From: rrr-marble Date: Fri, 2 Jul 2021 15:33:22 +0300 Subject: [PATCH] ref: try 3 times before giving up on new session --- main.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 91eec2c..42a8d8c 100644 --- a/main.py +++ b/main.py @@ -16,7 +16,9 @@ cur = con.cursor() # NB! single is enough for now, we might require multiple la async def new_session(): """Start a new session""" # add session to the database - try: + tries = 3 # something is very wrong with our random, if we miss 3 times + for i in range(tries): + try: # generate a cookie cookie = uuid4().hex cur.execute( @@ -27,8 +29,13 @@ async def new_session(): ) con.commit() except sqlite3.IntegrityError as e: - if str(e) == "UNIQUE constraint failed: sessions.cookie": + if i < tries - 1 and str(e) == "UNIQUE constraint failed: sessions.cookie": + continue + else if str(e) == "UNIQUE constraint failed: sessions.cookie": return {"cookie": "error"} + else: + raise + break # return new session cookie return {"cookie": cookie}