search
HomeBackend DevelopmentPHP Tutorialnginx source code study notes (21) - event module 2 - event-driven core ngx_process_events_and_timers

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
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
解决方法:您的组织要求您更改 PIN 码解决方法:您的组织要求您更改 PIN 码Oct 04, 2023 pm 05:45 PM

“你的组织要求你更改PIN消息”将显示在登录屏幕上。当在使用基于组织的帐户设置的电脑上达到PIN过期限制时,就会发生这种情况,在该电脑上,他们可以控制个人设备。但是,如果您使用个人帐户设置了Windows,则理想情况下不应显示错误消息。虽然情况并非总是如此。大多数遇到错误的用户使用个人帐户报告。为什么我的组织要求我在Windows11上更改我的PIN?可能是您的帐户与组织相关联,您的主要方法应该是验证这一点。联系域管理员会有所帮助!此外,配置错误的本地策略设置或不正确的注册表项也可能导致错误。即

Windows 11 上调整窗口边框设置的方法:更改颜色和大小Windows 11 上调整窗口边框设置的方法:更改颜色和大小Sep 22, 2023 am 11:37 AM

Windows11将清新优雅的设计带到了最前沿;现代界面允许您个性化和更改最精细的细节,例如窗口边框。在本指南中,我们将讨论分步说明,以帮助您在Windows操作系统中创建反映您的风格的环境。如何更改窗口边框设置?按+打开“设置”应用。WindowsI转到个性化,然后单击颜色设置。颜色更改窗口边框设置窗口11“宽度=”643“高度=”500“>找到在标题栏和窗口边框上显示强调色选项,然后切换它旁边的开关。若要在“开始”菜单和任务栏上显示主题色,请打开“在开始”菜单和任务栏上显示主题

如何在 Windows 11 上更改标题栏颜色?如何在 Windows 11 上更改标题栏颜色?Sep 14, 2023 pm 03:33 PM

默认情况下,Windows11上的标题栏颜色取决于您选择的深色/浅色主题。但是,您可以将其更改为所需的任何颜色。在本指南中,我们将讨论三种方法的分步说明,以更改它并个性化您的桌面体验,使其具有视觉吸引力。是否可以更改活动和非活动窗口的标题栏颜色?是的,您可以使用“设置”应用更改活动窗口的标题栏颜色,也可以使用注册表编辑器更改非活动窗口的标题栏颜色。若要了解这些步骤,请转到下一部分。如何在Windows11中更改标题栏的颜色?1.使用“设置”应用按+打开设置窗口。WindowsI前往“个性化”,然

OOBELANGUAGE错误Windows 11 / 10修复中出现问题的问题OOBELANGUAGE错误Windows 11 / 10修复中出现问题的问题Jul 16, 2023 pm 03:29 PM

您是否在Windows安装程序页面上看到“出现问题”以及“OOBELANGUAGE”语句?Windows的安装有时会因此类错误而停止。OOBE表示开箱即用的体验。正如错误提示所表示的那样,这是与OOBE语言选择相关的问题。没有什么可担心的,你可以通过OOBE屏幕本身的漂亮注册表编辑来解决这个问题。快速修复–1.单击OOBE应用底部的“重试”按钮。这将继续进行该过程,而不会再打嗝。2.使用电源按钮强制关闭系统。系统重新启动后,OOBE应继续。3.断开系统与互联网的连接。在脱机模式下完成OOBE的所

Windows 11 上启用或禁用任务栏缩略图预览的方法Windows 11 上启用或禁用任务栏缩略图预览的方法Sep 15, 2023 pm 03:57 PM

任务栏缩略图可能很有趣,但它们也可能分散注意力或烦人。考虑到您将鼠标悬停在该区域的频率,您可能无意中关闭了重要窗口几次。另一个缺点是它使用更多的系统资源,因此,如果您一直在寻找一种提高资源效率的方法,我们将向您展示如何禁用它。不过,如果您的硬件规格可以处理它并且您喜欢预览版,则可以启用它。如何在Windows11中启用任务栏缩略图预览?1.使用“设置”应用点击键并单击设置。Windows单击系统,然后选择关于。点击高级系统设置。导航到“高级”选项卡,然后选择“性能”下的“设置”。在“视觉效果”选

Windows 11 上的显示缩放比例调整指南Windows 11 上的显示缩放比例调整指南Sep 19, 2023 pm 06:45 PM

在Windows11上的显示缩放方面,我们都有不同的偏好。有些人喜欢大图标,有些人喜欢小图标。但是,我们都同意拥有正确的缩放比例很重要。字体缩放不良或图像过度缩放可能是工作时真正的生产力杀手,因此您需要知道如何对其进行自定义以充分利用系统功能。自定义缩放的优点:对于难以阅读屏幕上的文本的人来说,这是一个有用的功能。它可以帮助您一次在屏幕上查看更多内容。您可以创建仅适用于某些监视器和应用程序的自定义扩展配置文件。可以帮助提高低端硬件的性能。它使您可以更好地控制屏幕上的内容。如何在Windows11

10种在 Windows 11 上调整亮度的方法10种在 Windows 11 上调整亮度的方法Dec 18, 2023 pm 02:21 PM

屏幕亮度是使用现代计算设备不可或缺的一部分,尤其是当您长时间注视屏幕时。它可以帮助您减轻眼睛疲劳,提高易读性,并轻松有效地查看内容。但是,根据您的设置,有时很难管理亮度,尤其是在具有新UI更改的Windows11上。如果您在调整亮度时遇到问题,以下是在Windows11上管理亮度的所有方法。如何在Windows11上更改亮度[10种方式解释]单显示器用户可以使用以下方法在Windows11上调整亮度。这包括使用单个显示器的台式机系统以及笔记本电脑。让我们开始吧。方法1:使用操作中心操作中心是访问

如何修复Windows服务器中的激活错误代码0xc004f069如何修复Windows服务器中的激活错误代码0xc004f069Jul 22, 2023 am 09:49 AM

Windows上的激活过程有时会突然转向显示包含此错误代码0xc004f069的错误消息。虽然激活过程已经联机,但一些运行WindowsServer的旧系统可能会遇到此问题。通过这些初步检查,如果这些检查不能帮助您激活系统,请跳转到主要解决方案以解决问题。解决方法–关闭错误消息和激活窗口。然后,重新启动计算机。再次从头开始重试Windows激活过程。修复1–从终端激活从cmd终端激活WindowsServerEdition系统。阶段–1检查Windows服务器版本您必须检查您使用的是哪种类型的W

See all articles

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

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.