Mercurial > hg > ngx_http_delay_module
annotate ngx_http_delay_module.c @ 2:aee676aa6c1f default tip
Fixed delaying subrequests.
Similar to 7fcf209d40c8 in the limit_req module, we now set wev->delayed
to avoid interference with other subrequests.
With 903fb1ddc07f in nginx (wev->delayed handling moved to the connection
event handler, nginx 1.11.13), it will be also possible to remove
wev->timedout tests and wev->timedout / wev->delayed reset after the timer
expiration. These are preserved for now to make the code compatible with
older nginx versions.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 29 May 2017 18:28:58 +0300 |
parents | 9777bf89da35 |
children |
rev | line source |
---|---|
0 | 1 |
2 /* | |
3 * Copyright (C) Maxim Dounin | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 | |
12 typedef struct { | |
13 ngx_msec_t delay; | |
14 } ngx_http_delay_conf_t; | |
15 | |
16 | |
17 static void ngx_http_delay_event_handler(ngx_http_request_t *r); | |
18 | |
19 | |
20 static void *ngx_http_delay_create_conf(ngx_conf_t *cf); | |
21 static char *ngx_http_delay_merge_conf(ngx_conf_t *cf, void *parent, | |
22 void *child); | |
23 static ngx_int_t ngx_http_delay_init(ngx_conf_t *cf); | |
24 | |
25 | |
26 static ngx_command_t ngx_http_delay_commands[] = { | |
27 | |
28 { ngx_string("delay"), | |
29 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
30 ngx_conf_set_msec_slot, | |
31 NGX_HTTP_LOC_CONF_OFFSET, | |
32 offsetof(ngx_http_delay_conf_t, delay), | |
33 NULL }, | |
34 | |
35 ngx_null_command | |
36 }; | |
37 | |
38 | |
39 static ngx_http_module_t ngx_http_delay_module_ctx = { | |
40 NULL, /* preconfiguration */ | |
41 ngx_http_delay_init, /* postconfiguration */ | |
42 | |
43 NULL, /* create main configuration */ | |
44 NULL, /* init main configuration */ | |
45 | |
46 NULL, /* create server configuration */ | |
47 NULL, /* merge server configuration */ | |
48 | |
49 ngx_http_delay_create_conf, /* create location configration */ | |
50 ngx_http_delay_merge_conf /* merge location configration */ | |
51 }; | |
52 | |
53 | |
54 ngx_module_t ngx_http_delay_module = { | |
55 NGX_MODULE_V1, | |
56 &ngx_http_delay_module_ctx, /* module context */ | |
57 ngx_http_delay_commands, /* module directives */ | |
58 NGX_HTTP_MODULE, /* module type */ | |
59 NULL, /* init master */ | |
60 NULL, /* init module */ | |
61 NULL, /* init process */ | |
62 NULL, /* init thread */ | |
63 NULL, /* exit thread */ | |
64 NULL, /* exit process */ | |
65 NULL, /* exit master */ | |
66 NGX_MODULE_V1_PADDING | |
67 }; | |
68 | |
69 | |
70 static ngx_int_t | |
71 ngx_http_delay_handler(ngx_http_request_t *r) | |
72 { | |
73 ngx_http_delay_conf_t *dcf; | |
74 | |
75 if (ngx_http_get_module_ctx(r, ngx_http_delay_module) != NULL) { | |
76 return NGX_DECLINED; | |
77 } | |
78 | |
79 dcf = ngx_http_get_module_loc_conf(r, ngx_http_delay_module); | |
80 | |
81 if (dcf->delay == NGX_CONF_UNSET_MSEC) { | |
82 return NGX_DECLINED; | |
83 } | |
84 | |
85 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
86 "delaying request"); | |
87 | |
88 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) { | |
89 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
90 } | |
91 | |
92 r->read_event_handler = ngx_http_test_reading; | |
93 r->write_event_handler = ngx_http_delay_event_handler; | |
94 | |
2
aee676aa6c1f
Fixed delaying subrequests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
95 r->connection->write->delayed = 1; |
0 | 96 ngx_add_timer(r->connection->write, dcf->delay); |
97 | |
98 ngx_http_set_ctx(r, (void *) 1, ngx_http_delay_module); | |
99 | |
100 return NGX_AGAIN; | |
101 } | |
102 | |
103 | |
104 static void | |
105 ngx_http_delay_event_handler(ngx_http_request_t *r) | |
106 { | |
107 ngx_event_t *wev; | |
108 | |
109 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
110 "delay"); | |
111 | |
112 wev = r->connection->write; | |
113 | |
2
aee676aa6c1f
Fixed delaying subrequests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
114 if (wev->delayed && !wev->timedout) { |
0 | 115 |
116 if (ngx_handle_write_event(wev, 0) != NGX_OK) { | |
117 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
118 } | |
119 | |
120 return; | |
121 } | |
122 | |
2
aee676aa6c1f
Fixed delaying subrequests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
123 wev->delayed = 0; |
0 | 124 wev->timedout = 0; |
125 | |
126 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) { | |
127 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
128 return; | |
129 } | |
130 | |
131 r->read_event_handler = ngx_http_block_reading; | |
132 r->write_event_handler = ngx_http_core_run_phases; | |
133 | |
134 ngx_http_core_run_phases(r); | |
135 } | |
136 | |
137 | |
138 static void * | |
139 ngx_http_delay_create_conf(ngx_conf_t *cf) | |
140 { | |
141 ngx_http_delay_conf_t *conf; | |
142 | |
143 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_delay_conf_t)); | |
144 if (conf == NULL) { | |
145 return NULL; | |
146 } | |
147 | |
148 /* | |
149 * set by ngx_pcalloc(): | |
150 * | |
151 */ | |
152 | |
153 conf->delay = NGX_CONF_UNSET_MSEC; | |
154 | |
155 return conf; | |
156 } | |
157 | |
158 | |
159 static char * | |
160 ngx_http_delay_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
161 { | |
162 ngx_http_delay_conf_t *prev = parent; | |
163 ngx_http_delay_conf_t *conf = child; | |
164 | |
165 ngx_conf_merge_msec_value(conf->delay, prev->delay, | |
166 NGX_CONF_UNSET_MSEC); | |
167 | |
168 return NGX_CONF_OK; | |
169 } | |
170 | |
171 | |
172 static ngx_int_t | |
173 ngx_http_delay_init(ngx_conf_t *cf) | |
174 { | |
175 ngx_http_handler_pt *h; | |
176 ngx_http_core_main_conf_t *cmcf; | |
177 | |
178 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
179 | |
180 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers); | |
181 if (h == NULL) { | |
182 return NGX_ERROR; | |
183 } | |
184 | |
185 *h = ngx_http_delay_handler; | |
186 | |
187 return NGX_OK; | |
188 } |