nginx_event_framework
Overview
Each connection(preallocated for fast) has two events, one for read, the other for write, after nginx adds connection(fd) EPOLLIN and EPOLLOUT to epoll instance(each worker has its own), when event is ready, call handler saved at proper event in epoll directly, or adds the event in post queue, after epoll quits, at last step call event->handler.
Event framework
Data Structure
1 | struct ngx_event_s { |
Timer event
when struct ngx_event_s
is used as timer event, most of its fields are not used, only few are useful for timer event. when user adds a timer event, nginx adds the event to timer RB tree, the key is absolute time(expire time), each epoll_wait() call, use the minimal value of timer tree as timeout parameter, after epoll_wait() returns, it could be data ready or timeout, then check the timer event in RB tree, to see if some of them expire or not.
if there is no timer, epoll_wait will block for ever, wake up only when there is event on socket like data ready, EOF, error, interrupt etc.
1 | struct ngx_event_t { |