Skip to content

Commit 9a8cc23

Browse files
committed
nginx: correctly call child_init after starting a worker
1 parent 4e7c0e3 commit 9a8cc23

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

nginx/ngx_http_mapcache_module.c

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ static char *ngx_http_mapcache(ngx_conf_t *cf, ngx_command_t *cmd,
1212
void *conf);
1313

1414
static ngx_command_t ngx_http_mapcache_commands[] = {
15-
1615
{
1716
ngx_string("mapcache"),
1817
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
@@ -25,6 +24,7 @@ static ngx_command_t ngx_http_mapcache_commands[] = {
2524
ngx_null_command
2625
};
2726

27+
/* main configuration, shared across all workers */
2828
typedef struct {
2929
mapcache_context ctx;
3030
ngx_http_request_t *r;
@@ -56,10 +56,6 @@ static mapcache_context* ngx_mapcache_context_clone(mapcache_context *ctx)
5656
static void *
5757
ngx_http_mapcache_create_conf(ngx_conf_t *cf)
5858
{
59-
apr_initialize();
60-
atexit(apr_terminate);
61-
apr_pool_initialize();
62-
apr_pool_create(&process_pool,NULL);
6359
mapcache_context *ctx = apr_pcalloc(process_pool, sizeof(mapcache_ngx_context));
6460
ctx->pool = process_pool;
6561
ctx->connection_pool = NULL;
@@ -72,6 +68,28 @@ ngx_http_mapcache_create_conf(ngx_conf_t *cf)
7268
return ctx;
7369
}
7470

71+
static void *
72+
gx_http_mapcache_create_main_conf(ngx_conf_t *cf)
73+
{
74+
apr_initialize();
75+
atexit(apr_terminate);
76+
apr_pool_initialize();
77+
apr_pool_create(&process_pool,NULL);
78+
ngx_http_mapcache_main_conf_t *mcf;
79+
80+
mcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_mapcache_main_conf_t));
81+
if (mcf == NULL) {
82+
return NULL;
83+
}
84+
85+
mcf->configs = ngx_array_create(cf->pool, 1, sizeof(mapcache_cfg *));
86+
if (mcf->configs == NULL) {
87+
return NULL;
88+
}
89+
90+
return mcf;
91+
}
92+
7593

7694
static void ngx_http_mapcache_write_response(mapcache_context *ctx, ngx_http_request_t *r,
7795
mapcache_http_response *response)
@@ -156,7 +174,7 @@ static ngx_http_module_t ngx_http_mapcache_module_ctx = {
156174
NULL, /* preconfiguration */
157175
NULL, /* postconfiguration */
158176

159-
NULL, /* create main configuration */
177+
ngx_http_mapcache_create_main_conf, /* create main configuration */
160178
NULL, /* init main configuration */
161179

162180
NULL, /* create server configuration */
@@ -168,10 +186,22 @@ static ngx_http_module_t ngx_http_mapcache_module_ctx = {
168186

169187
static ngx_int_t ngx_mapcache_init_process(ngx_cycle_t *cycle)
170188
{
189+
/* this function is called on worker process startup */
171190
apr_initialize();
172191
atexit(apr_terminate);
173192
apr_pool_initialize();
174193
apr_pool_create(&process_pool,NULL);
194+
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);
195+
mapcache_cfg **confs = (mapcache_cfg**)mcf->configs->elts;
196+
int i;
197+
mapcache_context *ctx = apr_pcalloc(process_pool, sizeof(mapcache_ngx_context));
198+
ctx->pool = process_pool;
199+
mapcache_context_init(ctx);
200+
ctx->log = ngx_mapcache_context_log;
201+
ctx->clone = ngx_mapcache_context_clone;
202+
for(i=0; i<mcf->configs->nelts; i++) {
203+
mapcache_cache_child_init(ctx,confs[i],process_pool);
204+
}
175205
return NGX_OK;
176206
}
177207

@@ -300,14 +330,15 @@ ngx_http_mapcache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
300330
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "no mapcache <service>s configured/enabled, no point in continuing.");
301331
return NGX_CONF_ERROR;
302332
}
303-
mapcache_cache_child_init(ctx,ctx->config,ctx->pool);
304-
if(GC_HAS_ERROR(ctx)) {
305-
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,ctx->get_error_message(ctx));
306-
return NGX_CONF_ERROR;
307-
}
308333
mapcache_connection_pool_create(ctx->config, &ctx->connection_pool,ctx->pool);
309334
ctx->config->non_blocking = 1;
310335

336+
/* add the mapcache config to the list of configs to be initialized on worker startup */
337+
ngx_http_mapcache_main_conf_t *mcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_mapcache_module);
338+
mapcache_cfg **pcfg = ngx_array_push(mcf->configs);
339+
*pcfg = ctx->config;
340+
341+
311342
ngx_http_core_loc_conf_t *clcf;
312343

313344
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

0 commit comments

Comments
 (0)