概述
Nginx 是以事件的触发来驱动的,事件驱动模型主要包括事件收集、事件发送、事件处理(即事件管理)三部分。在Nginx 的工作进程中主要关注的事件是 IO 网络事件 和 定时器事件。在生成的 objs 目录文件中,其中ngx_modules.c 文件的内容是 Nginx 各种模块的执行顺序,我们可以从该文件的内容中看到事件模块的执行顺序为以下所示:注意:由于是在 Linux 系统下,所以支持具体的 epoll 事件模块,接下来的文章结构按照以下顺序来写。
extern ngx_module_t ngx_events_module;
extern ngx_module_t ngx_event_core_module;
extern ngx_module_t ngx_epoll_module;
事件模块接口
ngx_event_module_t 结构体
在 Nginx 中,结构体 ngx_module_t 是 Nginx 模块最基本的接口。对于每一种不同类型的模块,都有一个具体的结构体来描述这一类模块的通用接口,该接口由ngx_module_t 中的成员 ctx 管理。在 Nginx 中定义了事件模块的通用接口ngx_event_module_t 结构体,该结构体定义在文件src/event/ngx_event.h 中:
/* 事件驱动模型通用接口ngx_event_module_t结构体 */
typedef struct {
/* 事件模块名称 */
ngx_str_t *name;
/* 解析配置项前调用,创建存储配置项参数的结构体 */
void *(*create_conf)(ngx_cycle_t *cycle);
/* 完成配置项解析后调用,处理当前事件模块感兴趣的全部配置 */
char *(*init_conf)(ngx_cycle_t *cycle, void *conf);
/* 每个事件模块具体实现的方法,有10个方法,即IO多路复用模型的统一接口 */
ngx_event_actions_t actions;
} ngx_event_module_t;
在 ngx_event_module_t 结构体中actions 的类型是 ngx_event_actions_t 结构体,该成员结构实现了事件驱动模块的具体方法。该结构体定义在文件 src/event/ngx_event.h 中:
/* IO多路复用模型的统一接口 */
typedef struct {
/* 添加事件,将某个描述符的某个事件添加到事件驱动机制监控描述符集中 */
ngx_int_t (*add)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
/* 删除事件,将某个描述符的某个事件从事件驱动机制监控描述符集中删除 */
ngx_int_t (*del)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
/* 启动对某个指定事件的监控 */
ngx_int_t (*enable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
/* 禁用对某个指定事件的监控 */
ngx_int_t (*disable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
/* 将指定连接所关联的描述符添加到事件驱动机制监控中 */
ngx_int_t (*add_conn)(ngx_connection_t *c);
/* 将指定连接所关联的描述符从事件驱动机制监控中删除 */
ngx_int_t (*del_conn)(ngx_connection_t *c, ngx_uint_t flags);
/* 监控事件是否发生变化,仅用在多线程环境中 */
ngx_int_t (*process_changes)(ngx_cycle_t *cycle, ngx_uint_t nowait);
/* 等待事件的发生,并对事件进行处理 */
ngx_int_t (*process_events)(ngx_cycle_t *cycle, ngx_msec_t timer,
ngx_uint_t flags);
/* 初始化事件驱动模块 */
ngx_int_t (*init)(ngx_cycle_t *cycle, ngx_msec_t timer);
/* 在退出事件驱动模块前调用该函数回收资源 */
void (*done)(ngx_cycle_t *cycle);
} ngx_event_actions_t;
ngx_event_t 结构体
在 Nginx 中,每一个具体事件的定义由结构体ngx_event_t 来表示,该结构体 ngx_event_t 用来保存具体事件。该结构体定义在文件 src/event/ngx_event.h 中:
/* 描述每一个事件的ngx_event_t结构体 */
struct ngx_event_s {
/* 事件相关对象的数据,通常指向ngx_connect_t连接对象 */
void *data;
/* 标志位,为1表示事件可写,即当前对应的TCP连接状态可写 */
unsigned write:1;
/* 标志位,为1表示事件可以建立新连接 */
unsigned accept:1;
/* used to detect the stale events in kqueue, rtsig, and epoll */
unsigned instance:1;
/*
* the event was passed or would be passed to a kernel;
* in aio mode - operation was posted.
*/
/* 标志位,为1表示事件处于活跃状态 */
unsigned active:1;
/* 标志位,为1表示禁用事件 */
unsigned disabled:1;
/* the ready event; in aio mode 0 means that no operation can be posted */
/* 标志位,为1表示当前事件已经准备就绪 */
unsigned ready:1;
/* 该标志位只用于kqueue,eventport模块,对Linux上的驱动模块没有任何意义 */
unsigned oneshot:1;
/* aio operation is complete */
/* 该标志位用于异步AIO事件处理 */
unsigned complete:1;
/* 标志位,为1表示当前处理的字符流已经结束 */
unsigned eof:1;
/* 标志位,为1表示当前事件处理过程中出错 */
unsigned error:1;
/* 标志位,为1表示当前事件已超时 */
unsigned timedout:1;
/* 标志位,为1表示当前事件存在于定时器中 */
unsigned timer_set:1;
/* 标志位,为1表示当前事件需要延迟处理 */
unsigned delayed:1;
/*
* 标志位,为1表示TCP建立需要延迟,即完成建立TCP连接的三次握手后,
* 不会立即建立TCP连接,直到接收到数据包才建立TCP连接;
*/
unsigned deferred_accept:1;
/* the pending eof reported by kqueue, epoll or in aio chain operation */
/* 标志位,为1表示等待字符流结束 */
unsigned pending_eof:1;
/* 标志位,为1表示处理post事件 */
unsigned posted:1;
#if (NGX_WIN32)
/* setsockopt(SO_UPDATE_ACCEPT_CONTEXT) was successful */
unsigned accept_context_updated:1;
#endif
#if (NGX_HAVE_KQUEUE)
unsigned kq_vnode:1;
/* the pending errno reported by kqueue */
int kq_errno;
#endif
/*
* kqueue only:
* accept: number of sockets that wait to be accepted
* read: bytes to read when event is ready
* or lowat when event is set with NGX_LOWAT_EVENT flag
* write: available space in buffer when event is ready
* or lowat when event is set with NGX_LOWAT_EVENT flag
*
* iocp: TODO
*
* otherwise:
* accept: 1 if accept many, 0 otherwise
*/
#if (NGX_HAVE_KQUEUE) || (NGX_HAVE_IOCP)
int available;
#else
/* 标志位,在epoll事件机制中表示一次尽可能多地建立TCP连接 */
unsigned available:1;
#endif
/* 当前事件发生时的处理方法 */
ngx_event_handler_pt handler;
#if (NGX_HAVE_AIO)
#if (NGX_HAVE_IOCP)
ngx_event_ovlp_t ovlp;
#else
/* Linux系统aio机制中定义的结构体 */
struct aiocb aiocb;
#endif
#endif
/* epoll机制不使用该变量 */
ngx_uint_t index;
/* 日志记录 */
ngx_log_t *log;
/* 定时器 */
ngx_rbtree_node_t timer;
/* the posted queue */
ngx_queue_t queue;
/* 标志位,为1表示当前事件已经关闭 */
unsigned closed:1;
/* to test on worker exit */
unsigned channel:1;
unsigned resolver:1;
unsigned cancelable:1;
#if 0
/* the threads support */
/*
* the event thread context, we store it here
* if $(CC) does not understand __thread declaration
* and pthread_getspecific() is too costly
*/
void *thr_ctx;
#if (NGX_EVENT_T_PADDING)
/* event should not cross cache line in SMP */
uint32_t padding[NGX_EVENT_T_PADDING];
#endif
#endif
};
在每个事件结构体 ngx_event_t 最重要的成员是handler 回调函数,该回调函数定义了当事件发生时的处理方法。该回调方法原型在文件src/core/ngx_core.h 中:
typedef void (*ngx_event_handler_pt)(ngx_event_t *ev);
ngx_connection_t 结构体
当客户端向 Nginx 服务器发起连接请求时,此时若Nginx 服务器被动接收该连接,则相对