"/>

璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?/h1>

Posted 楂樻€ц兘鏈嶅姟鏋舵瀯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?/h1>

相关的知识,希望对你有一定的参考价值。

閲嶇骞茶揣锛岀涓€鏃堕棿閫佽揪

鉂?/span>

娌夋穩銆佸垎浜€佹垚闀匡紝璁╄嚜宸卞拰浠栦汉閮借兘鏈夋墍鏀惰幏锛侌煒?/p> 鉂?/span>

 鐩綍 

  • 涓€銆佺嚎绋嬫睜鐨勫師鐞?/p>

    • 1. 浠€涔堟槸绾跨▼姹?/p>

    • 2. nginx涓轰粈涔堥渶瑕佷娇鐢ㄧ嚎绋嬫睜

    • 3. Nginx绾跨▼姹犲簲鐢ㄥ満鏅?/p>

    • 4. 纾佺洏缁撴瀯

    • 5. Nginx璇锋眰澶勭悊娴佺▼

    • 6. 绾跨▼姹犵殑鎵ц娴佺▼

    • 7. 绾跨▼姹犵殑宸ヤ綔娴佺▼

    • 8. 绾跨▼姹犵殑鍔熻兘缁勪欢

  • 浜屻€佷唬鐮佸疄鐜扮嚎绋嬫睜

    • 1. 绾跨▼姹犵殑鍒涘缓

    • 2. 绾跨▼姹犵殑閿€姣?/p>

    • 3. 绾跨▼浠诲姟

    • 4.绾跨▼姹?/span>push浠诲姟

  • 涓夈€佺嚎绋嬫睜灏佽鎴愭ā鍧?/p>

  • 鍥涖€佺郴鍒楁帹鑽?/p>





璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?></p><p class=闈㈣瘯杩囩▼涓紝缁忓父浼氶棶鍒帮紝浣犱滑瀹㈡埛绔殑骞跺彂閲忔槸澶氬皯锛?/span>鏈夊摢浜涚摱棰堬紵鍋氭湇鍔$寮€鍙戠殑绋嬪簭鍛橀兘鐭ラ亾锛屽苟鍙戦噺鏄湇鍔$鑳藉悓鏃舵壙杞藉鎴风鐨勬暟閲忋€?/span>鏈嶅姟鍣ㄥ鐞嗗鎴风璇锋眰锛?/span>绾跨▼鐨勫垱寤哄拰閿€姣侊紝閮芥秹鍙婂埌绯荤粺璋冪敤锛屾瘮杈冩秷鑰楃郴缁熻祫婧愶紝閬垮厤棰戠箒鐨勭嚎绋嬪垱寤哄拰閿€姣佸氨鏄惧緱闈炲父蹇呰銆傞偅涔堬紝浠€涔堟槸绾跨▼姹狅紵绾跨▼姹犲伐浣滃師鐞嗘槸浠€涔堬紵绾跨▼姹犵殑鍔熻兘缁勪欢鏈夊摢浜涳紵搴旂敤鍦烘櫙鏈夊摢浜涳紵绛夌瓑


涓€銆佺嚎绋嬫睜鐨勫師鐞?/span>


1銆?/span>浠€涔堟槸绾跨▼姹?/span>

绾跨▼姹犳槸鍒涘缓鑻ュ共涓彲鎵ц鐨勭嚎绋嬫斁鍏ヤ竴涓睜锛堝鍣級涓紝鏈変换鍔¢渶瑕佸鐞嗘椂锛屼細鎻愪氦鍒扮嚎绋嬫睜涓殑浠诲姟闃熷垪锛屽鐞嗗畬涔嬪悗绾跨▼骞朵笉浼氳閿€姣侊紝鑰屾槸浠嶇劧鍦ㄧ嚎绋嬫睜涓瓑寰呬笅涓€涓换鍔°€?/span>


2銆?/em>Nginx涓轰粈涔堥渶瑕佺嚎绋嬫睜

涓轰簡鍦?/span>璋冪敤鎿嶄綔绯荤粺鍐呮牳鐨?/span>API杩囩▼涓?/span>锛屾搷浣滅郴缁熻涓虹嚎绋嬪垎閰嶄竴绯诲垪鐨勮祫婧愶紝鑰岃繖浜涙垚鏈緢楂橈紝鍙堝洜绾跨▼鏄竴涓噸閲忕骇鐨勫璞★紝闇€瑕侀伩鍏嶉绻佸垱寤哄拰閿€姣侊紝鍥犳寮曞叆绾跨▼姹犮€?/span>



3銆?/span>Nginx绾跨▼姹犵殑搴旂敤鍦烘櫙


璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?></p> 
<section class= 绾跨▼姹犲湪鎴戜滑鍐欎笟鍔′唬鐮佽繃绋嬩腑杩愮敤姣旇緝澶氾紝杩樻湁I/O寮傛鎿嶄綔銆佹棩蹇楃殑瀛樺偍闇€瑕佸簲鐢ㄧ嚎绋嬫睜銆?/span>

4銆?/span>纾佺洏缁撴瀯

璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?></span></p> 
<p class=

璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?></p> 
<p class=



5銆?/span>Nginx璇锋眰澶勭悊娴佺▼


璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?></p> 
<p class=



6銆?/span>绾跨▼姹犵殑鎵ц娴佺▼   璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?></span></p> 
<p class=

7銆?/span>绾跨▼姹犵殑宸ヤ綔娴佺▼


璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?></p> 
<p class=


8銆?/span>绾跨▼姹犵殑鍔熻兘缁勪欢

璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?></p> 
<p class=1. 鎵ц绾跨▼鍖呭惈绾跨▼ID銆佺粓姝㈡爣璇嗐€佹睜绠$悊缁勪欢瀵硅薄銆?/span>

typedef struct NWORKER { pthread_t thread; int terminate; struct NWORKQUEUE *workqueue; struct NWORKER *prev; struct NWORKER *next;} nWorker;

2. 绾跨▼涓墽琛岀殑浠诲姟鍖呭惈浠诲姟鍥炶皟鍑芥暟锛屼换鍔℃墽琛岀殑鍙傛暟銆?/span>

typedef struct NJOB { void (*job_function)(struct NJOB *job); void *user_data; struct NJOB *prev; struct NJOB *next;} nJob;

3. 姹犵鐞嗙粍浠讹細浜掓枼閿侊紝鏉′欢鍙橀噺锛屾墽琛岄槦鍒楋紝浠诲姟闃熷垪銆?/span>

typedef struct NWORKQUEUE { struct NWORKER *workers; struct NJOB *waiting_jobs; pthread_mutex_t jobs_mtx; pthread_cond_t jobs_cond;} nWorkQueue;


浜屻€佷唬鐮佸疄鐜扮嚎绋嬫睜

1銆?/span>绾跨▼姹犵殑鍒涘缓

int ntyThreadPoolCreate(nThreadPool *workqueue, int numWorkers) {
if (numWorkers < 1) numWorkers = 1; memset(workqueue, 0, sizeof(nThreadPool));
pthread_cond_t blank_cond = PTHREAD_COND_INITIALIZER; memcpy(&workqueue->jobs_cond, &blank_cond, sizeof(workqueue->jobs_cond));
pthread_mutex_t blank_mutex = PTHREAD_MUTEX_INITIALIZER; memcpy(&workqueue->jobs_mtx, &blank_mutex, sizeof(workqueue->jobs_mtx));
int i = 0; for (i = 0;i < numWorkers;i ++) { nWorker *worker = (nWorker*)malloc(sizeof(nWorker)); if (worker == NULL) { perror("malloc"); return 1; } memset(worker, 0, sizeof(nWorker)); worker->workqueue = workqueue; //printf("pthread_create --> %d ", i); int ret = pthread_create(&worker->thread, NULL, ntyWorkerThread, (void *)worker); if (ret) {
perror("pthread_create"); free(worker);
return 1; }
LL_ADD(worker, worker->workqueue->workers); }
return 0;}
2銆?/span> 绾跨▼姹犵殑 閿€姣?/span>
void ntyThreadPoolShutdown(nThreadPool *workqueue) { nWorker *worker = NULL;
for (worker = workqueue->workers;worker != NULL;worker = worker->next) { worker->terminate = 1; }
pthread_mutex_lock(&workqueue->jobs_mtx);
workqueue->workers = NULL; workqueue->waiting_jobs = NULL;
pthread_cond_broadcast(&workqueue->jobs_cond);
pthread_mutex_unlock(&workqueue->jobs_mtx);
}
3銆?/span> 绾跨▼浠诲姟
static void *ntyWorkerThread(void *ptr) { nWorker *worker = (nWorker*)ptr;
while (1) { pthread_mutex_lock(&worker->workqueue->jobs_mtx);
while (worker->workqueue->waiting_jobs == NULL) { if (worker->terminate) break; pthread_cond_wait(&worker->workqueue->jobs_cond, &worker->workqueue->jobs_mtx); }
if (worker->terminate) { pthread_mutex_unlock(&worker->workqueue->jobs_mtx); break; }
nJob *job = worker->workqueue->waiting_jobs; if (job != NULL) { LL_REMOVE(job, worker->workqueue->waiting_jobs); }
pthread_mutex_unlock(&worker->workqueue->jobs_mtx);
if (job == NULL) continue;
job->job_function(job); }
free(worker); pthread_exit(NULL);}

 4銆?/span>push浠诲姟

void ntyThreadPoolQueue(nThreadPool *workqueue, nJob *job) {
pthread_mutex_lock(&workqueue->jobs_mtx);
LL_ADD(job, workqueue->waiting_jobs);
pthread_cond_signal(&workqueue->jobs_cond); pthread_mutex_unlock(&workqueue->jobs_mtx);
}

5銆?/span>绾跨▼姹犵殑娴嬭瘯绋嬪簭


涓夈€佺嚎绋嬫睜灏佽鎴愭ā鍧?/span>

void *New(const void *_class, ...) { const AbstractClass *class = _class; void *p = calloc(1, class->size); memset(p, 0, class->size);
assert(p); *(const AbstractClass**)p = class;
if (class->ctor) { va_list params; va_start(params, _class); p = class->ctor(p, &params); va_end(params); } return p;}

void Delete(void *_class) { const AbstractClass **class = _class;
if (_class && (*class) && (*class)->dtor) { _class = (*class)->dtor(_class); }
free(_class);}
typedef struct _ThreadPool { const void *_; nThreadPool *wq;} ThreadPool;
typedef struct _ThreadPoolOpera {
size_t size; void* (*ctor)(void *_self, va_list *params); void* (*dtor)(void *_self); void (*addJob)(void *_self, void *task);
} ThreadPoolOpera;


鍥涖€佺郴鍒楁帹鑽?/span>

鍢垮樋锛屽疄璇濊鏈嶅姟鍣ㄥ疄鐜伴珮骞跺彂IO锛屼娇鐢ㄧ嚎绋嬫睜鏄潪甯稿父瑙佺殑锛?涓嚎杩樻槸寰堝父瑙佺殑鍝垀

闇€瑕佸畬鏁存簮鐮佺殑鏈嬪弸锛屽彲浠ュ湪鏈叕浼楀彿鐣欒█鍥炲"婧愮爜"銆?/span> 閴翠簬浣滆€呮湰浜轰篃鏄湪涓嶆柇瀛︿範鎴愰暱锛岃偗瀹氳繕鏈夊緢澶氳瑙d笉鍒颁綅鎴栦笉瀵圭殑鍦版柟锛屾杩庣粰鎴戝叕浼楀彿銆愰珮鎬ц兘鏈嶅姟鏋舵瀯銆戠暀瑷€锛屾垨寰俊鑱旂郴鏈汉锛屽叡鍚屾帰璁ㄥ涔狅紒 ^_^  



以上是关于璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?/h1>

的主要内容,如果未能解决你的问题,请参考以下文章

璋堣皥golang涓殑寮曠敤绫诲瀷涓庡湴鍧€鍒嗛厤

Mock.js绠€鏄撴暀绋嬶紝鑴辩鍚庣鐙珛寮€鍙戯紝瀹炵幇澧炲垹鏀规煡鍔熻兘

闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢

[绉戞櫘]ES6涓€浜涗笉甯歌鐨勫皬鐭ヨ瘑