14/27
Part 4 of 7 · Lost and repeated messagesLearn and do

A timeout asks again

Lost messages need a timer. In a real system, a machine that has waited too long for a child asks again. The tests stand in for the clock: when the network goes quiet and the root has no answer, they call tick() on every machine. A tick is a timeout firing.

On a tick, what should a machine do?

  • Asked, and still waiting: send a new ask to each child that hasn't answered. Not to the children that have. They'd only answer again, and on a big tree that's a flood of messages for nothing.
  • Never asked: nothing. It isn't part of a count yet.
  • Already answered: nothing. Its part is done.

Two flags tell the three apart. self.asked turns on when the machine starts its part, and self.answer holds the answer it sent, once it has sent one. The next step uses self.answer again.

The editor holds the level 2 Machine, now with self.asked and self.answer, and an empty tick. Write tick: a machine that was asked and hasn't answered asks again each child missing from self.replies.