nginx源码初读(8)--让烦恼从数据结构开始(ngx_event)
来源:互联网 发布:软件开发公司用工合同 编辑:程序博客网 时间:2024/05/24 05:34
nginx中的事件模块是一个很重要的模块,但这里作为初读,我们只简单看一下ngx_event的数据结构,至于模块和机制,留作之后再分析。
下面是结构体ngx_event_t的代码:
typedef struct ngx_event_s ngx_event_t;struct ngx_event_s { void *data; /* 事件上下文数据,通常data都是指向ngx_connection_t连接对象。 * 开启文件异步I/O时,它可能会指向ngx_event_aio_t结构体。 * ... ngx_int_t ngx_handle_write_event(ngx_event_t *wev, size_t lowat) { ngx_connection_t *c; if (lowat) { c = wev->data; // 在这里event的data就指向了connection_t,直接用指针获取 if (ngx_send_lowat(c, lowat) == NGX_ERROR) { return NGX_ERROR; } }...} ... unsigned write:1; /* 标志位,为1时表示事件是可写的。通常它表示对应的TCP连接可写,也就是连接处于可以发送网络包的状态。*/ unsigned accept:1; /* 标志位,为1时表示为此事件可以建立新的连接。通常在ngx_cycle_t中的listening动态数组中, 每一个监听对象ngx_listening_t,对应的读事件中的accept标志位才会是1。*/ unsigned instance:1; /* used to detect the stale events in kqueue, rtsig, and epoll * 这个标志位用于区分当前事件是否过期,它仅仅是给事件驱动模块使用的,而事件消费模块可不用关心。 * 为什么需要这个标志位呢?当开始处理一批事件时,处理前面的事件可能会关闭一些连接, 而这些连接有可能影响这批事件中还未处理到的后面的事件,这时可通过instance来避免处理后面的过期事件。 */ unsigned active:1; /* the event was passed or would be passed to a kernel; * in aio mode - operation was posted. * 标志位,为1表示当前事件是活跃的,为0表示事件是不活跃的。 * 这个状态对应着事件驱动模块处理方式的不同。例如,在添加事件,删除事件和处理事件时, active标志位的不同都会对应着不同的处理方式。在使用事件时,一般不会直接改变active标志位。 * ... if (!rev->delayed) { if (rev->active && !rev->ready) { ngx_add_timer(rev, p->read_timeout); } else if (rev->timer_set) { ngx_del_timer(rev); } } ... */ unsigned disabled:1; /* 标志位,为1表示禁用事件,仅在kqueue或者rtsig事件驱动模块中有效,对于epoll事件驱动模块则没有意义。 * ... if (c->read->active || c->read->disabled) { ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT); } ... // 位于close_connection函数中 */ / unsigned ready:1; /* the ready event; in aio mode 0 means that no operation can be posted * 标志位,为1表示当前事件准备就绪,也就是说,允许这个事件的handler处理这个事件。 * 在HTTP框架中,经常会检查事件的ready标志位,以确定是否可以接收请求或者发送相应。 * ... if (rev->ready) { if (ngx_use_accept_mutex) { ngx_post_event(rev, &ngx_posted_events); return; } rev->handler(rev); return; } ... */ unsigned oneshot:1; /* 该标志位仅对kqueue,eventport等模块有意义,而对于linux上的epoll事件驱动模块则是无意义的。*/ unsigned complete:1; /* aio operation is complete * 用于异步aio事件的处理 */ unsigned eof:1; unsigned error:1; /* 标志位,eof表示当前处理的字符流已经结束,error表示事件处理过程出错了。 * ... flags = (rev->eof || rev->error) ? NGX_CLOSE_EVENT : 0; ... */ unsigned timedout:1; /* 标志位,为1表示这个事件超时,用以提示handler做超时处理,它与timer_set都用了定时器 * ... if (wev->timedout) { wev->timedout = 0; ngx_http_perl_handle_request(r); return; } ... */ unsigned timer_set:1; /* 标志位,为1表示这个事件存在于定时器中 * ... if (!ngx_cleaner_event.timer_set) { ngx_add_timer(&ngx_cleaner_event, 30000); ngx_cleaner_event.timer_set = 1; } ... */ unsigned delayed:1; /* 标志位,delayed为1表示需要延迟处理这个事件,它仅用于限速功能 */ unsigned deferred_accept:1; /* 标志位,为1表示延迟建立TCP连接,也就是TCP三次握手后并不建立连接,而是等到真正收到数据包后才建连接 */ unsigned pending_eof:1; /* the pending eof reported by kqueue, epoll or in aio chain operation * 标志位,为1表示等待字符流结束,它只与kqueue和aio事件驱动机制有关 */ 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 unsigned available:1;#endif ngx_event_handler_pt handler; /* 这个事件发生时的处理方法,每个事件处理模块都会重新实现它 */#if (NGX_HAVE_AIO)// win下的一种事件驱动模型#if (NGX_HAVE_IOCP) ngx_event_ovlp_t ovlp;#else // 在linux中定义的aio结构体 struct aiocb aiocb;#endif#endif ngx_uint_t index; /* epoll 事件驱动方式不使用index */ ngx_log_t *log; /* 记录当前event的log对象 */ ngx_rbtree_node_t timer; /* 定时器节点,用于定时器红黑树中 */ /* the posted queue */ ngx_queue_t queue; 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};
我们再看一下event.h里一个数据结构:
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); /* 添加/移出事件方法,负责把事件添加/移出到操作系统提供的事件驱动机制(如epoll,kqueue等)中, 这样在事件发生之后,将可以/无法调用下面的process_envets时获取这个事件。*/ 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); /* 启用/禁用一个事件,目前事件框架不会调用,大部分事件驱动模块对该方法的实现都与add/del完全一致 */ 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_events)(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags); /* 在正常的工作循环中,将通过调用process_events方法来处理事件。 * 这个方法仅在ngx_process_events_and_timers方法中调用,它是处理分发事件的核心。*/ ngx_int_t (*init)(ngx_cycle_t *cycle, ngx_msec_t timer); void (*done)(ngx_cycle_t *cycle); /* 初始化和退出事件驱动模块的方法 */} ngx_event_actions_t;extern ngx_event_actions_t ngx_event_actions;
作为nginx中比较重要的一个驱动,event当然是很复杂的,第一遍看反正怎么也不容易懂,后续刨析了nginx的各种机制和流程应该就很容易懂了,先放过它,继续往下看。
贴个链接:https://segmentfault.com/a/1190000002715203
0 0
- nginx源码初读(8)--让烦恼从数据结构开始(ngx_event)
- nginx源码初读(1)--让烦恼从数据结构开始(ngx_cdecl/ngx_int/ngx_log)
- nginx源码初读(2)--让烦恼从数据结构开始(ngx_buf/ngx_chain)
- nginx源码初读(3)--让烦恼从数据结构开始(ngx_pool)
- nginx源码初读(4)--让烦恼从数据结构开始(ngx_str)
- nginx源码初读(5)--让烦恼从数据结构开始(ngx_array)
- nginx源码初读(6)--让烦恼从数据结构开始(ngx_list)
- nginx源码初读(7)--让烦恼从数据结构开始(ngx_queue)
- nginx源码初读(9)--让烦恼从数据结构开始(ngx_listening/ngx_connection)
- nginx源码初读(10)--让烦恼从数据结构开始(ngx_cycle)
- nginx源码初读(11)--让烦恼从数据结构开始(ngx_command/ngx_module/ngx_conf)
- nginx源码初读(1)--让烦恼从数据结构开始(ngx_cdecl/ngx_int/ngx_log)
- nginx源码初读(1)--让烦恼从数据结构开始(ngx_cdecl/ngx_int/ngx_log)
- nginx源码剖析--从main函数开始
- ngx_event
- nginx源码剖析(2)----从main()函数开始
- nginx源码剖析(2)----从main()函数开始
- 开始烦恼
- js验证数字正整数和0以及其他验证
- Django 移动端交互
- qt自定义搜索框(超简单,带效果图)
- CSS浏览器兼容问题总结
- win10下WAMP Server安装
- nginx源码初读(8)--让烦恼从数据结构开始(ngx_event)
- 解析数组里面包含数组的json格式的数据代码
- 外汇汇率查询接口开发文档与示例
- C++将微软文档转换成在线网页
- 【Java学习笔记】内部类
- 转场动画 帧动画
- python模块系列之 - xml
- ID definitions *must* be of the form @+id/name; try using @+id/main_list_dcimageView1
- android SQLiteHelper对数据库的操作