Skip to content

Commit ac2ac10

Browse files
authored
Add child init hook for cache backends (#290)
* Some cache backends might need to call specific code at startup of a process. In the case of Apache mpm_event, child_init is executed after process start (fork) and before starting any threads that will do actual request processing. * Use single noop child init function for all cache backends * mapcache_cache_child_init needs a DLL export to not cause linking problems on Windows
1 parent 882c009 commit ac2ac10

20 files changed

+53
-0
lines changed

apache/mod_mapcache.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,17 @@ static int write_http_response(mapcache_context_apache_request *ctx, mapcache_ht
307307

308308
static void mod_mapcache_child_init(apr_pool_t *pool, server_rec *s)
309309
{
310+
mapcache_context *ctx;
311+
ctx = (mapcache_context*)create_apache_server_context(s,pool);
310312
for( ; s ; s=s->next) {
311313
mapcache_server_cfg* cfg = ap_get_module_config(s->module_config, &mapcache_module);
312314
int i,rv;
313315
for(i=0;i<cfg->aliases->nelts;i++) {
314316
mapcache_alias_entry *alias_entry = APR_ARRAY_IDX(cfg->aliases,i,mapcache_alias_entry*);
317+
mapcache_cache_child_init(ctx,alias_entry->cfg,pool);
318+
if (GC_HAS_ERROR(ctx)) {
319+
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, "%s", ctx->get_error_message(ctx));
320+
}
315321
rv = mapcache_connection_pool_create(alias_entry->cfg, &(alias_entry->cp),pool);
316322
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating a child process mapcache connection pool on server %s for alias %s", s->server_hostname, alias_entry->endpoint);
317323
if(rv!=APR_SUCCESS) {
@@ -320,6 +326,10 @@ static void mod_mapcache_child_init(apr_pool_t *pool, server_rec *s)
320326
}
321327
for(i=0;i<cfg->quickaliases->nelts;i++) {
322328
mapcache_alias_entry *alias_entry = APR_ARRAY_IDX(cfg->quickaliases,i,mapcache_alias_entry*);
329+
mapcache_cache_child_init(ctx,alias_entry->cfg,pool);
330+
if (GC_HAS_ERROR(ctx)) {
331+
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, "%s", ctx->get_error_message(ctx));
332+
}
323333
rv = mapcache_connection_pool_create(alias_entry->cfg, &(alias_entry->cp),pool);
324334
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating a child process mapcache connection pool on server %s for alias %s", s->server_hostname, alias_entry->endpoint);
325335
if(rv!=APR_SUCCESS) {

cgi/mapcache.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ static void load_config(mapcache_context *ctx, char *filename)
213213
apr_pool_destroy(config_pool);
214214
}
215215
config_pool = tmp_config_pool;
216+
mapcache_cache_child_init(ctx,cfg,config_pool);
217+
if (GC_HAS_ERROR(ctx)) goto failed_load;
216218
mapcache_connection_pool_create(cfg, &ctx->connection_pool, config_pool);
217219

218220
return;

contrib/mapcache_detail/mapcache_detail.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,8 @@ int main(int argc, char * argv[])
714714
mapcache_context_init(&ctx);
715715
ctx.config = mapcache_configuration_create(ctx.pool);
716716
ctx.log = mapcache_log;
717+
mapcache_cache_child_init(&ctx,ctx.config,ctx.pool);
718+
if (GC_HAS_ERROR(&ctx)) goto failure;
717719
mapcache_connection_pool_create(ctx.config, &ctx.connection_pool, ctx.pool);
718720

719721

include/mapcache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ struct mapcache_cache {
369369

370370
void (*configuration_parse_xml)(mapcache_context *ctx, ezxml_t xml, mapcache_cache * cache, mapcache_cfg *config);
371371
void (*configuration_post_config)(mapcache_context *ctx, mapcache_cache * cache, mapcache_cfg *config);
372+
void (*child_init)(mapcache_context *ctx, mapcache_cache *cache, apr_pool_t *pchild);
372373
};
373374

374375
MS_DLL_EXPORT int mapcache_cache_tile_get(mapcache_context *ctx, mapcache_cache *cache, mapcache_tile *tile);
@@ -377,6 +378,9 @@ MS_DLL_EXPORT int mapcache_cache_tile_exists(mapcache_context *ctx, mapcache_cac
377378
MS_DLL_EXPORT void mapcache_cache_tile_set(mapcache_context *ctx, mapcache_cache *cache, mapcache_tile *tile);
378379
void mapcache_cache_tile_multi_set(mapcache_context *ctx, mapcache_cache *cache, mapcache_tile *tiles, int ntiles);
379380

381+
MS_DLL_EXPORT void mapcache_cache_child_init(mapcache_context *ctx, mapcache_cfg *config, apr_pool_t *pchild);
382+
static inline void mapcache_cache_child_init_noop(mapcache_context *ctx, mapcache_cache *cache, apr_pool_t *pchild) {
383+
};
380384

381385

382386
/**

lib/cache.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,16 @@ void mapcache_cache_tile_multi_set(mapcache_context *ctx, mapcache_cache *cache,
174174
}
175175
}
176176
}
177+
178+
void mapcache_cache_child_init(mapcache_context *ctx, mapcache_cfg *config, apr_pool_t *pchild)
179+
{
180+
apr_hash_index_t *cachei = apr_hash_first(pchild,config->caches);
181+
while(cachei) {
182+
mapcache_cache *cache;
183+
const void *key;
184+
apr_ssize_t keylen;
185+
apr_hash_this(cachei,&key,&keylen,(void**)&cache);
186+
cache->child_init(ctx,cache,pchild);
187+
cachei = apr_hash_next(cachei);
188+
}
189+
}

lib/cache_bdb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ mapcache_cache* mapcache_cache_bdb_create(mapcache_context *ctx)
417417
cache->cache._tile_multi_set = _mapcache_cache_bdb_multiset;
418418
cache->cache.configuration_post_config = _mapcache_cache_bdb_configuration_post_config;
419419
cache->cache.configuration_parse_xml = _mapcache_cache_bdb_configuration_parse_xml;
420+
cache->cache.child_init = mapcache_cache_child_init_noop;
420421
cache->basedir = NULL;
421422
cache->key_template = NULL;
422423
return (mapcache_cache*)cache;

lib/cache_composite.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,5 +262,6 @@ mapcache_cache* mapcache_cache_composite_create(mapcache_context *ctx)
262262
cache->cache._tile_multi_set = _mapcache_cache_composite_tile_multi_set;
263263
cache->cache.configuration_post_config = _mapcache_cache_composite_configuration_post_config;
264264
cache->cache.configuration_parse_xml = _mapcache_cache_composite_configuration_parse_xml;
265+
cache->cache.child_init = mapcache_cache_child_init_noop;
265266
return (mapcache_cache*)cache;
266267
}

lib/cache_couchbase.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ mapcache_cache* mapcache_cache_couchbase_create(mapcache_context *ctx) {
466466
cache->cache.tile_delete = _mapcache_cache_couchbase_delete;
467467
cache->cache.configuration_parse_xml = _mapcache_cache_couchbase_configuration_parse_xml;
468468
cache->cache.configuration_post_config = _mapcache_cache_couchbase_configuration_post_config;
469+
cache->cache.child_init = mapcache_cache_child_init_noop;
469470
cache->host = NULL;
470471
cache->username = NULL;
471472
cache->password = NULL;

lib/cache_disk.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ mapcache_cache* mapcache_cache_disk_create(mapcache_context *ctx)
768768
cache->cache._tile_set = _mapcache_cache_disk_set;
769769
cache->cache.configuration_post_config = _mapcache_cache_disk_configuration_post_config;
770770
cache->cache.configuration_parse_xml = _mapcache_cache_disk_configuration_parse_xml;
771+
cache->cache.child_init = mapcache_cache_child_init_noop;
771772
return (mapcache_cache*)cache;
772773
}
773774

lib/cache_fallback.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ mapcache_cache* mapcache_cache_fallback_create(mapcache_context *ctx)
190190
cache->cache._tile_multi_set = _mapcache_cache_fallback_tile_multi_set;
191191
cache->cache.configuration_post_config = _mapcache_cache_fallback_configuration_post_config;
192192
cache->cache.configuration_parse_xml = _mapcache_cache_fallback_configuration_parse_xml;
193+
cache->cache.child_init = mapcache_cache_child_init_noop;
193194
return (mapcache_cache*)cache;
194195
}
195196

0 commit comments

Comments
 (0)