Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the timer of nodejs

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how nodejs timers are used. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Take a look at the use of timers.

Int main ()

V_timer_t once

Uv_timer_init (uv_default_loop (), & once)

Uv_timer_start (& once, once_cb, 10,0)

Uv_run (uv_default_loop (), UV_RUN_DEFAULT)

Return 0

}

Let's start with the uv_timer_init function.

/ / initialize the uv_timer_t structure

Int uv_timer_init (uv_loop_t* loop, uv_timer_t* handle) {

Uv__handle_init (loop, (uv_handle_t*) handle, UV_TIMER)

Handle- > timer_cb = NULL

Handle- > repeat = 0

Return 0

}

The init function, like the init function in other stages, initializes handle and private fields. Then let's look at the start function. This function starts a timer (omitting part of the code).

/ / start a timer

Int uv_timer_start (

Uv_timer_t* handle

Uv_timer_cb cb

Uint64_t timeout

Uint64_t repeat

) {

Uint64_t clamped_timeout

/ / stop the previous one when you re-execute start

If (uv__is_active (handle))

Uv_timer_stop (handle)

/ / timeout, absolute value

Clamped_timeout = handle- > loop- > time + timeout

/ / initialize callback, timeout, whether to repeat timing, and assign an independent and unparalleled id

Handle- > timer_cb = cb

Handle- > timeout = clamped_timeout

Handle- > repeat = repeat

/ * start_id is the second index to be compared in uv__timer_cmp () * /

Handle- > start_id = handle- > loop- > timer_counter++

/ / insert minimum heap

Heap_insert (timer_heap (handle- > loop)

(struct heap_node*) & handle- > heap_node

Timer_less_than)

/ / activate the handle

Uv__handle_start (handle)

Return 0

}

The start function first initializes some fields in the handle, including timeout callback, whether to start the timer repeatedly, the absolute time of the timeout, and so on. Then insert the handle node into the minimum heap. Finally, mark the handle and activate the handle. The structure at this time is as follows.

This is the timer phase of the event loop.

/ / find out the node that has timed out and execute the callback inside

Void uv__run_timers (uv_loop_t* loop) {

Struct heap_node* heap_node

Uv_timer_t* handle

For (;;) {

Heap_node = heap_min (timer_heap (loop))

If (heap_node = = NULL)

Break

Handle = container_of (heap_node, uv_timer_t, heap_node)

/ / return if the time of the current node is greater than the current time, indicating that the subsequent nodes have not timed out.

If (handle- > timeout > loop- > time)

Break

/ / remove the timer node and reinsert the minimum heap, if repeat is set

Uv_timer_stop (handle)

Uv_timer_again (handle)

/ / execute timeout callback

Handle- > timer_cb (handle)

}

}

Libuv caches the current time at the beginning of each event loop, and the cached time is used throughout the event loop. After caching the current latest time, the uv__run_timers is executed, and the logic of this function is clear, which is to iterate through the smallest heap to find the node that currently timed out. Because the nature of the heap is that the parent node must be smaller than the child. So if a node is found and it does not time out, then the subsequent nodes will not time out. His callback is known for the node that times out. After performing the callback, there are two more key operations. The first is stop, and the second is again.

/ / stop a timer

Int uv_timer_stop (uv_timer_t* handle) {

If (! uv__is_active (handle))

Return 0

/ / remove the timer node from the minimum heap

Heap_remove (timer_heap (handle- > loop)

(struct heap_node*) & handle- > heap_node

Timer_less_than)

/ / clear the active state and the number of active of handle minus one

Uv__handle_stop (handle)

Return 0

}

The logic of stop is very simple, which is to remove handle from the binary heap. And deactivate the state. So what is againt? Again is designed to support scenarios such as setInterval.

/ / restart a timer. You need to set the repeat flag

Int uv_timer_again (uv_timer_t* handle) {

/ / if the repeat flag is set, the timer needs to be triggered repeatedly.

If (handle- > repeat) {

/ / remove the old timer node from the minimum heap, and then restart a timer

Uv_timer_stop (handle)

Uv_timer_start (handle, handle- > timer_cb, handle- > repeat, handle- > repeat)

}

Return 0

}

If the handle sets the repeat flag, the handle continues to perform a timeout callback after the timeout, after each repeat. For setInterval, the timeout is x, and after each x, a callback is performed. This is the underlying principle of timers in nodejs. But nodejs doesn't insert a node into the smallest heap every time you call setTimeout. In nodejs, there is only one handle about uv_timer_s. He maintains a data structure in the js layer, calculates the earliest expired nodes each time, and then modifies the handle timeout. The specific principle has been analyzed in a previous article.

There is also some connection between the timer phase and the poll io phase, because poll io may cause the main thread to block. In order to ensure that the main thread can execute the callback of the timer as soon as possible, poll io cannot block all the time, so at this time, the blocking duration is the duration of the timer node with the fastest expiration.

Thank you for reading! This is the end of the article on "how to use the timer of nodejs". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report