nginx中一個請求的count計數(shù)跟蹤淺析
首先說明一下應(yīng)用方式,有兩個nginx的模塊,一個名為jtxy,另一個名為jtcmd。一個http請求來了,會進(jìn)入jtxy的模塊處理,jtxy會創(chuàng)建出一個子請求發(fā)送給jtcmd,jtcmd里處理呢又會創(chuàng)建出一個upstream流到我們的上游非http服務(wù)A來處理,A處理完成后得到結(jié)果,會把結(jié)果返回給jtcmd的子請求,jtcmd的子請求把結(jié)果返回給jtxy。就是這樣一個流程,我們來跟蹤一下一個請的count計數(shù)。
1、請求到來,創(chuàng)建一個請求,ngx_http_alloc_request里count被初始化為1
此時,count為1。
r->main = r; r->count = 1;
2、jtxy模塊里處理請求時,調(diào)用了ngx_http_subrequest來創(chuàng)建一個子請求,在ngx_http_subrequest里計數(shù)被加1。
此時,count為2
r->main->count++;
3、從一個模塊出去(這里就是jtxy模塊),會調(diào)用ngx_http_finalize_request,在ngx_http_finalize_request里會計數(shù)減一。
此時,count為1。
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、然后進(jìn)入了我們的子請求jtcmd模塊,,在這個模塊里,如果發(fā)現(xiàn)自己是個子請求((r!=r->main)),那么就應(yīng)該把主請求計數(shù)加一。這里標(biāo)紅強調(diào)這點是因為如果不加1,那么主請求計數(shù)就會有問題,一會兒我們繼續(xù)跟蹤count的減1就會發(fā)現(xiàn)這個問題。
這里是jtxy發(fā)起的一個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里就已經(jīng)加1了,所以,我們這里就不必自己來做這個加1了。
此時count為3。
大家可以看看《深入理解nginx》的5.1.5節(jié)的內(nèi)容。對upstream流需要加1有講解。
所以呢,這里的count是加了2次的。
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。
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一樣,請求的處理完后會調(diào)用ngx_http_finalize_request把計數(shù)減一,但是這里不同的是我們這里是一個子請求,他這里有一步r = r->main;所以實際減時就減到了主請求上去了,這個也是我們在4里紅字說明的要加1的原因了。
此時,count為2
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,那么當(dāng)upstream結(jié)束,就要減一次1。
此時count為1。
7、子請求完成后,父請求的回調(diào)方法接著處理,接下來就回到了主請求模塊jtxy里,這里在處理結(jié)束后就會調(diào)用ngx_http_finalize_request來結(jié)束掉這個請求了,此時count為1,請求就會被釋放掉了。
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; }
總結(jié)
到此這篇關(guān)于nginx中一個請求的count計數(shù)跟蹤的文章就介紹到這了,更多相關(guān)nginx請求的count計數(shù)跟蹤內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。