Nginx source code learning notes (23) -- event module (4) -- timer red/black tree

Source: Internet
Author: User
Tags epoll

In section 21, we mentioned calling ngx_eventfind_timer () to obtain the timer and then passing it to the epoll module for the wait time. Today we will mainly explain this method.

From: http://blog.csdn.net/lengzijian


Timer In nginx is sorted by the structure of the red/black tree. Ngx_event_timer_rbtree is the red/black tree of timer in nginx.

1. Let's take a look at the structure of ngx_event_timer_rbtree:

Src/CORE/struct ngx_rbtree_s ngx_rbtree_t; typedef void (* handle) (kernel * root, ngx_rbtree_node_t * node, kernel * Sentinel); struct ngx_rbtree_s {kernel * root; // root node ngx_rbtree_node_t * Sentinel; // nx_rbtree_insert_pt insert; // insert method pointer };

2. Structure of nodes in the tree:

Src/CORE/javasngx_uint_t regular; typedef ngx_int_t regular; typedef struct ngx_rbtree_node_s ngx_rbtree_node_t; struct ngx_rbtree_node_s {primary key; required * left; // The left node has * right; // right node ngx_rbtree_node_t * parent; // parent node u_char color; // current node color u_char data; // current node data };

3. ngx_event_find_timer function:

Src/event/Merge (void) {ngx_msec_int_t timer; ngx_rbtree_node_t * node, * root, * Sentinel; // if the root node address is equal to that of the Sentinel, return-1 If (response = & Response) {return ngx_timer_infinite;} // lock ngx_event_timer_mutex ngx_mutex_lock (ngx_event_timer_mutex); root = cursor; sentinel = cursor; // obtain the minimum T value. Worker defines node = ngx_rbtree_min (root, Sentinel); // unlock ngx_mutex_unlock (unlock); // subtract ngx_current_mses value from the value of the minimum timer node (ngx_msec_int_t) node-> key-(ngx_msec_int_t) ngx_current_msec; Return (ngx_msec_t) (timer> 0? Timer: 0 );}

In nginx, all the timers that may be triggered are stored in the data structure such as the red and black trees. Through the red and black trees, you can quickly get the time difference from the current fastest timer time, use the time difference as a parameter for functions such as select/poll/epoll. That is to say, the result can be returned at most after such a long wait. Obtain the total time spent in function calling. Based on this time, retrieve the root node of the red/black tree and compare it to check whether the timer time should be triggered. If yes, delete the timer from the red/black tree, then, check the new timer node that becomes the root of the tree. This process continues, knowing that no timer meets the trigger condition, that is, no trigger event.


In nginx, when a new connection is received, the connection time will be saved and added to the red/black tree timer at this time.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.