璋堣皥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>
闈㈣瘯杩囩▼涓紝缁忓父浼氶棶鍒帮紝浣犱滑瀹㈡埛绔殑骞跺彂閲忔槸澶氬皯锛?/span>鏈夊摢浜涚摱棰堬紵鍋氭湇鍔$寮€鍙戠殑绋嬪簭鍛橀兘鐭ラ亾锛屽苟鍙戦噺鏄湇鍔$鑳藉悓鏃舵壙杞藉鎴风鐨勬暟閲忋€?/span>鏈嶅姟鍣ㄥ鐞嗗鎴风璇锋眰锛?/span>绾跨▼鐨勫垱寤哄拰閿€姣侊紝閮芥秹鍙婂埌绯荤粺璋冪敤锛屾瘮杈冩秷鑰楃郴缁熻祫婧愶紝閬垮厤棰戠箒鐨勭嚎绋嬪垱寤哄拰閿€姣佸氨鏄惧緱闈炲父蹇呰銆傞偅涔堬紝浠€涔堟槸绾跨▼姹狅紵绾跨▼姹犲伐浣滃師鐞嗘槸浠€涔堬紵绾跨▼姹犵殑鍔熻兘缁勪欢鏈夊摢浜涳紵搴旂敤鍦烘櫙鏈夊摢浜涳紵绛夌瓑
涓€銆佺嚎绋嬫睜鐨勫師鐞?/span>
1銆?/span>浠€涔堟槸绾跨▼姹?/span>
绾跨▼姹犳槸鍒涘缓鑻ュ共涓彲鎵ц鐨勭嚎绋嬫斁鍏ヤ竴涓睜锛堝鍣級涓紝鏈変换鍔¢渶瑕佸鐞嗘椂锛屼細鎻愪氦鍒扮嚎绋嬫睜涓殑浠诲姟闃熷垪锛屽鐞嗗畬涔嬪悗绾跨▼骞朵笉浼氳閿€姣侊紝鑰屾槸浠嶇劧鍦ㄧ嚎绋嬫睜涓瓑寰呬笅涓€涓换鍔°€?/span>
2銆?/em>Nginx涓轰粈涔堥渶瑕佺嚎绋嬫睜
涓轰簡鍦?/span>璋冪敤鎿嶄綔绯荤粺鍐呮牳鐨?/span>API杩囩▼涓?/span>锛屾搷浣滅郴缁熻涓虹嚎绋嬪垎閰嶄竴绯诲垪鐨勮祫婧愶紝鑰岃繖浜涙垚鏈緢楂橈紝鍙堝洜绾跨▼鏄竴涓噸閲忕骇鐨勫璞★紝闇€瑕侀伩鍏嶉绻佸垱寤哄拰閿€姣侊紝鍥犳寮曞叆绾跨▼姹犮€?/span>
3銆?/span>Nginx绾跨▼姹犵殑搴旂敤鍦烘櫙
绾跨▼姹犲湪鎴戜滑鍐欎笟鍔′唬鐮佽繃绋嬩腑杩愮敤姣旇緝澶氾紝杩樻湁I/O寮傛鎿嶄綔銆佹棩蹇楃殑瀛樺偍闇€瑕佸簲鐢ㄧ嚎绋嬫睜銆?/span>
4銆?/span>纾佺洏缁撴瀯
5銆?/span>Nginx璇锋眰澶勭悊娴佺▼
6銆?/span>绾跨▼姹犵殑鎵ц娴佺▼
7銆?/span>绾跨▼姹犵殑宸ヤ綔娴佺▼
8銆?/span>绾跨▼姹犵殑鍔熻兘缁勪欢
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, ¶ms);
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>
Posted 楂樻€ц兘鏈嶅姟鏋舵瀯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?/h1>
閲嶇骞茶揣锛岀涓€鏃堕棿閫佽揪
鉂?/span>
娌夋穩銆佸垎浜€佹垚闀匡紝璁╄嚜宸卞拰浠栦汉閮借兘鏈夋墍鏀惰幏锛侌煒?/p>
鉂?/span>
鉂?/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>
闈㈣瘯杩囩▼涓紝缁忓父浼氶棶鍒帮紝浣犱滑瀹㈡埛绔殑骞跺彂閲忔槸澶氬皯锛?/span>鏈夊摢浜涚摱棰堬紵鍋氭湇鍔$寮€鍙戠殑绋嬪簭鍛橀兘鐭ラ亾锛屽苟鍙戦噺鏄湇鍔$鑳藉悓鏃舵壙杞藉鎴风鐨勬暟閲忋€?/span>鏈嶅姟鍣ㄥ鐞嗗鎴风璇锋眰锛?/span>绾跨▼鐨勫垱寤哄拰閿€姣侊紝閮芥秹鍙婂埌绯荤粺璋冪敤锛屾瘮杈冩秷鑰楃郴缁熻祫婧愶紝閬垮厤棰戠箒鐨勭嚎绋嬪垱寤哄拰閿€姣佸氨鏄惧緱闈炲父蹇呰銆傞偅涔堬紝浠€涔堟槸绾跨▼姹狅紵绾跨▼姹犲伐浣滃師鐞嗘槸浠€涔堬紵绾跨▼姹犵殑鍔熻兘缁勪欢鏈夊摢浜涳紵搴旂敤鍦烘櫙鏈夊摢浜涳紵绛夌瓑
涓€銆佺嚎绋嬫睜鐨勫師鐞?/span>
1銆?/span>浠€涔堟槸绾跨▼姹?/span>
2銆?/em>Nginx涓轰粈涔堥渶瑕佺嚎绋嬫睜
3銆?/span>Nginx绾跨▼姹犵殑搴旂敤鍦烘櫙
绾跨▼姹犲湪鎴戜滑鍐欎笟鍔′唬鐮佽繃绋嬩腑杩愮敤姣旇緝澶氾紝杩樻湁I/O寮傛鎿嶄綔銆佹棩蹇楃殑瀛樺偍闇€瑕佸簲鐢ㄧ嚎绋嬫睜銆?/span>
4銆?/span>纾佺洏缁撴瀯 5銆?/span>Nginx璇锋眰澶勭悊娴佺▼ 6銆?/span>绾跨▼姹犵殑鎵ц娴佺▼ 7銆?/span>绾跨▼姹犵殑宸ヤ綔娴佺▼ 8銆?/span>绾跨▼姹犵殑鍔熻兘缁勪欢 1. 鎵ц绾跨▼鍖呭惈绾跨▼ID銆佺粓姝㈡爣璇嗐€佹睜绠$悊缁勪欢瀵硅薄銆?/span> 2. 绾跨▼涓墽琛岀殑浠诲姟鍖呭惈浠诲姟鍥炶皟鍑芥暟锛屼换鍔℃墽琛岀殑鍙傛暟銆?/span> 3. 姹犵鐞嗙粍浠讹細浜掓枼閿侊紝鏉′欢鍙橀噺锛屾墽琛岄槦鍒楋紝浠诲姟闃熷垪銆?/span> 浜屻€佷唬鐮佸疄鐜扮嚎绋嬫睜 1銆?/span>绾跨▼姹犵殑鍒涘缓 4銆?/span>push浠诲姟 5銆?/span>绾跨▼姹犵殑娴嬭瘯绋嬪簭 涓夈€佺嚎绋嬫睜灏佽鎴愭ā鍧?/span> 鍥涖€佺郴鍒楁帹鑽?/span> 以上是关于璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?/h1>
typedef struct NWORKER {
pthread_t thread;
int terminate;
struct NWORKQUEUE *workqueue;
struct NWORKER *prev;
struct NWORKER *next;
} nWorker;
typedef struct NJOB {
void (*job_function)(struct NJOB *job);
void *user_data;
struct NJOB *prev;
struct NJOB *next;
} nJob;
typedef struct NWORKQUEUE {
struct NWORKER *workers;
struct NJOB *waiting_jobs;
pthread_mutex_t jobs_mtx;
pthread_cond_t jobs_cond;
} nWorkQueue;
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;
}
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);
}
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);
}
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);
}
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, ¶ms);
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;