0

setTimeout in nodejs is implemented with c language library libev ev_timer. How does ev_timer work?

Is it implemented using polling? If I set a timeout of 30 seconds, does any process check for every milliseconds for pending timeouts?

raju
  • 4,788
  • 15
  • 64
  • 119

1 Answers1

0

libev has been replaced by libuv.

The timer API is illustrated here

Basically the event loop goes for polling after all other non-IO activities in the system are completed. At the moment, it will have one or more I/O events pending, and one or more timer events, among other things. The input to the poll is crafted in such a way that it's timeout is the least among the registered time events. This, in conjunction with a relative time field which the event loop maintains, helps to figure out the right time for the timer callback to be fired.

In short, the amount of time to wait is delegated to the OS through the poll call, not through regular wake up and recheck.

Hope this helps.

Gireesh Punathil
  • 1,344
  • 8
  • 18