Untitled
unknown
plain_text
a year ago
882 B
11
Indexable
def unlock(self, key: str) -> str | None:
# Case 1: Key does not exist in the database or in the lock system
if key not in self.db and key not in self.locks:
return "invalid_request"
# Case 2: Key exists but is not locked by any user
if key not in self.locks:
return None
# Remove the lock from the current holder
self.locks.pop(key)
# Case 3: There are users waiting for the lock in the queue
if self.lock_queues.get(key):
# Transfer the lock to the next user in the queue
next_user = self.lock_queues[key].popleft()
self.locks[key] = next_user # Assign lock to the next user
if not self.lock_queues[key]: # Clean up if the queue is empty
del self.lock_queues[key]
return "released"
# Case 4: Lock released, no one is waiting for it
return "released"
Editor is loading...
Leave a Comment