Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Nginx 事件驱动机制
Search
David Zhang
September 09, 2016
Programming
580
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Nginx 事件驱动机制
来自 Nevermore (@hzphust)
David Zhang
September 09, 2016
More Decks by David Zhang
See All by David Zhang
ngx_share: conf & module
crispgm
0
36
ReactNative 探索之路
crispgm
1
400
Other Decks in Programming
See All in Programming
私なりのハーネスエンジニアリング ~独自の言語処理系の開発を通して~
seosoft
0
120
AI時代のシニアエンジニアとは
yosuke_furukawa
PRO
12
11k
AIがコードを書いてくれる時代、我々は何を確かめるのか
yosuke_furukawa
PRO
13
5.1k
作って学ぶSpring BootのAuto Configuration / JJUG Night Seminar 2026-10-19
rshindo
0
210
エージェントを使い続けるためのメタワーク / Meta-work for keeping using the agent
rkaga
6
2k
プロジェクトコードをキレイキレイしよう!暗黙的な参照を読み解く静的解析
ostk0069
0
170
最近”作らせてる” ローカルAIアプリの話
akit37
0
510
学びを記事・スライド・Xへ高速展開する― AI-assisted Learning Loopで回すコンテンツ生成術
ochtum
0
160
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
210
【加筆修正版】Laravel のアプリケーションをどこにデプロイするか #phpcon_ehime
akase244
0
160
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
16
8.9k
contenteditable と日本語入力に向き合う
colorful12
0
230
Featured
See All Featured
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
650
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
2
2.1k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.4k
Un-Boring Meetings
codingconduct
0
470
The Language of Interfaces
destraynor
162
27k
Evolving SEO for Evolving Search Engines
ryanjones
0
340
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
A better future with KSS
kneath
240
18k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
610
A Tale of Four Properties
chriscoyier
163
24k
Tell your own story through comics
letsgokoyo
1
1.1k
Done Done
chrislema
187
17k
Transcript
NGINX事件驱动机制
大纲 ▸ Linuxᔮᕹ5ᐿIOཛྷୗ ▸ IOग़᪠॔አ ▸ nginxԪկཛྷࣘ ▸ nginxԪկḝۖຝ
⽂本 IO准备知识 ▸ DirectIO和BufferIO ▸ IO数据先拷贝到内核空间的缓冲区,然后从内核空间缓冲区复制到应用程序的地址 空间 ▸ 不经过内核缓冲区,直接访问应用程序地址空间 ▸
IO的两个阶段 ▸ 等待数据准备 ▸ 将数据从内核拷贝到用户进程中
LINUX系统5种IO模式 ▸ blocking IO ▸ nonblocking IO ▸ IO multiplexing
▸ signal driven IO ▸ asynchronous IO
⽂本
⽂本
⽂本 IO multiplexing ▸ I/O多路复用就是通过一种机制,一个进程可以监视多个描述符,一旦某个 描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写 操作 ▸ 本质是同步IO ▸
select/poll/epoll int select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); int poll (struct pollfd *fds, unsigned int nfds, int timeout);
⽂本 epoll ▸ 数据和接口 ▸ LT模式和ET模式 int epoll_create(int size); int
epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout); struct epoll_event { __uint32_t events; epoll_data_t data; } EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD EPOLLIN EPOLLOUT EPOLLHUP EPOLLERR
⽂本 nginx事件模块 ▸ 事件驱动架构 ▸ ᗑܜ/ᏺፏ/ਧԾኞԪկ ▸ ngxԪկཛྷࣘᨮᨱԪկጱතᵞ/ړݎ ▸ ٌ՜ཛྷࣘဳٙఽي᪁Ԫկ҅ၾᩇԪկ
▸ 与传统web服务器比较 ▸ 不再使用进程/线程作为事件消费者,只能是模块,只有事件收集/分发器才能占用 进程资源 ▸ 事件消费者不允许有任何阻塞操作,进程不会休眠
⽂本 EVENT ▸ IO和定时器 ▸ ngx_event_t ▸ void *data, ngx_event_handler_pt
handler ▸ nginx启动时预分配了所有读写事件,和连接相对应 ▸ ngx_handle_read_event/ngx_handle_write_event
⽂本 CONNECTION ▸ 主动连接/被动连接 ▸ ngx_connection_t ▸ fd, read_ev, write_ev,data,
sockaddr,recv,write ▸ 连接池 ▸ connections/free_connections ▸ ngx_get_connection/ngx_free_connection
⽂本 NGX_EVENT_MODULE ▸ 事件模块 ▸ 功能 ▸ 定义新的事件类型 ▸ 定义所有事件模块必须实现的ngx_event_module_t接口
▸ 管理并解析事件类模块的配置项 ▸ ngx_events_module ▸ ngx_events_commands ▸ ngx_events_module_ctx
⽂本 NGX_EVENT_CORE_MODULE ▸ 所有事件模块中顺序第一位 ▸ 功能 ▸ 创建连接池 ▸ 决定使用哪些事件驱动机制
▸ 初始化将要使用的事件模块 ▸ ngx_events_core_commands
⽂本 NGX_EPOLL_MODULE ▸ ngx_epoll_init ▸ epoll_create ▸ 创建event_list数组 ▸ ngx_epoll_add_event/ngx_epoll_del_event
▸ epoll_ctl ▸ ngx_epoll_process_events ▸ epoll_wait ▸ instance/NGX_POST_EVENTS 标志位
⽂本 ACCEPT_MUTEX ▸ “惊群”效应 ▸ 多个worker同时accept一个连接 ▸ accept_mutex ▸ ngx_trylock_accept_mutex
▸ ngx_enable_accept_event ▸ 什么时候释放? ▸ nginx post事件机制 ▸ NGX_POST_EVENTS flags ▸ ngx_posted_accept_events/ngx_posted_events ▸ worker子进程的负载均衡 ▸ ngx_accept_disabled = connection_n/8 - free_connection_n
⽂本 ▸ ngx_process_events_and_timer ▸ 处理网络事件 ▸ 调用所有事件模块的ngx_event_process ▸ 处理post事件队列 ▸
ngx_event_process_posted ▸ 处理定时器事件 ▸ ngx_event_expire_timer()
THANKS !