nginx 이벤트 모듈 - 첫 번 째 편
이벤트 메커니즘
다음은 nginx 이벤트 와 관련 된 설정 입 니 다. 다음 과 같 습 니 다.
1events {
2    worker_connections  1024;
3    use epoll;
4}
우 리 는 epoll 체 제 를 명확 하 게 사용 했다. nginx 에서 사건 과 관련 된 모듈 은 모두 세 개 로 각각
ngx_events_module, ngx_event_core_module, ngx_epoll_module 이다.이 글 은 모듈 을 소개 합 니 다.ngx_events_module
이 모듈 은 nginx 에서 이벤트 메커니즘 을 도입 한 모듈 입 니 다.
ngx_events_module 파일 에서 ngx_events.c 와 관련 된 설정 을 찾 을 수 있 습 니 다. 다음 과 같 습 니 다. 1static ngx_command_t  ngx_events_commands[] = {
 2
 3    { ngx_string("events"),
 4      NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
 5      ngx_events_block,
 6      0,
 7      0,
 8      NULL },
 9
10      ngx_null_command
11};
12
13
14static ngx_core_module_t  ngx_events_module_ctx = {
15    ngx_string("events"),
16    NULL,
17    ngx_event_init_conf
18};
19
20
21ngx_module_t  ngx_events_module = {
22    NGX_MODULE_V1,
23    &ngx_events_module_ctx,                /* module context */
24    ngx_events_commands,                   /* module directives */
25    NGX_CORE_MODULE,                       /* module type */
26    NULL,                                  /* init master */
27    NULL,                                  /* init module */
28    NULL,                                  /* init process */
29    NULL,                                  /* init thread */
30    NULL,                                  /* exit thread */
31    NULL,                                  /* exit process */
32    NULL,                                  /* exit master */
33    NGX_MODULE_V1_PADDING
34};
35
36typedef struct {
37    ngx_str_t             name;
38    void               *(*create_conf)(ngx_cycle_t *cycle);
39    char               *(*init_conf)(ngx_cycle_t *cycle, void *conf);
40} ngx_core_module_t;
위의 설정 에서 우 리 는 다음 과 같은 정 보 를 얻 을 수 있 습 니 다.
ngx_events_module 는 핵심 모듈 ngx_events_module 유형  이다.NGX_CORE_MODULE 하나의 명령 만 해석 합 니 다. 즉 ngx_events_module 이 events 명령 이 고 인자  가 없습니다.NGX_BLOCK 의 ngx_events_module 함 수 는 비어 있 고 create_conf() 함수  만 있 습 니 다.init_conf() 명령 을 만 났 을 때 events 함 수 를 호출 하여 분석 처리 이벤트 명령 어 분석우 리 는 앞의 글 에서 프로필 의 해석 이
ngx_event_block() 함수 에서 이 루어 졌 다 고 소개 한 적 이 있다.우 리 는 다시 이 부분의 코드 를 떼 어 냈 다. 다음 과 같다.1cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));
2    if (cycle->conf_ctx == NULL) {
3        ngx_destroy_pool(pool);
4        return NULL;
5    }
여기에
ngx_init_cycle() 의 메모리 공간 을 분배 한 다음 에 다음 과 같은 코드 를 실행 합 니 다. 1for (i = 0; cycle->modules[i]; i++) {
 2        if (cycle->modules[i]->type != NGX_CORE_MODULE) {
 3            continue;
 4        }
 5
 6        module = cycle->modules[i]->ctx;
 7
 8        if (module->create_conf) {
 9            rv = module->create_conf(cycle);
10            if (rv == NULL) {
11                ngx_destroy_pool(pool);
12                return NULL;
13            }
14            cycle->conf_ctx[cycle->modules[i]->index] = rv;
15        }
16    }
모든
conf_ctx 유형의 모듈 을 옮 겨 다 니 며 그들의 NGX_CORE_MODULE 방법 을 호출 하고 create_conf() 에 값 을 부여 합 니 다. 위 에서 분석 한 바 와 같이 cycle->conf_ctx 방법 이 없 기 때문에 이 부분 코드 는 ngx_event_module 에 영향 을 주지 않 습 니 다.ngx_events_block
다음은 우리 가
create_conf() 함 수 를 분석 해 보 겠 습 니 다. 이 함수 의 역할 은 바로 해석 ngx_event_module 명령 입 니 다. 코드 는 다음 과 같 습 니 다. 1static char *
 2ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 3{
 4    char                 *rv;
 5    void               ***ctx;
 6    ngx_uint_t            i;
 7    ngx_conf_t            pcf;
 8    ngx_event_module_t   *m;
 9
10    if (*(void **) conf) {
11        return "is duplicate";
12    }
13
14    /* count the number of the event modules and set up their indices */
15
16    ngx_event_max_module = ngx_count_modules(cf->cycle, NGX_EVENT_MODULE);
17
18    ctx = ngx_pcalloc(cf->pool, sizeof(void *));
19    if (ctx == NULL) {
20        return NGX_CONF_ERROR;
21    }
22
23    *ctx = ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *));
24    if (*ctx == NULL) {
25        return NGX_CONF_ERROR;
26    }
27
28    *(void **) conf = ctx;
29
30    for (i = 0; cf->cycle->modules[i]; i++) {
31        if (cf->cycle->modules[i]->type != NGX_EVENT_MODULE) {
32            continue;
33        }
34
35        m = cf->cycle->modules[i]->ctx;
36
37        if (m->create_conf) {
38            (*ctx)[cf->cycle->modules[i]->ctx_index] =
39                                                     m->create_conf(cf->cycle);
40            if ((*ctx)[cf->cycle->modules[i]->ctx_index] == NULL) {
41                return NGX_CONF_ERROR;
42            }
43        }
44    }
45
46    pcf = *cf;
47    cf->ctx = ctx;
48    cf->module_type = NGX_EVENT_MODULE;
49    cf->cmd_type = NGX_EVENT_CONF;
50
51    rv = ngx_conf_parse(cf, NULL);
52
53    *cf = pcf;
54
55    if (rv != NGX_CONF_OK) {
56        return rv;
57    }
58
59    for (i = 0; cf->cycle->modules[i]; i++) {
60        if (cf->cycle->modules[i]->type != NGX_EVENT_MODULE) {
61            continue;
62        }
63
64        m = cf->cycle->modules[i]->ctx;
65
66        if (m->init_conf) {
67            rv = m->init_conf(cf->cycle,
68                              (*ctx)[cf->cycle->modules[i]->ctx_index]);
69            if (rv != NGX_CONF_OK) {
70                return rv;
71            }
72        }
73    }
74
75    return NGX_CONF_OK;
76}
이 함수 의 실행 절 차 는 다음 과 같다.
현재 몇 개의
ngx_events_block() 유형의 모듈 이 있 는 지 계산 합 니 다. 여기 있 는 예 에는 두 개의 이 유형의 모듈  이 있 습 니 다.메모리 공간 할당 모든
events 유형 모듈 을 호출 하 는 NGX_EVENT_MODULE 방법 재 귀 분석 NGX_EVENT_MODULE 블록 명령 의 내부 명령, 예 를 들 어 create_conf(), events 등 명령 모든 use 유형 모듈 을 호출 하 는 worker_connections 방법 위 가 바로 NGX_EVENT_MODULE 방법의 집행 절차 이다.이 방법 은 매우 간단 하 다. init_conf() 와 ngx_events_block() 에 관련 되 기 때문에 다음 절 에 우 리 는 이 두 사건 모듈 을 상세 하 게 소개 한다.본문 을 좋아 하 는 여러분, 다음 그림 에 따라 구독 번호 정 르 도스 에 관심 을 가 져 주 십시오. 더 많은 재 미 있 는 내용 을 정 르 도스 에 게 보 내 는 것 을 환영 합 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.