Klaus Demo nginx / 4491dae
cleanup resolver Igor Sysoev 14 years ago
4 changed file(s) with 83 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
4949 ngx_int_t ngx_udp_connect(ngx_udp_connection_t *uc);
5050
5151
52 static void ngx_resolver_cleanup(void *data);
53 static void ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree);
5254 static ngx_int_t ngx_resolve_name_locked(ngx_resolver_t *r,
5355 ngx_resolver_ctx_t *ctx);
5456 static void ngx_resolver_expire(ngx_resolver_t *r, ngx_rbtree_t *tree,
8991 /* STUB: ngx_peer_addr_t * */
9092
9193 ngx_resolver_t *
92 ngx_resolver_create(ngx_peer_addr_t *addr, ngx_log_t *log)
94 ngx_resolver_create(ngx_pool_t *pool, ngx_peer_addr_t *addr)
9395 {
9496 ngx_resolver_t *r;
97 ngx_pool_cleanup_t *cln;
9598 ngx_udp_connection_t *uc;
9699
97 r = ngx_calloc(sizeof(ngx_resolver_t), log);
100 cln = ngx_pool_cleanup_add(pool, 0);
101 if (cln == NULL) {
102 return NULL;
103 }
104
105 cln->handler = ngx_resolver_cleanup;
106
107 r = ngx_calloc(sizeof(ngx_resolver_t), pool->log);
98108 if (r == NULL) {
99109 return NULL;
100110 }
101111
102 r->event = ngx_calloc(sizeof(ngx_event_t), log);
112 cln->data = r;
113
114 r->event = ngx_calloc(sizeof(ngx_event_t), pool->log);
103115 if (r->event == NULL) {
104116 return NULL;
105117 }
118130
119131 r->event->handler = ngx_resolver_resend_handler;
120132 r->event->data = r;
121 r->event->log = log;
133 r->event->log = pool->log;
122134 r->ident = -1;
123135
124136 r->resend_timeout = 5;
125137 r->expire = 30;
126138 r->valid = 300;
127139
128 r->log = log;
140 r->log = pool->log;
129141 r->log_level = NGX_LOG_ALERT;
130142
131143 if (addr) {
132 uc = ngx_calloc(sizeof(ngx_udp_connection_t), log);
144 uc = ngx_calloc(sizeof(ngx_udp_connection_t), pool->log);
133145 if (uc == NULL) {
134146 return NULL;
135147 }
139151 uc->sockaddr = addr->sockaddr;
140152 uc->socklen = addr->socklen;
141153 uc->server = addr->name;
142 uc->log = log;
154 uc->log = pool->log;
143155 }
144156
145157 return r;
158 }
159
160
161 static void
162 ngx_resolver_cleanup(void *data)
163 {
164 ngx_resolver_t *r = data;
165
166 if (r) {
167 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
168 "cleanup resolver");
169
170 ngx_resolver_cleanup_tree(r, &r->name_rbtree);
171
172 ngx_resolver_cleanup_tree(r, &r->addr_rbtree);
173
174 if (r->event) {
175 ngx_free(r->event);
176 }
177
178 if (r->udp_connection) {
179 if (r->udp_connection->connection) {
180 ngx_close_connection(r->udp_connection->connection);
181 }
182
183 ngx_free(r->udp_connection);
184 }
185
186 ngx_free(r);
187 }
188 }
189
190
191 static void
192 ngx_resolver_cleanup_tree(ngx_resolver_t *r, ngx_rbtree_t *tree)
193 {
194 ngx_resolver_ctx_t *ctx, *next;
195 ngx_resolver_node_t *rn;
196
197 while (tree->root != tree->sentinel) {
198
199 rn = (ngx_resolver_node_t *) ngx_rbtree_min(tree->root, tree->sentinel);
200
201 ngx_queue_remove(&rn->queue);
202
203 for (ctx = rn->waiting; ctx; ctx = next) {
204 next = ctx->next;
205
206 if (ctx->event) {
207 ngx_resolver_free(r, ctx->event);
208 }
209
210 ngx_resolver_free(r, ctx);
211 }
212
213 ngx_rbtree_delete(tree, &rn->node);
214
215 ngx_resolver_free_node(r, rn);
216 }
146217 }
147218
148219
706777
707778 uc->connection->data = r;
708779 uc->connection->read->handler = ngx_resolver_read_response;
780 uc->connection->read->resolver = 1;
709781 }
710782
711783 n = ngx_send(uc->connection, rn->query, rn->qlen);
130130 };
131131
132132
133 ngx_resolver_t *ngx_resolver_create(ngx_peer_addr_t *addr, ngx_log_t *log);
133 ngx_resolver_t *ngx_resolver_create(ngx_pool_t *pool, ngx_peer_addr_t *addr);
134134 ngx_resolver_ctx_t *ngx_resolve_start(ngx_resolver_t *r,
135135 ngx_resolver_ctx_t *temp);
136136 ngx_int_t ngx_resolve_name(ngx_resolver_ctx_t *ctx);
29092909 conf->resolver = prev->resolver;
29102910
29112911 if (conf->resolver == NULL) {
2912 conf->resolver = ngx_resolver_create(NULL, cf->cycle->new_log);
2912 conf->resolver = ngx_resolver_create(cf->pool, NULL);
29132913 if (conf->resolver == NULL) {
29142914 return NGX_CONF_ERROR;
29152915 }
37273727 return NGX_CONF_ERROR;
37283728 }
37293729
3730 clcf->resolver = ngx_resolver_create(&u.addrs[0], cf->cycle->new_log);
3730 clcf->resolver = ngx_resolver_create(cf->pool, &u.addrs[0]);
37313731 if (clcf->resolver == NULL) {
37323732 return NGX_OK;
37333733 }
436436 return NGX_CONF_ERROR;
437437 }
438438
439 cscf->resolver = ngx_resolver_create(&u.addrs[0], cf->cycle->new_log);
439 cscf->resolver = ngx_resolver_create(cf->pool, &u.addrs[0]);
440440 if (cscf->resolver == NULL) {
441441 return NGX_CONF_OK;
442442 }