首先說明一下應用方式,有兩個nginx的模塊,一個名為jtxy,另一個名為jtcmd。一個http請求來了,會進入jtxy的模塊處理,jtxy會創建出一個子請求發送給jtcmd,jtcmd里處理呢又會創建出一個upstream流到我們的上游非http服務A來處理,A處理完成后得到結果,會把結果返回給jtcmd的子請求,jtcmd的子請求把結果返回給jtxy。就是這樣一個流程,我們來跟蹤一下一個請的count計數。
1、請求到來,創建一個請求,ngx_http_alloc_request里count被初始化為1
此時,count為1。
1
2
|
r->main = r; r->count = 1; |
2、jtxy模塊里處理請求時,調用了ngx_http_subrequest來創建一個子請求,在ngx_http_subrequest里計數被加1。
此時,count為2
1
|
r->main->count++; |
3、從一個模塊出去(這里就是jtxy模塊),會調用ngx_http_finalize_request,在ngx_http_finalize_request里會計數減一。
此時,count為1。
1
2
3
4
5
|
if (r->content_handler) { r->write_event_handler = ngx_http_request_empty_handler; ngx_http_finalize_request(r, r->content_handler(r)); return NGX_OK; } |
4、然后進入了我們的子請求jtcmd模塊,,在這個模塊里,如果發現自己是個子請求((r!=r->main)),那么就應該把主請求計數加一。這里標紅強調這點是因為如果不加1,那么主請求計數就會有問題,一會兒我們繼續跟蹤count的減1就會發現這個問題。
這里是jtxy發起的一個jtcmd子請求這里的r和r->main并不相同的,r是jtcmd,r->main就是jtxy。
此時,count為2。
同時因為我們子請求jtcmd模塊里使用了upstream,那么count是還需要在加1,但是我們使用時是ngx_http_read_client_request_body(r, ngx_http_upstream_init),ngx_http_read_client_request_body里就已經加1了,所以,我們這里就不必自己來做這個加1了。
此時count為3。
大家可以看看《深入理解nginx》的5.1.5節的內容。對upstream流需要加1有講解。
所以呢,這里的count是加了2次的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
r->upstream->resolved->sockaddr = (struct sockaddr*)&backendSockAddr; r->upstream->resolved->socklen = sizeof(struct sockaddr_in); r->upstream->resolved->naddrs = 1; r->upstream->create_request = jtcmd_upstream_create_request; r->upstream->process_header = jtcmd_upstream_process_header; r->upstream->finalize_request = jtcmd_upstream_finalize_request; r->upstream->abort_request = jtcmd_upstream_abort_request; r->upstream->input_filter_init = ngx_http_jtcmd_filter_init; r->upstream->input_filter = ngx_http_jtcmd_filter; r->upstream->input_filter_ctx = jtcmdctx; //r- >subrequest_in_memory = 1; if (r!=r->main) { r->main->count++; } ngx_int_t rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init); if (rc == NGX_ERROR || rc > NGX_OK) { return rc; } |
這里的r是子請求,r->main是主請求。同時還注意到,子請求的count始終是0。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
ngx_int_t ngx_http_read_client_request_body(ngx_http_request_t *r, ngx_http_client_body_handler_pt post_handler) { size_t preread; ssize_t size; ngx_int_t rc; ngx_buf_t *b; ngx_chain_t out; ngx_http_request_body_t *rb; ngx_http_core_loc_conf_t *clcf; r->main->count++; |
5、同第3一樣,請求的處理完后會調用ngx_http_finalize_request把計數減一,但是這里不同的是我們這里是一個子請求,他這里有一步r = r->main;所以實際減時就減到了主請求上去了,這個也是我們在4里紅字說明的要加1的原因了。
此時,count為2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t rc) { ngx_connection_t *c; r = r->main; c = r->connection; ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "http request count:%d blk:%d" , r->count, r->blocked); if (r->count == 0) { ngx_log_error(NGX_LOG_ALERT, c->log, 0, "http request count is zero" ); } r->count--; |
6、然后呢,因為子請求使用了upstream,因為這個原因count加了一次1,那么當upstream結束,就要減一次1。
此時count為1。
7、子請求完成后,父請求的回調方法接著處理,接下來就回到了主請求模塊jtxy里,這里在處理結束后就會調用ngx_http_finalize_request來結束掉這個請求了,此時count為1,請求就會被釋放掉了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
void ngx_http_free_request(ngx_http_request_t *r, ngx_int_t rc) { ngx_log_t *log; ngx_pool_t *pool; struct linger linger; ngx_http_cleanup_t *cln; ngx_http_log_ctx_t *ctx; ngx_http_core_loc_conf_t *clcf; log = r->connection->log; ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request" ); if (r->pool == NULL) { ngx_log_error(NGX_LOG_ALERT, log, 0, "http request already closed" ); return ; } |
總結
到此這篇關于nginx中一個請求的count計數跟蹤的文章就介紹到這了,更多相關nginx請求的count計數跟蹤內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/wwyyxx26/article/details/122211554