From b31f551050c8bb38384c251bb8cadfcc5e8dd945 Mon Sep 17 00:00:00 2001 From: ideal Date: Wed, 28 Oct 2015 13:25:46 +0800 Subject: [PATCH 1/2] close mysql when pool is destroyed --- ngx_http_mysql_module.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/ngx_http_mysql_module.c b/ngx_http_mysql_module.c index 1c5f0ea..aa5ff44 100644 --- a/ngx_http_mysql_module.c +++ b/ngx_http_mysql_module.c @@ -49,6 +49,8 @@ static char* ngx_http_mysql_query(ngx_conf_t *cf, ngx_command_t *cmd, void *conf static char* ngx_http_mysql_subrequest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char* ngx_http_mysql_escape(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +static void ngx_http_mysql_cleanup(void *data); + static ngx_int_t ngx_http_mysql_init(ngx_conf_t *cf); struct ngx_http_mysql_loc_conf_s { @@ -687,9 +689,15 @@ static char* ngx_http_mysql_merge_srv_conf(ngx_conf_t *cf, void *parent, void *c conf->free_node = conf->nodes; - for(n = 0; n < conf->max_conn - 1; ++n) + for (n = 0; n < conf->max_conn - 1; ++n) conf->nodes[n].next = conf->nodes + n + 1; + cp = ngx_pool_cleanup_add(cf->pool, 0); + if (cp == NULL) { + return NGX_CONF_ERROR; + } + cp->handler = ngx_http_mysql_cleanup; + cp->data = conf->nodes; } else if (prev->max_conn != NGX_CONF_UNSET) { conf->max_conn = prev->max_conn; @@ -850,6 +858,18 @@ static char* ngx_http_mysql_escape(ngx_conf_t *cf, ngx_command_t *cmd, void *con return NGX_CONF_OK; } +static void ngx_http_mysql_cleanup(void *data) +{ + ngx_http_mysql_node_t *p, *nodes; + + nodes = (ngx_http_mysql_node_t *)data; + for (p = nodes; p; p = p->next) { + if (p->ready) { + mysql_close(p->mysql); + } + } +} + static ngx_int_t ngx_http_mysql_init(ngx_conf_t *cf) { ngx_http_core_main_conf_t *cmcf; From f97cf60a1b776558f6f33fb7b1b8facf1b1f381d Mon Sep 17 00:00:00 2001 From: ideal Date: Wed, 28 Oct 2015 13:58:21 +0800 Subject: [PATCH 2/2] add definition of cp and fix error in ngx_http_mysql_cleanup() --- ngx_http_mysql_module.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ngx_http_mysql_module.c b/ngx_http_mysql_module.c index aa5ff44..a43ecb8 100644 --- a/ngx_http_mysql_module.c +++ b/ngx_http_mysql_module.c @@ -662,6 +662,7 @@ static char* ngx_http_mysql_merge_srv_conf(ngx_conf_t *cf, void *parent, void *c { ngx_http_mysql_srv_conf_t *prev = parent; ngx_http_mysql_srv_conf_t *conf = child; + ngx_pool_cleanup_t *cp; ngx_int_t n; ngx_log_debug0(NGX_LOG_INFO, cf->log, 0, "mysql merge srv"); @@ -865,7 +866,7 @@ static void ngx_http_mysql_cleanup(void *data) nodes = (ngx_http_mysql_node_t *)data; for (p = nodes; p; p = p->next) { if (p->ready) { - mysql_close(p->mysql); + mysql_close(&p->mysql); } } }