Nginx timer

WBOY
WBOYOriginal
2016-08-08 09:19:321459browse

Nginx timer

Nginx time management

In order to increase speed, Nginx implements time management by itself, designs data structures and some variables, and stores them in memory. Each process manages the current time independently.

ngx_time_t

<code><span>typedef</span><span>struct</span> {
    <span>//格林威治时间1970年1月1日凌晨0点0分0秒到当前时间的秒数</span>
    time_t          sec;

    <span>//sec成员只能精确到秒,msec则是当前时间相对sec的毫秒偏移量</span>
    ngx_uint_t  msec;

    <span>//时区</span>
    ngx_int_t       gmtoff;
}   ngx_time_t;</code>

ngx_tm_t

<code><span>struct</span> tm {
    <span>int</span> tm_sec; <span>/* 秒–取值区间为[0,59] */</span><span>int</span> tm_min; <span>/* 分 - 取值区间为[0,59] */</span><span>int</span> tm_hour; <span>/* 时 - 取值区间为[0,23] */</span><span>int</span> tm_mday; <span>/* 一个月中的日期 - 取值区间为[1,31] */</span><span>int</span> tm_mon; <span>/* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */</span><span>int</span> tm_year; <span>/* 年份,其值从1900开始 */</span><span>int</span> tm_wday; <span>/* 星期–取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */</span><span>int</span> tm_yday;    <span>/* 从每年的1月1日开始的天数 - 取值区间为[0,365],其中0代表1月1日,1代表1月2日,一次类推 */</span><span>int</span>  tm_isdst;  <span>/* 夏令时标识符。在实行夏令时的时候,tm_isdst为正;不实行夏令时的时候tm_isdst为0;否则为负 */</span>
};

typedef <span>struct</span> tm   ngx_tm_t;

<span>#<span>define</span> ngx_tm_sec            tm_sec</span><span>#<span>define</span> ngx_tm_min            tm_min</span><span>#<span>define</span> ngx_tm_hour           tm_hour</span><span>#<span>define</span> ngx_tm_mday           tm_mday</span><span>#<span>define</span> ngx_tm_mon            tm_mon</span><span>#<span>define</span> ngx_tm_year           tm_year</span><span>#<span>define</span> ngx_tm_wday           tm_wday</span><span>#<span>define</span> ngx_tm_isdst          tm_isdst</span></code>

From the above definition, it can be seen that the ngx_tm_t and struct tm structures are exactly the same.

Nginx cache time operation function

Function name Parameter meaning Execution meaning
void ngx_time_init(void); Initialize the time variable cached in the current process
void ngx_time_update(void) Use gettimeofday call to update the cached time with system time
u_char *ngx_http_time(u_char *buf,time_t t) t is the time that needs to be converted (from Greenwich Mean Time seconds), buf is the memory used to store the string after being converted to HTTP time Convert t to the form of "Mon,28 Sep 1970 06:00:00 GMT"
u_char *ngx_http_cookie_time(u_char *buf, time_t t) Same as above t is the time that needs to be converted (seconds from Greenwich Mean Time), buf is the memory used to store the string after converting to HTTP time
void ngx_gmtime(time_t t,ngx_tm_t *tp fill t with ngx_tm_t
time_t ngx_next_time(time_t when) when represents the expected expiration time, which only represents the number of seconds in a day Failure returns -1; otherwise returns: 1. If when is merged If it does not time out after reaching the current time, then return the number of seconds to Greenwich Mean Time; 2. If it times out, then return the number of seconds to Greenwich Mean Time at the same moment the next day
define ngx_time() ngx_cache_time- >sec Get the number of seconds from Greenwich Mean Time to the current time
define ngx_timeofday() (ngx_time_t *)ngx_cached_time Get the cached ngx_time_t type time

implementation of timer

The structure of the timer is a red-black tree. ngx_event_timer_rbtree is a red-black tree composed of all timers, and ngx_event_timer_sentinel is the sentinel node of this red-black tree. The key of this red-black tree is the timeout of the event. Therefore, the node in the lower left corner is the most likely event to time out.

Timer operation function

Function name Parameter meaning Execution meaning
ngx_int_t ngx_event_timer_init(ngx_log_t * log); Log object Initialization timer
ngx_msec_t ngx_event_find_timer(void); Find the node in the lower left corner of the red-black tree. If it is larger than the current time, it means there is no ready event. Return the distance "Timeout time"; otherwise, return 0, indicating that an event is ready
void ngx_event_expire_timers(void); Check all events in the red-black tree and call the handler callback function of all ready events
static ngx_inline void ngx_event_del_timer(ngx_event_t *ev); ev is an event that needs to be operated Delete an event from the timer
static ngx_inline void ngx_event_add_time(ngx_event_t *ev,ngx_msec_t timer) timer unit is milliseconds , it tells the timer event ev wants timeout after timer milliseconds Add a timer event with a timeout of timer milliseconds

Copyright Statement: Pain is just in your mind.

The above has introduced the Nginx timer, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn