A reservation hides a task, and only its holder can finish it
Why not pop the task and forget about it? Because workers crash. If the queue forgets a task the moment a worker takes it, a crash loses that task for good.
So reserve doesn't delete. It moves the task from waiting to reserved and writes down who holds it. The task is hidden from every other worker, but the queue still knows about it. Later the holder sends an ack, short for acknowledgment: the work is done, and the queue can mark it finished.
That needs two dicts:
| Dict | Maps | Example |
|---|---|---|
status | task id to its state | {"a": "reserved", "b": "waiting"} |
holder | a reserved task to its worker | {"a": "w1"} |
An ack counts only when it comes from the holder. Any other ack returns False and changes nothing:
- another worker's ack, because it never had the task
- a second ack from the holder, because the task is already done
- an ack for an id the queue never saw
Why False and not an exception? In a real system, acks arrive late, twice, or from a worker that lost its task. Those are normal events. A boolean lets the worker log it and move on.
w1 reserves a. Then w2 calls ack("w2", "a"). What should happen?