From 9a8cc235d3c1287dd4333dc124911da179b14ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C4=81ris=20Narti=C5=A1s?= Date: Tue, 30 Sep 2025 16:50:28 +0300 Subject: [PATCH] nginx: correctly call child_init after starting a worker --- nginx/ngx_http_mapcache_module.c | 53 +++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/nginx/ngx_http_mapcache_module.c b/nginx/ngx_http_mapcache_module.c index 87a220b7..e6ffc9e1 100644 --- a/nginx/ngx_http_mapcache_module.c +++ b/nginx/ngx_http_mapcache_module.c @@ -12,7 +12,6 @@ static char *ngx_http_mapcache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static ngx_command_t ngx_http_mapcache_commands[] = { - { ngx_string("mapcache"), NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, @@ -25,6 +24,7 @@ static ngx_command_t ngx_http_mapcache_commands[] = { ngx_null_command }; +/* main configuration, shared across all workers */ typedef struct { mapcache_context ctx; ngx_http_request_t *r; @@ -56,10 +56,6 @@ static mapcache_context* ngx_mapcache_context_clone(mapcache_context *ctx) static void * ngx_http_mapcache_create_conf(ngx_conf_t *cf) { - apr_initialize(); - atexit(apr_terminate); - apr_pool_initialize(); - apr_pool_create(&process_pool,NULL); mapcache_context *ctx = apr_pcalloc(process_pool, sizeof(mapcache_ngx_context)); ctx->pool = process_pool; ctx->connection_pool = NULL; @@ -72,6 +68,28 @@ ngx_http_mapcache_create_conf(ngx_conf_t *cf) return ctx; } +static void * +gx_http_mapcache_create_main_conf(ngx_conf_t *cf) +{ + apr_initialize(); + atexit(apr_terminate); + apr_pool_initialize(); + apr_pool_create(&process_pool,NULL); + ngx_http_mapcache_main_conf_t *mcf; + + mcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_mapcache_main_conf_t)); + if (mcf == NULL) { + return NULL; + } + + mcf->configs = ngx_array_create(cf->pool, 1, sizeof(mapcache_cfg *)); + if (mcf->configs == NULL) { + return NULL; + } + + return mcf; +} + static void ngx_http_mapcache_write_response(mapcache_context *ctx, ngx_http_request_t *r, mapcache_http_response *response) @@ -156,7 +174,7 @@ static ngx_http_module_t ngx_http_mapcache_module_ctx = { NULL, /* preconfiguration */ NULL, /* postconfiguration */ - NULL, /* create main configuration */ + ngx_http_mapcache_create_main_conf, /* create main configuration */ NULL, /* init main configuration */ NULL, /* create server configuration */ @@ -168,10 +186,22 @@ static ngx_http_module_t ngx_http_mapcache_module_ctx = { static ngx_int_t ngx_mapcache_init_process(ngx_cycle_t *cycle) { + /* this function is called on worker process startup */ apr_initialize(); atexit(apr_terminate); apr_pool_initialize(); apr_pool_create(&process_pool,NULL); + ngx_http_mapcache_main_conf_t *mcf = (ngx_http_mapcache_main_conf_t *)ngx_http_cycle_get_module_main_conf(cycle, ngx_http_mapcache_module); + mapcache_cfg **confs = (mapcache_cfg**)mcf->configs->elts; + int i; + mapcache_context *ctx = apr_pcalloc(process_pool, sizeof(mapcache_ngx_context)); + ctx->pool = process_pool; + mapcache_context_init(ctx); + ctx->log = ngx_mapcache_context_log; + ctx->clone = ngx_mapcache_context_clone; + for(i=0; iconfigs->nelts; i++) { + mapcache_cache_child_init(ctx,confs[i],process_pool); + } return NGX_OK; } @@ -300,14 +330,15 @@ ngx_http_mapcache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "no mapcache s configured/enabled, no point in continuing."); return NGX_CONF_ERROR; } - mapcache_cache_child_init(ctx,ctx->config,ctx->pool); - if(GC_HAS_ERROR(ctx)) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,ctx->get_error_message(ctx)); - return NGX_CONF_ERROR; - } mapcache_connection_pool_create(ctx->config, &ctx->connection_pool,ctx->pool); ctx->config->non_blocking = 1; + /* add the mapcache config to the list of configs to be initialized on worker startup */ + ngx_http_mapcache_main_conf_t *mcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_mapcache_module); + mapcache_cfg **pcfg = ngx_array_push(mcf->configs); + *pcfg = ctx->config; + + ngx_http_core_loc_conf_t *clcf; clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);