Nginx的事件處理機制

來源:互聯網
上載者:User

標籤:io   os   使用   ar   sp   div   art   on   amp   

  1. void  
  2. ngx_process_events_and_timers(ngx_cycle_t *cycle)  
  3. {  
  4.     ngx_uint_t  flags;  
  5.     ngx_msec_t  timer, delta;  
  6.     if (ngx_timer_resolution) {  
  7.         timer = NGX_TIMER_INFINITE;  
  8.         flags = 0;  
  9.     } else {  
  10.         timer = ngx_event_find_timer();  
  11.         flags = NGX_UPDATE_TIME;  
  12.     }  
  13.     /*ngx_use_accept_mutex變數代表是否使用accept相互排斥體 
  14.      預設是使用,accept_mutex off;指令關閉。 
  15.      accept mutex的作用就是避免驚群,同一時候實現負載平衡。 
  16.      */  
  17.     if (ngx_use_accept_mutex) {  
  18.       
  19.         /* 
  20.          ngx_accept_disabled變數在ngx_event_accept函數中計算。 
  21.          假設ngx_accept_disabled大於了0,就表示該進程接受的 
  22.          串連過多,因此就放棄一次爭搶accept mutex的機會,同一時候將 
  23.          自己減1。然後,繼續處理已有串連上的事件。Nginx就借用 
  24.          此變數實現了進程關於串連的基本負載平衡。 
  25.          */  
  26.         if (ngx_accept_disabled > 0) {  
  27.             ngx_accept_disabled--;  
  28.         } else {  
  29.             /* ngx_accept_disabled小於0,串連數沒超載*/  
  30.               
  31.             /*嘗試鎖accept mutex,僅僅有成功擷取鎖的進程,才會將listen 
  32.               通訊端放入epoll中。因此,這就保證了僅僅有一個進程擁有 
  33.               監聽套介面,故全部進程堵塞在epoll_wait時,不會出現驚群現象。 
  34.             */  
  35.             if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {  
  36.                 return;  
  37.             }  
  38.             if (ngx_accept_mutex_held) {  
  39.                 /*擷取鎖的進程,將加入一個NGX_POST_EVENTS標誌, 
  40.                   此標誌的作用是將全部產生的事件放入一個隊列中, 
  41.                   等釋放鎖後,再慢慢來處理事件。由於,處理事件可能 
  42.                   會非常耗時,假設不先釋放鎖再處理的話,該進程就長 
  43.                   時間霸佔了鎖,導致其它進程無法擷取鎖,這樣accept 
  44.                   的效率就低了。 
  45.                 */  
  46.                 flags |= NGX_POST_EVENTS;   
  47.             } else {  
  48.               
  49.                 /*沒有獲得鎖的進程,當然不須要NGX_POST_EVENTS標誌了。 
  50.                   但須要設定最長延遲多久,再次去爭搶鎖。 
  51.                 */  
  52.                 if (timer == NGX_TIMER_INFINITE  
  53.                     || timer > ngx_accept_mutex_delay)  
  54.                 {  
  55.                     timer = ngx_accept_mutex_delay;  
  56.                 }  
  57.             }  
  58.         }  
  59.     }  
  60.       
  61.     delta = ngx_current_msec;  
  62.     /*epoll開始wait事件了,ngx_process_events的詳細實現是相應到 
  63.       epoll模組中的ngx_epoll_process_events函數。單獨分析epoll 
  64.       模組的時候,再詳細看看。 
  65.     */  
  66.     (void) ngx_process_events(cycle, timer, flags);  
  67.     delta = ngx_current_msec - delta; /*統計本次wait事件的耗時*/  
  68.     /*ngx_posted_accept_events是一個事件隊列 
  69.       暫存epoll從監聽套介面wait到的accept事件。 
  70.       前文提到的NGX_POST_EVENTS標誌被使用後,就會將 
  71.       全部的accept事件暫存到這個隊列。 
  72.        
  73.       這裡完畢對隊列中的accept事件的處理,實際就是調用 
  74.       ngx_event_accept函數來擷取一個新的串連,然後放入 
  75.       epoll中。 
  76.     */  
  77.     if (ngx_posted_accept_events) {  
  78.         ngx_event_process_posted(cycle, &ngx_posted_accept_events);  
  79.     }  
  80.     /*全部accept事件處理完畢,假設擁有鎖的話,就趕緊釋放了。 
  81.       其它進程還等著搶了。 
  82.     */  
  83.     if (ngx_accept_mutex_held) {  
  84.         ngx_shmtx_unlock(&ngx_accept_mutex);  
  85.     }  
  86.     /*delta是上文對epoll wait事件的耗時統計,存在毫秒級的耗時 
  87.       就對全部事件的timer進行檢查,假設time out就從timer rbtree中, 
  88.       刪除到期的timer,同一時候調用相應事件的handler函數完畢處理。 
  89.     */  
  90.     if (delta) {  
  91.         ngx_event_expire_timers();  
  92.     }  
  93.     /*處理普通事件(串連上獲得的讀寫事件)隊列上的全部事件, 
  94.       由於每一個事件都有自己的handler方法,該怎麼處理事件就 
  95.       依賴於事件的詳細handler了。 
  96.     */  
  97.     if (ngx_posted_events) {  
  98.         if (ngx_threaded) {  
  99.             ngx_wakeup_worker_thread(cycle);  
  100.         } else {  
  101.             ngx_event_process_posted(cycle, &ngx_posted_events);  
  102.         }  
  103.     }  
  104. }  

Nginx的事件處理機制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.