Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
874 B
2
Indexable
Never
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"
Leave a Comment