Untitled
unknown
plain_text
2 years ago
874 B
22
Indexable
def unlock(self, key: str) -> str | None:
# If the key doesn't exist in the database or in the locks, it's an invalid request.
if key not in self.db and key not in self.locks:
return "invalid_request"
# If the key exists but is not locked, return None.
if key not in self.locks:
return None
# The key is locked, so we remove the lock.
self.locks.pop(key)
# If there are users waiting for this lock, transfer the lock to the next user.
if key in self.lock_queues and self.lock_queues[key]:
next_user = self.lock_queues[key].popleft()
self.locks[key] = next_user
if not self.lock_queues[key]: # Clean up the queue if it's empty.
del self.lock_queues[key]
return "released"
# If no one else is waiting, the lock is successfully released.
return "released"
Editor is loading...
Leave a Comment