comparison src/stream/ngx_stream.c @ 6693:3908156a51fa

Stream: phases.
author Roman Arutyunyan <arut@nginx.com>
date Thu, 15 Sep 2016 14:55:54 +0300
parents 56fc55e32f23
children ea9dfe2f62e7
comparison
equal deleted inserted replaced
6692:56fc55e32f23 6693:3908156a51fa
10 #include <ngx_event.h> 10 #include <ngx_event.h>
11 #include <ngx_stream.h> 11 #include <ngx_stream.h>
12 12
13 13
14 static char *ngx_stream_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); 14 static char *ngx_stream_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
15 static ngx_int_t ngx_stream_init_phases(ngx_conf_t *cf,
16 ngx_stream_core_main_conf_t *cmcf);
17 static ngx_int_t ngx_stream_init_phase_handlers(ngx_conf_t *cf,
18 ngx_stream_core_main_conf_t *cmcf);
15 static ngx_int_t ngx_stream_add_ports(ngx_conf_t *cf, ngx_array_t *ports, 19 static ngx_int_t ngx_stream_add_ports(ngx_conf_t *cf, ngx_array_t *ports,
16 ngx_stream_listen_t *listen); 20 ngx_stream_listen_t *listen);
17 static char *ngx_stream_optimize_servers(ngx_conf_t *cf, ngx_array_t *ports); 21 static char *ngx_stream_optimize_servers(ngx_conf_t *cf, ngx_array_t *ports);
18 static ngx_int_t ngx_stream_add_addrs(ngx_conf_t *cf, ngx_stream_port_t *stport, 22 static ngx_int_t ngx_stream_add_addrs(ngx_conf_t *cf, ngx_stream_port_t *stport,
19 ngx_stream_conf_addr_t *addr); 23 ngx_stream_conf_addr_t *addr);
217 } 221 }
218 } 222 }
219 } 223 }
220 } 224 }
221 225
226 if (ngx_stream_init_phases(cf, cmcf) != NGX_OK) {
227 return NGX_CONF_ERROR;
228 }
229
222 for (m = 0; cf->cycle->modules[m]; m++) { 230 for (m = 0; cf->cycle->modules[m]; m++) {
223 if (cf->cycle->modules[m]->type != NGX_STREAM_MODULE) { 231 if (cf->cycle->modules[m]->type != NGX_STREAM_MODULE) {
224 continue; 232 continue;
225 } 233 }
226 234
237 return NGX_CONF_ERROR; 245 return NGX_CONF_ERROR;
238 } 246 }
239 247
240 *cf = pcf; 248 *cf = pcf;
241 249
250 if (ngx_stream_init_phase_handlers(cf, cmcf) != NGX_OK) {
251 return NGX_CONF_ERROR;
252 }
242 253
243 if (ngx_array_init(&ports, cf->temp_pool, 4, sizeof(ngx_stream_conf_port_t)) 254 if (ngx_array_init(&ports, cf->temp_pool, 4, sizeof(ngx_stream_conf_port_t))
244 != NGX_OK) 255 != NGX_OK)
245 { 256 {
246 return NGX_CONF_ERROR; 257 return NGX_CONF_ERROR;
253 return NGX_CONF_ERROR; 264 return NGX_CONF_ERROR;
254 } 265 }
255 } 266 }
256 267
257 return ngx_stream_optimize_servers(cf, &ports); 268 return ngx_stream_optimize_servers(cf, &ports);
269 }
270
271
272 static ngx_int_t
273 ngx_stream_init_phases(ngx_conf_t *cf, ngx_stream_core_main_conf_t *cmcf)
274 {
275 if (ngx_array_init(&cmcf->phases[NGX_STREAM_POST_ACCEPT_PHASE].handlers,
276 cf->pool, 1, sizeof(ngx_stream_handler_pt))
277 != NGX_OK)
278 {
279 return NGX_ERROR;
280 }
281
282 if (ngx_array_init(&cmcf->phases[NGX_STREAM_PREACCESS_PHASE].handlers,
283 cf->pool, 1, sizeof(ngx_stream_handler_pt))
284 != NGX_OK)
285 {
286 return NGX_ERROR;
287 }
288
289 if (ngx_array_init(&cmcf->phases[NGX_STREAM_ACCESS_PHASE].handlers,
290 cf->pool, 1, sizeof(ngx_stream_handler_pt))
291 != NGX_OK)
292 {
293 return NGX_ERROR;
294 }
295
296 #if (NGX_STREAM_SSL)
297 if (ngx_array_init(&cmcf->phases[NGX_STREAM_SSL_PHASE].handlers,
298 cf->pool, 1, sizeof(ngx_stream_handler_pt))
299 != NGX_OK)
300 {
301 return NGX_ERROR;
302 }
303 #endif
304
305 if (ngx_array_init(&cmcf->phases[NGX_STREAM_LOG_PHASE].handlers,
306 cf->pool, 1, sizeof(ngx_stream_handler_pt))
307 != NGX_OK)
308 {
309 return NGX_ERROR;
310 }
311
312 return NGX_OK;
313 }
314
315
316 static ngx_int_t
317 ngx_stream_init_phase_handlers(ngx_conf_t *cf,
318 ngx_stream_core_main_conf_t *cmcf)
319 {
320 ngx_int_t j;
321 ngx_uint_t i, n;
322 ngx_stream_handler_pt *h;
323 ngx_stream_phase_handler_t *ph;
324 ngx_stream_phase_handler_pt checker;
325
326 n = 1 /* content phase */;
327
328 for (i = 0; i < NGX_STREAM_LOG_PHASE; i++) {
329 n += cmcf->phases[i].handlers.nelts;
330 }
331
332 ph = ngx_pcalloc(cf->pool,
333 n * sizeof(ngx_stream_phase_handler_t) + sizeof(void *));
334 if (ph == NULL) {
335 return NGX_ERROR;
336 }
337
338 cmcf->phase_engine.handlers = ph;
339 n = 0;
340
341 for (i = 0; i < NGX_STREAM_LOG_PHASE; i++) {
342 h = cmcf->phases[i].handlers.elts;
343
344 switch (i) {
345
346 case NGX_STREAM_CONTENT_PHASE:
347 ph->checker = ngx_stream_core_content_phase;
348 n++;
349 ph++;
350
351 continue;
352
353 default:
354 checker = ngx_stream_core_generic_phase;
355 }
356
357 n += cmcf->phases[i].handlers.nelts;
358
359 for (j = cmcf->phases[i].handlers.nelts - 1; j >= 0; j--) {
360 ph->checker = checker;
361 ph->handler = h[j];
362 ph->next = n;
363 ph++;
364 }
365 }
366
367 return NGX_OK;
258 } 368 }
259 369
260 370
261 static ngx_int_t 371 static ngx_int_t
262 ngx_stream_add_ports(ngx_conf_t *cf, ngx_array_t *ports, 372 ngx_stream_add_ports(ngx_conf_t *cf, ngx_array_t *ports,