nginx对upstream访问的处理非常的,不能说奇怪,但也至少是很另类。在处理的流程中,针对每个upsream,在访问到的时候,再建立socket,然后绑定upstream的地址,然后针对此upstream进行读,然后再关闭。这么处理非常违反开发直觉,在做游戏服务器的时候,对于socket这种系统资源,是在系统启动时候预先建立好,然后再整个服务器运行的过程中都保持连接,当单独的socket断开时,并不是关闭此socket, 而是将此socket重新赋值给新的连接。
看看下面的流程,会很让人震惊的
(gdb) bt
#0 ngx_event_connect_peer (pc=0x9c1e6d8) at src/event/ngx_event_connect.c:32
#1 0x0808cd9b in ngx_http_upstream_connect (r=0x9c1de38, u=0x9c1e6d0) at src/http/ngx_http_upstream.c:1368
#2 0x0808bd82 in ngx_http_upstream_init_request (r=0x9c1de38) at src/http/ngx_http_upstream.c:772
#3 0x0808b61d in ngx_http_upstream_init (r=0x9c1de38) at src/http/ngx_http_upstream.c:523
#4 0x080bc5d7 in ngx_http_memcached_handler (r=0x9c1de38) at src/http/modules/ngx_http_memcached_module.c:224
#5 0x0807a37b in ngx_http_core_content_phase (r=0x9c1de38, ph=0x9c33db8) at src/http/ngx_http_core_module.c:1363
#6 0x08075828 in ngx_http_core_run_phases (r=0x9c1de38) at src/http/ngx_http_core_module.c:840
#7 0x0807594a in ngx_http_handler (r=<optimized out>) at src/http/ngx_http_core_module.c:823
#8 0x0807d281 in ngx_http_process_request (r=0x9c1de38) at src/http/ngx_http_request.c:1910
#9 0x0807fa89 in ngx_http_process_request_headers (rev=0x78a930c8) at src/http/ngx_http_request.c:1342
#10 0x0807fd3e in ngx_http_process_request_line (rev=0x78a930c8) at src/http/ngx_http_request.c:1022
#11 0x08080691 in ngx_http_wait_request_handler (rev=0x78a930c8) at src/http/ngx_http_request.c:499
#12 0x08072a91 in ngx_epoll_process_events (cycle=0x9c185e8, timer=60000, flags=1) at src/event/modules/ngx_epoll_module.c:822
#13 0x0806abbc in ngx_process_events_and_timers (cycle=0x9c185e8) at src/event/ngx_event.c:242
#14 0x08070ebc in ngx_worker_process_cycle (cycle=0x9c185e8, data=0x0) at src/os/unix/ngx_process_cycle.c:753
#15 0x0806f932 in ngx_spawn_process (cycle=0x9c185e8, proc=0x8070e2a <ngx_worker_process_cycle>, data=0x0, name=0x80d540a "worker process",
respawn=-3) at src/os/unix/ngx_process.c:198
#16 0x08070338 in ngx_start_worker_processes (cycle=cycle@entry=0x9c185e8, n=1, type=type@entry=-3) at src/os/unix/ngx_process_cycle.c:358
#17 0x080716a5 in ngx_master_process_cycle (cycle=0x9c185e8) at src/os/unix/ngx_process_cycle.c:130
#18 0x08052669 in main (argc=1, argv=0xbfaeb814) at src/core/nginx.c:367