Working with updates in MTProto

Apart from the basic operations required to work with MTProto update sequences, implementations also have to take care to postpone updates received via the socket while filling gaps in the event and Update sequences, as well as avoid filling gaps in the same sequence.

An interesting and easy way this can be implemented, instead of using various locks, is by running background loops, like in MadelineProto.

When a getDifference is requested, a signal resumes the updater loop, which starts fetching difference until the gap is filled, then it pauses itself again.

When a new Updates constructor or event is received, it is added to the FIFO queue of the correspondent feeder loop, and a signal is sent to said loop. The signal wakes up the loop, which starts parsing the queued events: as per update handling, duplicates are eliminated, correct events are accepted and the state is updated. When a gap is encountered, the updater loop is resumed: the feeder loop waits until the updater loop is paused again. Finally, the manually fetched difference is prepended to the feeder FIFO queue, to parse it before any additional update that was fetched from the socket while fetching the difference.

The simplicity of this approach is in its sequentiality:

This site uses cookies, as described in the cookie policy. By clicking on "Accept" you consent to the use of cookies.