Nginx (九)  事件

结构

struct ngx_event_s {

void            *data;

unsigned         write: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.

*/

unsigned         active:1;

unsigned         disabled:1;

/* the ready event; in aio mode 0 means that no operation can be posted */

unsigned         ready:1;

unsigned         oneshot:1;

/* aio operation is complete */

unsigned         complete:1;

unsigned         eof:1;

unsigned         error:1;

unsigned         timedout:1;

unsigned         timer_set:1;

unsigned         delayed:1;

unsigned         read_discarded:1;

unsigned         unexpected_eof:1;

unsigned         deferred_accept:1;

/* the pending eof reported by kqueue or in aio chain operation */

unsigned         pending_eof:1;

#if !(NGX_THREADS)

unsigned         posted_ready:1;

#endif

#if (NGX_WIN32)

/* setsockopt(SO_UPDATE_ACCEPT_CONTEXT) was succesfull */

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

unsigned         available:1;

#endif

ngx_event_handler_pt  handler;

#if (NGX_HAVE_AIO)

#if (NGX_HAVE_IOCP)

ngx_event_ovlp_t ovlp;

#else

struct aiocb     aiocb;

#endif

#endif

ngx_uint_t       index;

ngx_log_t       *log;

ngx_rbtree_node_t   timer;

unsigned         closed:1;

/* to test on worker exit */

unsigned         channel:1;

unsigned         resolver:1;

#if (NGX_THREADS)

unsigned         locked:1;

unsigned         posted_ready:1;

unsigned         posted_timedout:1;

unsigned         posted_eof:1;

#if (NGX_HAVE_KQUEUE)

/* the pending errno reported by kqueue */

int              posted_errno;

#endif

#if (NGX_HAVE_KQUEUE) || (NGX_HAVE_IOCP)

int              posted_available;

#else

unsigned         posted_available:1;

#endif

ngx_atomic_t    *lock;

ngx_atomic_t    *own_lock;

#endif

/* the links of the posted queue */

ngx_event_t     *next;

ngx_event_t    **prev;

#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

};

typedef struct {

ngx_uint_t       lock;

ngx_event_t     *events;

ngx_event_t     *last;

} ngx_event_mutex_t;

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;

相关函数

具体以epoll事件模块为例。其中ngx_epoll_process_events()函数在前面工作进程的逻辑分析部份已经有很清楚的伪代码说明。

ngx_epoll_init()函数。这个函数中的NGX_HAVE_FILE_AIO宏有效部份中包括起来的那部份代码,暂死放在一边;因为他对我们理解epoll事件模块没有影响,过份关心反而不利于理解。

剩下的代码就好理解了,首先取得ngx_conf_t对象。根据cycle中的最大的connection对象数的一半创建epoll用于监听的epoll socket。

接着根据ngx_conf_t对象中的events(整数)创建epoll监听用的event_list。

后面三行很容易理解。

nevents = epcf->events;

ngx_io = ngx_os_io;

ngx_event_actions = ngx_epoll_module_ctx.actions;

最后设定ngx_event_flags标志。

#if (NGX_HAVE_CLEAR_EVENT)

ngx_event_flags = NGX_USE_CLEAR_EVENT

#else

ngx_event_flags = NGX_USE_LEVEL_EVENT

#endif

|NGX_USE_GREEDY_EVENT

|NGX_USE_EPOLL_EVENT;

返回 NGX_OK。

ngx_epoll_done()函数。同样也不关心NGX_HAVE_FILE_AIO宏包括的代码。

于是这个函数就只是关闭epoll监听的socket;释放event_list。

ngx_epoll_add_event()函数。

1. 从待添加的ev对象(ngx_event_t)中取出相关c对象(ngx_connection_t);

2.如果要添加NGX_READ_EVENT类型的ev对象;

那么看c对象中的write事件对象是否是active状态;

active状态下用EPOLL_CTL_MOD方式调用epoll_ctl()加到epoll监听的事件中;

如果是NGX_WRITE_EVENT类型的ev对象;c对象中read事件是active状态;

通样用EPOLL_CTL_MOD方式调用epoll_ctl()加到epoll监听的事件中;

不是上述情况,那就仅仅用EPOLL_CTL_ADD方式调用epoll_ctl()加到epoll监听的事件中;

(nginx中一个ngx_connection_t对象含有write和read两个事件对象,分别代表了读、写事件。同时ngx_event_t对象中有个data成员指向与之相关的ngx_connection_t对象。

所以在这个函数中实际上是对一个ngx_connection_t对象操作,待添加的ev对象只可能是某个ngx_connection_t对象中的一个。如果说这个ngx_connection_t对象中write和read事件都没有激活,只需注册目前这个就ok了;另一种可能就是如添加一个read类型事件时,原来write事件要保留下来,因此用EPOLL_CTL_MOD方式调用epoll_ctl()加到epoll监听的事件中。)

1.       最后将在epoll监听事件后,将ev对象激活。

ngx_epoll_del_event()函数。

1. 先检查ev对象相关的文件描术符是否已经关了?如果是ev对象的activty设为0即可返回;(因为文件描术符关了,epoll自动从监听队列中删除。所以没有必要明显删除。)

2. 后面逻辑与ngx_epoll_add_event()函数的差不多,只不过是删除操作。

ngx_epoll_add_connection()函数。

用EPOLL_CTL_ADD方式调用epoll_ctl()注册某个ngx_connection_t对象中的read与write事件对象,并激活之。

ngx_epoll_del_connection()函数。

同样先检查相关的文件描术符是否已经关了?如果是ngx_connection_t对象中read与write事件的activty设为0即可返回;(因为文件描术符关了,epoll自动从监听队列中删除。所以没有必要明显删除。)

用EPOLL_CTL_DEL方式调用epoll_ctl()注销某个ngx_connection_t对象中的read与write事件对象,并设active为0。

ngx_enable_accept_events()函数。

将cycle中监听的ngx_listening_t对象中添加监听事件,主要对accept事件的发生。

如果ngx_event_flags标志中表明了使用了实时信号(即RTSIG),调用ngx_add_conn()为一个ngx_listening_t对象中ngx_connection_t对象添加读、写事件;否则仅对这个ngx_connection_t对象添加读取事件。(客户端一有请求,服务器端accept事件发生时当然也是可读了。)

January 14th Tuesday 2010相关推荐

  1. January 7th Tuesday 2010

    Nginx(六) 1. ngx_start_worker_processes()函数,这个函数按指定数目n,以ngx_worker_process_cycle()函数为参数调用ngx_spawn_pr ...

  2. January 12th Tuesday 2010

    Nginx(八)  工作进程 ngx_event_find_timer()函数. nginx中的timer用红黑树的结构排序.ngx_event_timer_rbtree就是nginx中timer的红 ...

  3. This Week in Spring - January 14th, 2020

    Engineering Josh Long January 14, 2020 Hi, Spring fans! What a week! I have - nothing- happening thi ...

  4. January 11th Monday 2010

    Nginx(七)  工作进程 进程部份 一切从ngx_worker_process_init()函数开始: 1.先调用ngx_set_environment()函数为本进程设定环境变量,那些环境变量都 ...

  5. moment 时间日期处理库 解析部分

    moment 官方api 1. 安装导入 1.1 安装 npm install moment -g # 全局安装 npm install moment # 安装 1.2 导入 //require 方式 ...

  6. 版本扫盲及最新android studio下载

    2019独角兽企业重金招聘Python工程师标准>>> 由于某镇墙很高,有些资源并不好找,这里提供一些android studio的链接. 最新当然是2.0 Beta 6,但是最新比 ...

  7. 在Ubuntu Linux中获取上次访问的文件时间

    Ubuntu Linux has a rich set of commands for manipulating and accessing files. The stat utility gives ...

  8. 《机器学习实战》笔记(04):基于概率论的分类方法 - 朴素贝叶斯分类

    基于概率论的分类方法:朴素贝叶斯分类 Naive Bayesian classification 这大节内容源于带你理解朴素贝叶斯分类算法,并非源于<机器学习实战>.个人认为<机器学 ...

  9. centos 6.4 postfix mysql_CentOS 6.4下Postfix邮件服务安装和基本配置

    三.基于Postfix构建简单电子邮件 1.配置并测试Postfix服务器 1>.编辑main.cf文件,调整Postfix的基本运行参数 [root@mail~]# vi /etc/postf ...

最新文章

  1. 新年之际,最新摄影必备的无人机免费送10个给大家!
  2. Centos7安装Python3并更改默认版本为python3(编译安装)
  3. mysql索引与约束有什么关系_MySQL 约束与索引
  4. MySQL子查询介绍
  5. ios开发 静音键设置_合肥包河区:连夜设置三道拦水坝只为按下中考“静音键”...
  6. PHP读取创建txt,doc,xls,pdf类型文件
  7. kernel devel 安装与卸载
  8. Comet OJ(Contest #8)-D菜菜种菜【树状数组,指针】
  9. GIT上fork的项目获取最新源代码
  10. 下载丨67页PDF,云和恩墨技术通讯(2021年1月刊)
  11. c# session总结
  12. FFT(不ji是干甚用的diao操作)
  13. 《与大象共舞》读书笔记
  14. endnote 中文论文中的et al处理
  15. 谁是卧底? 科普影子寄存器
  16. nslookup命令反解ip_PING、TRACERT、NSLOOKUP命令的使用方法
  17. bugku 告诉你个秘密(ISCCCTF) (636A56355279427363446C4A49454A7154534230526D6843 56445A31614342354E326C4B494)
  18. 信息论复习—信源编码的基本方法
  19. Java教程张孝祥百度云,蚂蚁金服5面
  20. “App真的会偷偷录音,然后给我推销东西吗?”

热门文章

  1. 百度Q3财报解读:“云+AI”酝酿新动能
  2. c语言textout字体大小,textout函数怎么用
  3. APW7080KAI-TRG
  4. iOS 十进制转换成十六进制
  5. 向大家推荐一部励志电影
  6. 转-Launchy:最强快速启动的完整优化指南
  7. Sqlsugar使用Oracle进行时间查询相关
  8. 基于JAVA社区流浪猫狗救助网站计算机毕业设计源码+数据库+lw文档+系统+部署
  9. 电锯惊魂6果然让我失望了
  10. CMD新建删除文件夹