Klaus Demo nginx / f9b7db5
Axe several perl interpreter instances: they may be useful in currently unsupported threaded environment, but now they complicate code: *) perl_clone() requires at least duplicating nginx stash; *) the multiplicity requires to re-evalute all precompiled subroutines and nginx stash in new interpreter context. Igor Sysoev 15 years ago
3 changed file(s) with 15 addition(s) and 177 deletion(s). Raw diff Collapse all Expand all
3535 echo " + perl interpreter multiplicity found"
3636 fi
3737
38 if $NGX_PERL -V:useithreads | grep define > /dev/null; then
39 have=NGX_HAVE_PERL_CLONE . auto/have
40 echo " + perl_clone() found"
41
42 else
38 if $NGX_PERL -V:useithreads | grep undef > /dev/null; then
4339 # FreeBSD port wants to link with -pthread non-threaded perl
4440 ngx_perl_ldopts=`echo $ngx_perl_ldopts | sed 's/ -pthread//'`
4541 fi
1010
1111
1212 typedef struct {
13 PerlInterpreter **free_perls;
14 ngx_uint_t interp;
15 ngx_uint_t nalloc;
16 ngx_uint_t interp_max;
17
1813 PerlInterpreter *perl;
1914 ngx_str_t modules;
2015 ngx_array_t requires;
4439 ngx_http_ssi_ctx_t *ssi_ctx, ngx_str_t **params);
4540 #endif
4641
47 static ngx_int_t
48 ngx_http_perl_get_interpreter(ngx_http_perl_main_conf_t *pmcf,
49 PerlInterpreter **perl, ngx_log_t *log);
50 static ngx_inline void
51 ngx_http_perl_free_interpreter(ngx_http_perl_main_conf_t *pmcf,
52 PerlInterpreter *perl);
5342 static char *ngx_http_perl_init_interpreter(ngx_conf_t *cf,
5443 ngx_http_perl_main_conf_t *pmcf);
5544 static PerlInterpreter *
7160 void *conf);
7261 static char *ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
7362 static char *ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
74 static char *ngx_http_perl_interp_max_unsupported(ngx_conf_t *cf, void *post,
75 void *data);
76 #if (NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
63 static void ngx_http_perl_cleanup_sv(void *data);
64
65 #if (NGX_HAVE_PERL_MULTIPLICITY)
7766 static void ngx_http_perl_cleanup_perl(void *data);
7867 #endif
79 static void ngx_http_perl_cleanup_sv(void *data);
80
81
82 static ngx_conf_post_handler_pt ngx_http_perl_interp_max_p =
83 ngx_http_perl_interp_max_unsupported;
8468
8569
8670 static ngx_command_t ngx_http_perl_commands[] = {
9882 NGX_HTTP_MAIN_CONF_OFFSET,
9983 0,
10084 NULL },
101
102 { ngx_string("perl_interp_max"),
103 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
104 ngx_conf_set_num_slot,
105 NGX_HTTP_MAIN_CONF_OFFSET,
106 offsetof(ngx_http_perl_main_conf_t, interp_max),
107 &ngx_http_perl_interp_max_p },
10885
10986 { ngx_string("perl"),
11087 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
228205
229206 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
230207
231 rc = ngx_http_perl_get_interpreter(pmcf, &ctx->perl, r->connection->log);
232
233 if (rc != NGX_OK) {
234 ngx_http_finalize_request(r, rc);
235 return;
236 }
237
238208 {
239209
240 dTHXa(ctx->perl);
210 dTHXa(pmcf->perl);
241211
242212 if (ctx->next == NULL) {
243213 plcf = ngx_http_get_module_loc_conf(r, ngx_http_perl_module);
254224
255225 }
256226
257 ngx_http_perl_free_interpreter(pmcf, ctx->perl);
258
259227 if (rc > 600) {
260228 rc = NGX_OK;
261229 }
299267 ngx_http_perl_variable_t *pv = (ngx_http_perl_variable_t *) data;
300268
301269 ngx_int_t rc;
302 ngx_uint_t recursive;
303270 ngx_str_t value;
304271 ngx_http_perl_ctx_t *ctx;
305272 ngx_http_perl_main_conf_t *pmcf;
316283 }
317284
318285 ngx_http_set_ctx(r, ctx, ngx_http_perl_module);
319
320 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
321
322 rc = ngx_http_perl_get_interpreter(pmcf, &ctx->perl,
323 r->connection->log);
324
325 if (rc != NGX_OK) {
326 return rc;
327 }
328
329 recursive = 0;
330
331 } else {
332 pmcf = NULL;
333 recursive = 1;
334 }
286 }
287
288 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
335289
336290 value.data = NULL;
337291
338292 {
339293
340 dTHXa(ctx->perl);
294 dTHXa(pmcf->perl);
341295
342296 rc = ngx_http_perl_call_handler(aTHX_ r, pv->sub, NULL,
343297 &pv->handler, &value);
344298
345 }
346
347 if (recursive == 0) {
348 ngx_http_perl_free_interpreter(pmcf, ctx->perl);
349299 }
350300
351301 if (value.data) {
384334 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
385335 "perl ssi handler");
386336
387 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
388
389337 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module);
390338
391339 if (ctx == NULL) {
397345 ngx_http_set_ctx(r, ctx, ngx_http_perl_module);
398346 }
399347
400 rc = ngx_http_perl_get_interpreter(pmcf, &ctx->perl, r->connection->log);
401
402 if (rc != NGX_OK) {
403 return rc;
404 }
348 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module);
405349
406350 ctx->ssi = ssi_ctx;
407351
410354
411355 {
412356
413 dTHXa(ctx->perl);
357 dTHXa(pmcf->perl);
414358
415359 #if 0
416360
439383
440384 }
441385
442 ngx_http_perl_free_interpreter(pmcf, ctx->perl);
443
444386 ctx->filename.data = NULL;
445387 ctx->redirect_uri.len = 0;
446388 ctx->ssi = NULL;
453395 #endif
454396
455397
456 static ngx_int_t
457 ngx_http_perl_get_interpreter(ngx_http_perl_main_conf_t *pmcf,
458 PerlInterpreter **perl, ngx_log_t *log)
459 {
460 if (pmcf->interp) {
461 pmcf->interp--;
462
463 *perl = pmcf->free_perls[pmcf->interp];
464
465 return NGX_OK;
466 }
467
468 if (pmcf->nalloc < pmcf->interp_max) {
469 *perl = ngx_http_perl_create_interpreter(pmcf, log);
470
471 if (*perl) {
472 return NGX_OK;
473 }
474
475 return NGX_HTTP_INTERNAL_SERVER_ERROR;
476 }
477
478 ngx_log_error(NGX_LOG_ALERT, log, 0, "no free perl interpreter");
479
480 return NGX_HTTP_SERVICE_UNAVAILABLE;
481 }
482
483
484 static ngx_inline void
485 ngx_http_perl_free_interpreter(ngx_http_perl_main_conf_t *pmcf,
486 PerlInterpreter *perl)
487 {
488 pmcf->free_perls[pmcf->interp++] = perl;
489 }
490
491
492398 static char *
493399 ngx_http_perl_init_interpreter(ngx_conf_t *cf, ngx_http_perl_main_conf_t *pmcf)
494400 {
495 #if (NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
401 #if (NGX_HAVE_PERL_MULTIPLICITY)
496402 ngx_pool_cleanup_t *cln;
497403
498404 cln = ngx_pool_cleanup_add(cf->pool, 0);
516422 }
517423 }
518424
519 #if !(NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
425 #if !(NGX_HAVE_PERL_MULTIPLICITY)
520426
521427 if (perl) {
522428 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log)
541447 return NGX_CONF_ERROR;
542448 }
543449
544 #if (NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
450 #if (NGX_HAVE_PERL_MULTIPLICITY)
545451
546452 cln->handler = ngx_http_perl_cleanup_perl;
547453 cln->data = pmcf->perl;
568474
569475 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "create perl interpreter");
570476
571 #if (NGX_HAVE_PERL_CLONE)
572
573 if (pmcf->perl) {
574
575 perl = perl_clone(pmcf->perl, CLONEf_KEEP_PTR_TABLE);
576 if (perl == NULL) {
577 ngx_log_error(NGX_LOG_ALERT, log, 0, "perl_clone() failed");
578 return NULL;
579 }
580
581 {
582
583 dTHXa(perl);
584
585 ptr_table_free(PL_ptr_table);
586 PL_ptr_table = NULL;
587
588 }
589
590 pmcf->nalloc++;
591
592 return perl;
593 }
594
595 #endif
596
597477 perl = perl_alloc();
598478 if (perl == NULL) {
599479 ngx_log_error(NGX_LOG_ALERT, log, 0, "perl_alloc() failed");
647527 }
648528
649529 }
650
651 pmcf->nalloc++;
652530
653531 return perl;
654532
823701 return NGX_CONF_ERROR;
824702 }
825703
826 pmcf->interp_max = NGX_CONF_UNSET_UINT;
827
828704 if (ngx_array_init(&pmcf->requires, cf->pool, 1, sizeof(u_char *))
829705 != NGX_OK)
830706 {
840716 {
841717 ngx_http_perl_main_conf_t *pmcf = conf;
842718
843 #if (NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
844 ngx_conf_init_uint_value(pmcf->interp_max, 10);
845 #else
846 ngx_conf_init_uint_value(pmcf->interp_max, 1);
847 #endif
848
849 pmcf->free_perls = ngx_pcalloc(cf->pool,
850 pmcf->interp_max * sizeof(PerlInterpreter *));
851 if (pmcf->free_perls == NULL) {
852 return NGX_CONF_ERROR;
853 }
854
855719 if (pmcf->perl == NULL) {
856720 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) {
857721 return NGX_CONF_ERROR;
858722 }
859723 }
860724
861 #if !(NGX_HAVE_PERL_CLONE)
862 ngx_http_perl_free_interpreter(pmcf, pmcf->perl);
863 #endif
864
865725 return NGX_CONF_OK;
866726 }
867727
868728
869 #if (NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
729 #if (NGX_HAVE_PERL_MULTIPLICITY)
870730
871731 static void
872732 ngx_http_perl_cleanup_perl(void *data)
1122982
1123983 return NGX_CONF_OK;
1124984 }
1125
1126
1127 static char *
1128 ngx_http_perl_interp_max_unsupported(ngx_conf_t *cf, void *post, void *data)
1129 {
1130 #if (NGX_HAVE_PERL_CLONE || NGX_HAVE_PERL_MULTIPLICITY)
1131
1132 return NGX_CONF_OK;
1133
1134 #else
1135
1136 return "to use perl_interp_max you have to build perl with "
1137 "-Dusemultiplicity or -Dusethreads options";
1138
1139 #endif
1140 }
1919 typedef ngx_http_request_t *nginx;
2020
2121 typedef struct {
22 PerlInterpreter *perl;
23
2422 ngx_str_t filename;
2523 ngx_str_t redirect_uri;
2624 ngx_str_t redirect_args;