I was looking to tokio source code to get the answer for the question and I was under the impression that the sleep method literally puts a timer with duration but I think i might have misunderstood the code because doing so would be highly inefficient. Would it be possible to get a clearer picture of this?
When you await the sleep
method, two things happen:
The Tokio runtime will, between polling tasks running on it, check both epoll for IO events and the timer wheel for timer events. Whenever such an event happens, the Tokio runtime emits a wake-up to the associated task, putting that task back on to the run queue.
When no tasks are ready to run, Tokio will go to sleep by blocking on an epoll event. This sleep has a timeout equal to the smallest timer in the timer wheel allowing Tokio to wake that task up when the timer elapses.
A timer wheel is a data structure that lets you access the smallest timer efficiently, similar to e.g. a binary heap, but it allows removal of timers if they are cancelled.