From 4491daef308a52f611bde5b6a9f5fb85b21f8fa3 Mon Sep 17 00:00:00 2001 From: Igor Sysoev Date: Thu, 28 Feb 2008 20:38:34 +0000 Subject: [PATCH] cleanup resolver --- src/core/ngx_resolver.c | 86 ++++++++++++++++++++++++++++++--- src/core/ngx_resolver.h | 2 +- src/http/ngx_http_core_module.c | 4 +- src/mail/ngx_mail_core_module.c | 2 +- 4 files changed, 83 insertions(+), 11 deletions(-) diff --git a/src/core/ngx_resolver.c b/src/core/ngx_resolver.c index d3e6c43c1..eab57eb28 100644 --- a/src/core/ngx_resolver.c +++ b/src/core/ngx_resolver.c @@ -50,6 +50,8 @@ typedef struct { ngx_int_t ngx_udp_connect(ngx_udp_connection_t *uc); +static void ngx_resolver_cleanup(void *data); +static void ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree); static ngx_int_t ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx); static void ngx_resolver_expire(ngx_resolver_t *r, ngx_rbtree_t *tree, @@ -90,17 +92,27 @@ static void *ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size); /* STUB: ngx_peer_addr_t * */ ngx_resolver_t * -ngx_resolver_create(ngx_peer_addr_t *addr, ngx_log_t *log) +ngx_resolver_create(ngx_pool_t *pool, ngx_peer_addr_t *addr) { ngx_resolver_t *r; + ngx_pool_cleanup_t *cln; ngx_udp_connection_t *uc; - r = ngx_calloc(sizeof(ngx_resolver_t), log); + cln = ngx_pool_cleanup_add(pool, 0); + if (cln == NULL) { + return NULL; + } + + cln->handler = ngx_resolver_cleanup; + + r = ngx_calloc(sizeof(ngx_resolver_t), pool->log); if (r == NULL) { return NULL; } - r->event = ngx_calloc(sizeof(ngx_event_t), log); + cln->data = r; + + r->event = ngx_calloc(sizeof(ngx_event_t), pool->log); if (r->event == NULL) { return NULL; } @@ -119,18 +131,18 @@ ngx_resolver_create(ngx_peer_addr_t *addr, ngx_log_t *log) r->event->handler = ngx_resolver_resend_handler; r->event->data = r; - r->event->log = log; + r->event->log = pool->log; r->ident = -1; r->resend_timeout = 5; r->expire = 30; r->valid = 300; - r->log = log; + r->log = pool->log; r->log_level = NGX_LOG_ALERT; if (addr) { - uc = ngx_calloc(sizeof(ngx_udp_connection_t), log); + uc = ngx_calloc(sizeof(ngx_udp_connection_t), pool->log); if (uc == NULL) { return NULL; } @@ -140,13 +152,72 @@ ngx_resolver_create(ngx_peer_addr_t *addr, ngx_log_t *log) uc->sockaddr = addr->sockaddr; uc->socklen = addr->socklen; uc->server = addr->name; - uc->log = log; + uc->log = pool->log; } return r; } +static void +ngx_resolver_cleanup(void *data) +{ + ngx_resolver_t *r = data; + + if (r) { + ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, + "cleanup resolver"); + + ngx_resolver_cleanup_tree(r, &r->name_rbtree); + + ngx_resolver_cleanup_tree(r, &r->addr_rbtree); + + if (r->event) { + ngx_free(r->event); + } + + if (r->udp_connection) { + if (r->udp_connection->connection) { + ngx_close_connection(r->udp_connection->connection); + } + + ngx_free(r->udp_connection); + } + + ngx_free(r); + } +} + + +static void +ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree) +{ + ngx_resolver_ctx_t *ctx, *next; + ngx_resolver_node_t *rn; + + while (tree->root != tree->sentinel) { + + rn = (ngx_resolver_node_t *) ngx_rbtree_min(tree->root, tree->sentinel); + + ngx_queue_remove(&rn->queue); + + for (ctx = rn->waiting; ctx; ctx = next) { + next = ctx->next; + + if (ctx->event) { + ngx_resolver_free(r, ctx->event); + } + + ngx_resolver_free(r, ctx); + } + + ngx_rbtree_delete(tree, &rn->node); + + ngx_resolver_free_node(r, rn); + } +} + + ngx_resolver_ctx_t * ngx_resolve_start(ngx_resolver_t *r, ngx_resolver_ctx_t *temp) { @@ -707,6 +778,7 @@ ngx_resolver_send_query(ngx_resolver_t *r, ngx_resolver_node_t *rn) uc->connection->data = r; uc->connection->read->handler = ngx_resolver_read_response; + uc->connection->read->resolver = 1; } n = ngx_send(uc->connection, rn->query, rn->qlen); diff --git a/src/core/ngx_resolver.h b/src/core/ngx_resolver.h index e513c396f..6cf8a7f9f 100644 --- a/src/core/ngx_resolver.h +++ b/src/core/ngx_resolver.h @@ -131,7 +131,7 @@ struct ngx_resolver_ctx_s { }; -ngx_resolver_t *ngx_resolver_create(ngx_peer_addr_t *addr, ngx_log_t *log); +ngx_resolver_t *ngx_resolver_create(ngx_pool_t *pool, ngx_peer_addr_t *addr); ngx_resolver_ctx_t *ngx_resolve_start(ngx_resolver_t *r, ngx_resolver_ctx_t *temp); ngx_int_t ngx_resolve_name(ngx_resolver_ctx_t *ctx); diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c index 8a9a4769d..da273773b 100644 --- a/src/http/ngx_http_core_module.c +++ b/src/http/ngx_http_core_module.c @@ -2910,7 +2910,7 @@ ngx_http_core_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) conf->resolver = prev->resolver; if (conf->resolver == NULL) { - conf->resolver = ngx_resolver_create(NULL, cf->cycle->new_log); + conf->resolver = ngx_resolver_create(cf->pool, NULL); if (conf->resolver == NULL) { return NGX_CONF_ERROR; } @@ -3728,7 +3728,7 @@ ngx_http_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } - clcf->resolver = ngx_resolver_create(&u.addrs[0], cf->cycle->new_log); + clcf->resolver = ngx_resolver_create(cf->pool, &u.addrs[0]); if (clcf->resolver == NULL) { return NGX_OK; } diff --git a/src/mail/ngx_mail_core_module.c b/src/mail/ngx_mail_core_module.c index 91b425f7f..3af9fbf99 100644 --- a/src/mail/ngx_mail_core_module.c +++ b/src/mail/ngx_mail_core_module.c @@ -437,7 +437,7 @@ ngx_mail_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } - cscf->resolver = ngx_resolver_create(&u.addrs[0], cf->cycle->new_log); + cscf->resolver = ngx_resolver_create(cf->pool, &u.addrs[0]); if (cscf->resolver == NULL) { return NGX_CONF_OK; }