Untitled

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