When I read the Node HTTP module documentation, I noticed the server.timeout property. I wanted to introduce it briefly, but after sorting it out, I found that there is a huge content supporting timeout: server.timout -> node core timers -> uv timers -> linux msleep/hrtimer -> clocksource -> tsc -> cmos rtc -> clock After the end of the timer series, Noder can roughly understand: How clock cycle drives Linux msleep/hrtimer; The relationship between Linux timers and UV timers; Relationship between Node timers and UV timers.

How to implement the timer mechanism in libuv layer with the help of epoll_wait. Now let’s talk about the timer related to Nodejs application layer.

Data structure and invocation relationships

The data structures from the top to the bottom are:

  1. Nodejs – Linked list, minimum heap (priority queue for implementation)
  2. Libuv – Minimum heap
  3. Linux – Bucket – linked list, red black tree

Consider the scenario, what is the data model between the three

setTimout(fn1(){}, 1000);
setTimout(fn2(){}, 1000);
setTimout(fn3(){}, 2000);
setTimout(fn4(){}, 2000);
setTimout(fn5(){}, 3000);
setTimout(fn6(){}, 3000);
Copy the code

Key functions:

  1. scheduleTimer – Node calls this function to build libuv data
  2. processTimers– Node indicates the node timerThe callback function.Libuv will then trigger a callback, there is this visible concreteFn [x] is not passed and libuvIs executed on the Node side
  3. Epoll_wait-libuv calls Linux system functions to build a red-black tree of high-precision timers

Key points:

  1. The data is different at the three levels
    1. The Node endTime + functionThree time points, six functions
    2. Libuv endtimeThree time points
    3. Linux sideThe minimum time, a point in time
  2. Call relationship
    1. The NodejsfixedtheThe timing callback function processTimersPass to libuv and call libuv via scheduleTimer,Waiting for the callback
    2. Minimum Libuv read timeA synchronized blockCall Linux’s epoll_wait and the process entersInterrupted sleep.Wait for interrupt
    3. Linux High precision timer set cycles of APIC,Continue to performOther tasks,The APIC then triggers the interrupt

Nodejs the timers

Nodejs provides timers that are not only used by users, but also widely used by themselves. For Nodes, timers are a kind of protection mechanism, so let’s take a look at where Node itself is used.

  1. TCP related modules– HTTP [s], net
    1. server.timeout
    2. server.keepAliveTimeout tcp
  2. Child_process module
    1. child_process.exec(… , {timeout})
  3. DNS module
    1. Resolver({ timeout })
  4. Readline module
    1. readline.createInterface({… , escapeCodeTimeout})
  5. The vm module
    1. script.runInContext(… , { timeout})

The above modules or methods in modules are heavy users of system resources. Nodejs, as a server-side programming language, needs to pay special attention to the cost of resources. Therefore, in places with resource costs, Timer guardians are generally used to release resources in a timely manner.

server.timeout

Socket hang Up and connect ECONNREFUSED This content will cover other aspects of the timer, socket, TCP, etc. This content is relatively independent, so it will be the last section of timers and will be discussed next time.

Follow my wechat official account “SUNTOPO WLOG”, welcome to leave a comment and discuss, I will reply as much as possible, thank you for reading.