Nginx学习(18)—事件处理框架

来源:互联网 发布:高性能mysql在线阅读 编辑:程序博客网 时间:2024/05/18 11:47

事件处理框架

Nginx事件处理框架主要围绕两种事件进行,分别是是网络事件以及定时器事件。其中,网络事件主要就是以TCP事件为主了。Nginx的高可移植性决定了它能够在不同的操作系统内核中选择符合的事件驱动机制支持网络事件的处理。Nginx支持的事件驱动机制可以看《笔记十一》中的模块间关系图。

那Nginx的事件处理框架又是如何选择事件驱动机制的呢?
  1. Nginx定义了一个核心模块ngx_events_module,在Nginx启动时调用ngx_init_cycle方法解析配置项,找到"events{}"配置项后,ngx_events_module模块即开始工作。它的主要工作就是为所有的事件模块解析"events{}"中的配置项,同时管理这些事件模块存储配置项的结构体;
  2. Nginx定义了一个非常重要的事件模块ngx_event_core_module,该模块决定使用哪种事件驱动机制,以及如何管理事件(后续笔记详细介绍);
  3. Nginx定义了一系列运行在不同操作系统、不同内核版本上的事件驱动机制,包括:ngx_epoll_module、ngx_kqueue_module、ngx_poll_module、ngx_select_module、ngx_devpoll_module、ngx_eventport_module、ngx_aio_module、ngx_rtsig_module和基于Windows的ngx_kqueue_module模块。在ngx_event_core_module模块中,将会从以上9个模块中选择1个作为Nginx进程的事件驱动模块。

事件模块结构

事件模块是一种新的模块类型,它的通用接口是ngx_event_module_t结构体:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. typedef struct {  
  2.     ngx_str_t              *name;  // 事件模块名称  
  3.   
  4.     /* 创建存储events{}中配置项的结构体 */  
  5.     void                 *(*create_conf)(ngx_cycle_t *cycle);  
  6.       
  7.     /* 初始化操作,用以处理事件模块感兴趣的配置项 */         
  8.     char                 *(*init_conf)(ngx_cycle_t *cycle, void *conf);  
  9.   
  10.     /* 重要!每个事件模块需要实现10种抽象方法 */  
  11.     ngx_event_actions_t     actions;  
  12. } ngx_event_module_t;  
下面是ngx_evnet_actions_t的实现:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. typedef struct {  
  2.     /* 添加/删除事件,负责在事件驱动机制里(如epoll中)添加/删除 感兴趣的事件 */  
  3.     ngx_int_t  (*add)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);  
  4.     ngx_int_t  (*del)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);  
  5.   
  6.     /* 启动/禁用事件,目前不会调用此2种方法,代码中实现方式跟添加/删除一致 */  
  7.     ngx_int_t  (*enable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);  
  8.     ngx_int_t  (*disable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);  
  9.   
  10.     /* 添加/删除新的连接,复杂在事件驱动机制里(如epoll)添加/删除新的连接, 
  11.      * 这意味着新连接上的读写事件都将被事件驱动关注 
  12.       */  
  13.     ngx_int_t  (*add_conn)(ngx_connection_t *c);  
  14.     ngx_int_t  (*del_conn)(ngx_connection_t *c, ngx_uint_t flags);  
  15.   
  16.     /* 具体的事件处理方法 */  
  17.     ngx_int_t  (*process_changes)(ngx_cycle_t *cycle, ngx_uint_t nowait);  
  18.     ngx_int_t  (*process_events)(ngx_cycle_t *cycle, ngx_msec_t timer,  
  19.                    ngx_uint_t flags);  
  20.                      
  21.     /* 事件驱动模块的初始化及结束方法 */  
  22.     ngx_int_t  (*init)(ngx_cycle_t *cycle, ngx_msec_t timer);  
  23.     void       (*done)(ngx_cycle_t *cycle);  
  24. } ngx_event_actions_t;  

Nginx事件定义

说实话,这种大型的结构体成员如果不是亲自去实现,我也不知道怎么学习。这种摘抄式的笔记,我也不知道用处有多大,抄一遍加深下印象?目前看,就这种意思吧。
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. struct ngx_event_s {  
  2.     /* 事件相关的对象,通常指向ngx_connection_t连接对象 */  
  3.     void            *data;  
  4.   
  5.     /* 标志位,标识事件可写,意味着对应的TCP连接可写,也即连接处于发送网络包状态 */  
  6.     unsigned         write:1;  
  7.       
  8.     /* 标志位,标识可建立新的连接,一般是在ngx_listening_t对应的读事件中标记 */  
  9.     unsigned         accept:1;  
  10.   
  11.     /* used to detect the stale events in kqueue, rtsig, and epoll */  
  12.     /* 用于区分当前事件是否过期,这涉及到一个很好的小技巧,后续会专门介绍 */  
  13.     unsigned         instance:1;  
  14.   
  15.     /* 
  16.      * the event was passed or would be passed to a kernel; 
  17.      * in aio mode - operation was posted. 
  18.      */  
  19.     /* 标志位,标识当前事件是否活跃 */  
  20.     unsigned         active:1;  
  21.   
  22.     /* 标志位,标识当前事件是否禁用 */  
  23.     unsigned         disabled:1;  
  24.   
  25.     /* the ready event; in aio mode 0 means that no operation can be posted */  
  26.     /* 标志位,标识当前事件是否准备好,为1时表示可以被相应消费模块进行处理 */  
  27.     unsigned         ready:1;  
  28.   
  29.     /* 对epoll来说无意义,不笔记,我也不懂... */  
  30.     unsigned         oneshot:1;  
  31.   
  32.     /* aio operation is complete */  
  33.     /* 标志位,用于AIO异步事件的处理 */  
  34.     unsigned         complete:1;  
  35.   
  36.     unsigned         eof:1;        // 当前处理的字符流结束符  
  37.     unsigned         error:1;      // 事件处理错误  
  38.   
  39.     unsigned         timedout:1;   // 超时标识  
  40.     unsigned         timer_set:1;  // 标志该事件处于定时器中  
  41.   
  42.     unsigned         delayed:1;    // 需要延迟处理事件  
  43.   
  44.     unsigned         deferred_accept:1;  // 未使用  
  45.   
  46.     /* the pending eof reported by kqueue, epoll or in aio chain operation */  
  47.     unsigned         pending_eof:1;    
  48.   
  49. #if !(NGX_THREADS)  
  50.     unsigned         posted_ready:1;  // 表示处理post事件时,当前事件准备就绪  
  51. #endif  
  52.   
  53. #if (NGX_WIN32)  
  54.     /* setsockopt(SO_UPDATE_ACCEPT_CONTEXT) was successful */  
  55.     unsigned         accept_context_updated:1;  
  56. #endif  
  57.   
  58. #if (NGX_HAVE_KQUEUE)  
  59.     unsigned         kq_vnode:1;  
  60.   
  61.     /* the pending errno reported by kqueue */  
  62.     int              kq_errno;  
  63. #endif  
  64.   
  65.     /* 
  66.      * kqueue only: 
  67.      *   accept:     number of sockets that wait to be accepted 
  68.      *   read:       bytes to read when event is ready 
  69.      *               or lowat when event is set with NGX_LOWAT_EVENT flag 
  70.      *   write:      available space in buffer when event is ready 
  71.      *               or lowat when event is set with NGX_LOWAT_EVENT flag 
  72.      * 
  73.      * iocp: TODO 
  74.      * 
  75.      * otherwise: 
  76.      *   accept:     1 if accept many, 0 otherwise 
  77.      */  
  78.   
  79. #if (NGX_HAVE_KQUEUE) || (NGX_HAVE_IOCP)  
  80.     int              available;  
  81. #else  
  82.     unsigned         available:1;  // 在epoll机制下,标识一次尽可能多的建立TCP连接  
  83. #endif  
  84.   
  85.     /* 事件发生时的处理函数,每个事件消费模块都会重新实现它 */  
  86.     ngx_event_handler_pt  handler;  
  87.   
  88.   
  89. #if (NGX_HAVE_AIO)  
  90.   
  91. #if (NGX_HAVE_IOCP)  
  92.     ngx_event_ovlp_t ovlp;  
  93. #else  
  94.     struct aiocb     aiocb;  
  95. #endif  
  96.   
  97. #endif  
  98.   
  99.     ngx_uint_t       index;  
  100.   
  101.     ngx_log_t       *log;  
  102.   
  103.     ngx_rbtree_node_t   timer;    // 定时器节点,用于定时器红黑树中  
  104.   
  105.     unsigned         closed:1;  
  106.   
  107.     /* to test on worker exit */  
  108.     unsigned         channel:1;  
  109.     unsigned         resolver:1;  
  110.   
  111. #if (NGX_THREADS)  
  112.   
  113.     unsigned         locked:1;  
  114.   
  115.     unsigned         posted_ready:1;  
  116.     unsigned         posted_timedout:1;  
  117.     unsigned         posted_eof:1;  
  118.   
  119. #if (NGX_HAVE_KQUEUE)  
  120.     /* the pending errno reported by kqueue */  
  121.     int              posted_errno;  
  122. #endif  
  123.   
  124. #if (NGX_HAVE_KQUEUE) || (NGX_HAVE_IOCP)  
  125.     int              posted_available;  
  126. #else  
  127.     unsigned         posted_available:1;  
  128. #endif  
  129.   
  130.     ngx_atomic_t    *lock;  
  131.     ngx_atomic_t    *own_lock;  
  132.   
  133. #endif  
  134.   
  135.     /* the links of the posted queue */  
  136.     /* post事件双向链表 */  
  137.     ngx_event_t     *next;  
  138.     ngx_event_t    **prev;  
  139.   
  140.   
  141. #if 0  
  142.   
  143.     /* the threads support */  
  144.   
  145.     /* 
  146.      * the event thread context, we store it here 
  147.      * if $(CC) does not understand __thread declaration 
  148.      * and pthread_getspecific() is too costly 
  149.      */  
  150.   
  151.     void            *thr_ctx;  
  152.   
  153. #if (NGX_EVENT_T_PADDING)  
  154.   
  155.     /* event should not cross cache line in SMP */  
  156.   
  157.     uint32_t         padding[NGX_EVENT_T_PADDING];  
  158. #endif  
  159. #endif  
  160. }  
每一个事件最核心的部分就是handler回调方法,它将由每一个事件消费模块实现,以此决定这个事件究竟如何“消费”。原型如下:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. typedef void (*ngx_event_handler_pt)(ngx_event_t *ev);  
所有Nginx模块只要进行事件处理就必然要设置handler回调方法。

那么如何Nginx是如何往事件驱动模块中添加/删除感兴趣的事件呢?
答案:
  1. Nginx封装了两个简单的方法用于在事件驱动模块中添加或删除事件;
  2. 调用上面提到的ngx_event_actions_t中的add或del抽象方法;(书中表示不推荐)
现在可以看看Nginx封装的两个方法:
  • ngx_int_t ngx_handle_read_event(ngx_event_t *rev, ngx_uint_t flags);
该方法负责将读事件添加到事件驱动模块中,rev是要操作的事件,flags指定事件的驱动方式。以epoll驱动机制为例,flags取值为0或者NGX_CLOSE_EVENT,Nginx主要工作在ET模式下,一般可以忽略flags参数。返回值NGX_OK或NGX_ERROR。
  • ngx_int_t ngx_handle_write_event(ngx_event_t *wev, size_t lowat);
同上,该方法表示将写事件添加到事件驱动模块中,wev表示要操作的事件,lowat表示只有当连接对应的套接字缓冲区必须有lowat大小的可用空间时,才能处理这个可写事件。

另外,关于ngx_event_actions_t中的事件设置方法,直接使用时都会与具体的事件驱动机制强相关,使用上述两种封装方法可以屏蔽不同机制的差异。
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #define ngx_add_event        ngx_event_actions.add  
  2. #define ngx_del_event        ngx_event_actions.del  
  3. #define ngx_add_conn         ngx_event_actions.add_conn  
  4. #define ngx_del_conn         ngx_event_actions.del_conn  

总结

这个基本属于对事件驱动机制的介绍吧。之前将事件模块章看了一遍,现在在重头摘抄做下笔记,加深印象吧。
0 0