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:
- Nodejs – Linked list, minimum heap (priority queue for implementation)
- Libuv – Minimum heap
- 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:
scheduleTimer
– Node calls this function to build libuv dataprocessTimers
– Node indicates the node timerThe callback function
.Libuv will then trigger a callback
, there is this visible concreteFn [x] is not passed and libuv
Is executed on the Node side- Epoll_wait-libuv calls Linux system functions to build a red-black tree of high-precision timers
Key points:
- The data is different at the three levels
- The Node end
Time + function
Three time points, six functions - Libuv end
time
Three time points - Linux side
The minimum time
, a point in time
- The Node end
- Call relationship
- The Nodejs
fixed
theThe timing callback function processTimers
Pass to libuv and call libuv via scheduleTimer,Waiting for the callback
- Minimum Libuv read time
A synchronized block
Call Linux’s epoll_wait and the process entersInterrupted sleep
.Wait for interrupt
- Linux High precision timer set cycles of APIC,
Continue to perform
Other tasks,The APIC then triggers the interrupt
- The Nodejs
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.
TCP related modules
– HTTP [s], net- server.timeout
- server.keepAliveTimeout tcp
Child_process module
- child_process.exec(… , {timeout})
DNS module
- Resolver({ timeout })
Readline module
- readline.createInterface({… , escapeCodeTimeout})
The vm module
- 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.