Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
850 B
2
Indexable
Never
def unlock(self, key: str) -> str | None:
    # First, check if the key exists in the database at all.
    if key not in self.db and key not in self.locks:
        return "invalid_request"

    # If there is no lock on the key but it exists in the database, return None.
    if key not in self.locks:
        return None

    # Handle the lock release.
    current_lock = self.locks.pop(key, None)

    # If there are users waiting in the lock queue.
    if self.lock_queues.get(key):
        next_user = self.lock_queues[key].popleft()
        self.locks[key] = next_user  # Transfer the lock to the next user
        return "released"  # Lock was released and transferred to the next user

    # If the lock is released without any queue.
    return "released"  # This case means the lock was released from the current user with no one to transfer
Leave a Comment