Untitled

mail@pastecode.io avatar
unknown
plain_text
4 months ago
948 B
2
Indexable
def unlock(self, key: str) -> str | None:
    # Check if the key exists in the database or it has pending locks (even if deleted)
    if key not in self.db and key not in self.locks:
        return "invalid_request"  # The key neither exists nor is locked

    # If the key is not locked but exists in the db, return None
    if key not in self.locks:
        return None  # No lock to release

    # Remove the lock from the current holder
    self.locks.pop(key)

    # Check if there are users waiting for the lock
    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"
    else:
        # No one else is waiting for the lock
        return "released"
Leave a Comment