1313import string
1414from contextlib import suppress
1515from datetime import datetime
16+ from typing import Final
1617
1718import redis .asyncio as aioredis
1819from aiohttp import web
@@ -65,6 +66,33 @@ async def get_authorized_user(request: web.Request) -> dict:
6566 return {}
6667
6768
69+ # GUEST_USER_RC_LOCK:
70+ #
71+ # These locks prevents the GC from deleting a GUEST user in to stages of its lifefime:
72+ #
73+ # 1. During construction:
74+ # - Prevents GC from deleting this GUEST user while it is being created
75+ # - Since the user still does not have an ID assigned, the lock is named with his random_user_name
76+ # - the timeout here is the TTL of the lock in Redis. in case the webserver is overwhelmed and cannot create
77+ # a user during that time or crashes, then redis will ensure the lock disappears and let the garbage collector do its work
78+ #
79+ MAX_DELAY_TO_CREATE_USER : Final [int ] = 5 # secs
80+ #
81+ # 2. During initialization
82+ # - Prevents the GC from deleting this GUEST user, with ID assigned, while it gets initialized and acquires it's first resource
83+ # - Uses the ID assigned to name the lock
84+ #
85+ MAX_DELAY_TO_GUEST_FIRST_CONNECTION : Final [int ] = 15 # secs
86+ #
87+ #
88+ # NOTES:
89+ # - In case of failure or excessive delay the lock has a timeout that automatically unlocks it
90+ # and the GC can clean up what remains
91+ # - Notice that the ids to name the locks are unique, therefore the lock can be acquired w/o errors
92+ # - These locks are very specific to resources and have timeout so the risk of blocking from GC is small
93+ #
94+
95+
6896async def create_temporary_guest_user (request : web .Request ):
6997 """Creates a guest user with a random name and
7098
@@ -86,33 +114,6 @@ async def create_temporary_guest_user(request: web.Request):
86114 password = generate_password (length = 12 )
87115 expires_at = datetime .utcnow () + settings .STUDIES_GUEST_ACCOUNT_LIFETIME
88116
89- # GUEST_USER_RC_LOCK:
90- #
91- # These locks prevents the GC from deleting a GUEST user in to stages of its lifefime:
92- #
93- # 1. During construction:
94- # - Prevents GC from deleting this GUEST user while it is being created
95- # - Since the user still does not have an ID assigned, the lock is named with his random_user_name
96- # - the timeout here is the TTL of the lock in Redis. in case the webserver is overwhelmed and cannot create
97- # a user during that time or crashes, then redis will ensure the lock disappears and let the garbage collector do its work
98- #
99- MAX_DELAY_TO_CREATE_USER = 5 # secs
100- #
101- # 2. During initialization
102- # - Prevents the GC from deleting this GUEST user, with ID assigned, while it gets initialized and acquires it's first resource
103- # - Uses the ID assigned to name the lock
104- #
105- MAX_DELAY_TO_GUEST_FIRST_CONNECTION = 15 # secs
106- #
107- #
108- # NOTES:
109- # - In case of failure or excessive delay the lock has a timeout that automatically unlocks it
110- # and the GC can clean up what remains
111- # - Notice that the ids to name the locks are unique, therefore the lock can be acquired w/o errors
112- # - These locks are very specific to resources and have timeout so the risk of blocking from GC is small
113- #
114-
115- # (1) read details above
116117 usr = None
117118 try :
118119 async with redis_locks_client .lock (
0 commit comments