Home Backend Development PHP Tutorial nginx source code study notes (21) - event module 2 - event-driven core ngx_process_events_and_timers

nginx source code study notes (21) - event module 2 - event-driven core ngx_process_events_and_timers

Jul 29, 2016 am 09:11 AM
event gt nbsp

First of all, let’s continue to recall that there is an uninvolved content ngx_process_events_and_timers in the previous sub-thread execution operation. Today we will study this function.

This article comes from: http://blog.csdn.net/lengzijian/article/details/7601730

First, let’s take a look at some screenshots of Section 19:

nginx 源码学习笔记(二十一)—— event 模块二 ——事件驱动核心ngx_process_events_and_timers

Today we mainly explain the event-driven function, the red part in the picture:

[cpp] view plaincopyprint?

  1. src/event/ngx_event.c
  2. void
  3. ngx_process_events_and_timers(ngx _cycle_t *cycle)
  4. {
  5. ngx_uint_t flags;
  6. ngx_msec_t timer, delta;
  7. (ngx_timer_resolution) time r = NGX_TIMER_INFINITE; flags = 0;
  8. } else
  9. {
  10. timer = ngx_event_find_timer(); flags = NGX_UPDATE_TIME;
  11. }
  12. /*
  13. The ngx_use_accept_mutex variable represents whether to use accept The mutex
  14. is used by default and can be turned off through the accept_mutex off; command; */
  15.                                                                                                             
  16.                                   ngx_accept_disabled variable is calculated in the ngx_event_accept function.
  17. If ngx_accept_disabled is greater than 0, it means that the process accepts too many links, Therefore, it gives up an opportunity to compete for the accept mutex and reduces itself by one.
  18. Then, continue to process events on existing connections.
  19. nginx takes advantage of this to implement basic load balancing of inherited connections.
  20.                                                                          
  21.                 ngx_accept_disabled--; /*
  22. Try to lock Accept Mutex. Only by successfully obtaining the lock can the Listen put word in EPOLL.
  23. Therefore, this ensures that only one process has the listening socket, so when all processes are blocked in epoll_wait,
  24. will not cause a group panic.​​​​
  25.                                                                                                                                                                                                                                                             If the process acquires the lock, a NGX_POST_EVENTS flag will be added.
  26. The function of this flag is to put all generated events into a queue, and then process the events slowly after they are released. Because the processing time may be time -consuming, if the lock is not applied first before processing, the process will occupy the lock for a long time. The efficiency is low.
  27.                                                                                                            
  28. }
  29. else{
  30. Not obtained The resulting process, of course, does not need the NGX_POST_EVENTS flag.
  31. But you need to set the delay time before fighting for the lock.
  32. */
  33. (timer == ngx_timer_infinite
  34. || timer & gt; ngx_accept_mutex_dlay) {
  35. timer = ngx_acceth_mutex_delay;
  36.                                                                                                 
  37. delta = ngx_current_msec;
  38. /*Next, epoll will start the wait event ,
  39. The specific implementation of ngx_process_events corresponds to the ngx_epoll_process_events function in the epoll module
  40. Will be explained in detail here later
  41.  */
  42.  ( void
  43. ) ngx_process_events(cycle, timer, flags);
  44. / / Statistics of the time consumption of this wait event
  45. delta = ngx_current_msec-delta;
  46. ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
  47.                                           
  48. /*
  49. ngx_posted_accept_events is An event queue
  50. , which temporarily stores the accept event epoll waits from the listening socket.
  51. After the NGX_POST_EVENTS flag mentioned above is used, all accept events will be temporarily stored in this queue
  52. */
  53. if
  54. (ngx_posted_accept_events ) { ngx_event_process_posted(cycle, &ngx_posted_accept_events);
  55. }
  56. //After all accept events are processed, If the lock is held, release it.
  57. if (ngx_accept_mutex_held) {
  58. ngx_shmtx_unlock(&ngx_accept_mutex);
  59. }
  60. /*
  61. delta is before For statistical time-consuming, if there is millisecond-level time-consuming, check the timers of all times.
  62. If it is timeout, delete the expired timer from the time rbtree, and call the handler function of the corresponding event to process
  63. */
  64. if (delta) {
  65. ngx_event_expire_timers();
  66. }
  67. ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle-> ;log, 0,
  68.                                                                                              /*
  69. Processing ordinary events (read and write data obtained on the connection event),
  70. Because each event has its own handler method,
  71. */
  72. if
  73. (ngx_posted_events) {
  74. (ngx_threaded) {
  75.              ngx_wakeup_worker_thread(cycle);                                              : }
  76. }
  77. I have talked about the accept event before. In fact, it is to monitor whether there are new events on the socket. Here is the handler method of the accept time:
  78. ngx_event_accept:
  79. [cpp] view plaincopyprint?
    1. src/event/ngx_event_accept.c  
    2.   
    3. void  
    4. ngx_event_accept(ngx_event_t *ev)  
    5. {  
    6.     socklen_t          socklen;  
    7.     ngx_err_t          err;  
    8.     ngx_log_t         *log;  
    9.     ngx_socket_t       s;  
    10.     ngx_event_t       *rev, *wev;  
    11.     ngx_listening_t   *ls;  
    12.     ngx_connection_t  *c, *lc;  
    13.     ngx_event_conf_t  *ecf;  
    14.     u_char             sa[NGX_SOCKADDRLEN];  
    15.       
    16.     //省略部分代码  
    17.   
    18.     lc = ev->data;  
    19.     ls = lc->listening;  
    20.     ev->ready = 0;  
    21.   
    22.     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,  
    23.                    "accept on %V, ready: %d", &ls->addr_text, ev->available);  
    24.   
    25.     do {  
    26.         socklen = NGX_SOCKADDRLEN;  
    27.         //accept一个新的连接  
    28.         s = accept(lc->fd, (struct sockaddr *) sa, &socklen);  
    29.         //省略部分代码  
    30.           
    31.         /* 
    32.         accept到一个新的连接后,就重新计算ngx_accept_disabled的值, 
    33.         它主要是用来做负载均衡,之前有提过。 
    34.          
    35.         这里,我们可以看到他的就只方式 
    36.         “总连接数的八分之一   -   剩余的连接数“ 
    37.         总连接指每个进程设定的最大连接数,这个数字可以再配置文件中指定。After the 7/8 of the total number of connections to the total number of connections, the NGX_ACCEPT_DISABLED is greater than zero, and the connection is overloaded
    38. */
    39. ngx_accept_disabled = ngx_cycle->connection_n / 8
    40. - ngx_cycle->free_connection_n;
    41. c = ngx_get_connection(s, ev->log);
    42. //Only released when the connection is closed pool
    43. if (c->pool == NULL ) {
    44. ngx_close_accepted_connection(c);
    45.                                                                                    );                                                                                                                                                                                
    46. ngx_memcpy(c->sockaddr, sa, socklen);
    47. log = ngx_palloc(c->pool, sizeof
    48. (ngx_log_t));
    49. if (log == null) { ngx_close_accepted_connection (c);
    50. Roturn
    51. ; }  
    52.   
    53.         /* set a blocking mode for aio and non-blocking mode for others */  
    54.   
    55.         if (ngx_inherited_nonblocking) {  
    56.             if (ngx_event_flags & NGX_USE_AIO_EVENT) {  
    57.                 if (ngx_blocking(s) == -1) {  
    58.                     ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,  
    59.                                   ngx_blocking_n " failed");  
    60.                     ngx_close_accepted_connection(c);  
    61.                     return;  
    62.                 }  
    63.             }  
    64.   
    65.         } else {  
    66.             //我们使用epoll模型,这里我们设置连接为nonblocking  
    67.             if (!(ngx_event_flags & (NGX_USE_AIO_EVENT|NGX_USE_RTSIG_EVENT))) {  
    68.                 if (ngx_nonblocking(s) == -1) {  
    69.                     ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,  
    70.                                   ngx_nonblocking_n " failed");  
    71.                     ngx_close_accepted_connection(c);  
    72.                     return;  
    73.                 }  
    74.             }  
    75.         }  
    76.   
    77.         *log = ls->log;  
    78.         //初始化新的连接  
    79.         c->recv = ngx_recv;  
    80.         c->send = ngx_send;  
    81.         c->recv_chain = ngx_recv_chain;  
    82.         c->send_chain = ngx_send_chain;  
    83.   
    84.         c->log = log;  
    85.         c->pool->log = log;  
    86.   
    87.         c->socklen = socklen;  
    88.         c->listening = ls;  
    89.         c->local_sockaddr = ls->sockaddr;  
    90.   
    91.         c->unexpected_eof = 1;  
    92.   
    93. #if (NGX_HAVE_UNIX_DOMAIN)  
    94.         if (c->sockaddr->sa_family == AF_UNIX) {  
    95.             c->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;  
    96.             c->tcp_nodelay = NGX_TCP_NODELAY_DISABLED;  
    97. #if (NGX_SOLARIS)  
    98.             /* Solaris's sendfilev() supports AF_NCA, AF_INET, and AF_INET6 */  
    99.             c->sendfile = 0;  
    100. #endif  
    101.         }  
    102. #endif  
    103.   
    104.         rev = c->read;  
    105.         wev = c->write;  
    106.   
    107.         wev->ready = 1;  
    108.   
    109.         if (ngx_event_flags & (NGX_USE_AIO_EVENT|NGX_USE_RTSIG_EVENT)) {  
    110.             /* rtsig, aio, iocp */  
    111.             rev->ready = 1;  
    112.         }  
    113.   
    114.         if (ev->deferred_accept) {  
    115.             rev->ready = 1;  
    116. #if (NGX_HAVE_KQUEUE)  
    117.             rev->available = 1;  
    118. #endif  
    119.         }  
    120.   
    121.         rev->log = log;  
    122.         wev->log = log;  
    123.   
    124.         /* 
    125.          * TODO: MT: - ngx_atomic_fetch_add() 
    126.          *             or protection by critical section or light mutex 
    127.          * 
    128.          * TODO: MP: - allocated in a shared memory 
    129.          *           - ngx_atomic_fetch_add() 
    130.          *             or protection by critical section or light mutex 
    131.          */  
    132.   
    133.         c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);  
    134.           
    135.         if (ngx_add_conn && (ngx_event_flags & NGX_USE_EPOLL_EVENT) == 0) {  
    136.             if (ngx_add_conn(c) == NGX_ERROR) {  
    137.                 ngx_close_accepted_connection(c);  
    138.                 return;  
    139.             }  
    140.         }  
    141.   
    142.         log->data = NULL;  
    143.         log->handler = NULL;  
    144.           
    145.         /* 
    146.         这里listen handler很重要,它将完成新连接的最后初始化工作, 
    147.         同时将accept到的新的连接放入epoll中;挂在这个handler上的函数, 
    148.         就是ngx_http_init_connection 在之后http模块中在详细介绍 
    149.         */  
    150.         ls->handler(c);  
    151.   
    152.         if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {  
    153.             ev->available--;  
    154.         }  
    155.   
    156.     } while (ev->available);  
    157. }  

    accpt事件的handler方法也就是如此了。之后就是每个连接的读写事件handler方法,这一部分会直接将我们引入http模块,我们还不急,还要学习下nginx经典模块epoll。

    The above introduces the nginx source code study notes (21) - event module 2 - the event-driven core ngx_process_events_and_timers, including queue content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

See all articles