Mercurial > hg > nginx
comparison src/core/ngx_resolver.c @ 4225:016352c19049
Support of several servers in the "resolver" directive.
Patch by Kirill A. Korinskiy.
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 24 Oct 2011 16:09:05 +0000 |
parents | 159b58f9c0bd |
children | 768212ca0745 |
comparison
equal
deleted
inserted
replaced
4224:4c4997de65b8 | 4225:016352c19049 |
---|---|
89 static void *ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size); | 89 static void *ngx_resolver_dup(ngx_resolver_t *r, void *src, size_t size); |
90 static u_char *ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len); | 90 static u_char *ngx_resolver_log_error(ngx_log_t *log, u_char *buf, size_t len); |
91 | 91 |
92 | 92 |
93 ngx_resolver_t * | 93 ngx_resolver_t * |
94 ngx_resolver_create(ngx_conf_t *cf, ngx_addr_t *addr) | 94 ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n) |
95 { | 95 { |
96 ngx_url_t u; | |
97 ngx_uint_t i; | |
96 ngx_resolver_t *r; | 98 ngx_resolver_t *r; |
97 ngx_pool_cleanup_t *cln; | 99 ngx_pool_cleanup_t *cln; |
98 ngx_udp_connection_t *uc; | 100 ngx_udp_connection_t *uc; |
99 | 101 |
100 cln = ngx_pool_cleanup_add(cf->pool, 0); | 102 cln = ngx_pool_cleanup_add(cf->pool, 0); |
105 cln->handler = ngx_resolver_cleanup; | 107 cln->handler = ngx_resolver_cleanup; |
106 | 108 |
107 r = ngx_calloc(sizeof(ngx_resolver_t), cf->log); | 109 r = ngx_calloc(sizeof(ngx_resolver_t), cf->log); |
108 if (r == NULL) { | 110 if (r == NULL) { |
109 return NULL; | 111 return NULL; |
112 } | |
113 | |
114 if (n) { | |
115 if (ngx_array_init(&r->udp_connections, cf->pool, n, | |
116 sizeof(ngx_udp_connection_t)) | |
117 != NGX_OK) | |
118 { | |
119 return NULL; | |
120 } | |
110 } | 121 } |
111 | 122 |
112 cln->data = r; | 123 cln->data = r; |
113 | 124 |
114 r->event = ngx_calloc(sizeof(ngx_event_t), cf->log); | 125 r->event = ngx_calloc(sizeof(ngx_event_t), cf->log); |
138 r->valid = 300; | 149 r->valid = 300; |
139 | 150 |
140 r->log = &cf->cycle->new_log; | 151 r->log = &cf->cycle->new_log; |
141 r->log_level = NGX_LOG_ERR; | 152 r->log_level = NGX_LOG_ERR; |
142 | 153 |
143 if (addr) { | 154 for (i = 0; i < n; i++) { |
144 uc = ngx_calloc(sizeof(ngx_udp_connection_t), cf->log); | 155 ngx_memzero(&u, sizeof(ngx_url_t)); |
156 | |
157 u.host = names[i]; | |
158 u.port = 53; | |
159 | |
160 if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) { | |
161 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V: %s", &u.host, u.err); | |
162 return NULL; | |
163 } | |
164 | |
165 uc = ngx_array_push(&r->udp_connections); | |
145 if (uc == NULL) { | 166 if (uc == NULL) { |
146 return NULL; | 167 return NULL; |
147 } | 168 } |
148 | 169 |
149 r->udp_connection = uc; | 170 ngx_memzero(uc, sizeof(ngx_udp_connection_t)); |
150 | 171 |
151 uc->sockaddr = addr->sockaddr; | 172 uc->sockaddr = u.addrs->sockaddr; |
152 uc->socklen = addr->socklen; | 173 uc->socklen = u.addrs->socklen; |
153 uc->server = addr->name; | 174 uc->server = u.addrs->name; |
154 | 175 |
155 uc->log = cf->cycle->new_log; | 176 uc->log = cf->cycle->new_log; |
156 uc->log.handler = ngx_resolver_log_error; | 177 uc->log.handler = ngx_resolver_log_error; |
157 uc->log.data = uc; | 178 uc->log.data = uc; |
158 uc->log.action = "resolving"; | 179 uc->log.action = "resolving"; |
165 static void | 186 static void |
166 ngx_resolver_cleanup(void *data) | 187 ngx_resolver_cleanup(void *data) |
167 { | 188 { |
168 ngx_resolver_t *r = data; | 189 ngx_resolver_t *r = data; |
169 | 190 |
191 ngx_uint_t i; | |
192 ngx_udp_connection_t *uc; | |
193 | |
170 if (r) { | 194 if (r) { |
171 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, | 195 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, |
172 "cleanup resolver"); | 196 "cleanup resolver"); |
173 | 197 |
174 ngx_resolver_cleanup_tree(r, &r->name_rbtree); | 198 ngx_resolver_cleanup_tree(r, &r->name_rbtree); |
177 | 201 |
178 if (r->event) { | 202 if (r->event) { |
179 ngx_free(r->event); | 203 ngx_free(r->event); |
180 } | 204 } |
181 | 205 |
182 if (r->udp_connection) { | 206 |
183 if (r->udp_connection->connection) { | 207 uc = r->udp_connections.elts; |
184 ngx_close_connection(r->udp_connection->connection); | 208 |
209 for (i = 0; i < r->udp_connections.nelts; i++) { | |
210 if (uc[i].connection) { | |
211 ngx_close_connection(uc[i].connection); | |
185 } | 212 } |
186 | |
187 ngx_free(r->udp_connection); | |
188 } | 213 } |
189 | 214 |
190 ngx_free(r); | 215 ngx_free(r); |
191 } | 216 } |
192 } | 217 } |
240 | 265 |
241 return temp; | 266 return temp; |
242 } | 267 } |
243 } | 268 } |
244 | 269 |
245 if (r->udp_connection == NULL) { | 270 if (r->udp_connections.nelts == 0) { |
246 return NGX_NO_RESOLVER; | 271 return NGX_NO_RESOLVER; |
247 } | 272 } |
248 | 273 |
249 ctx = ngx_resolver_calloc(r, sizeof(ngx_resolver_ctx_t)); | 274 ctx = ngx_resolver_calloc(r, sizeof(ngx_resolver_ctx_t)); |
250 | 275 |
824 ngx_resolver_send_query(ngx_resolver_t *r, ngx_resolver_node_t *rn) | 849 ngx_resolver_send_query(ngx_resolver_t *r, ngx_resolver_node_t *rn) |
825 { | 850 { |
826 ssize_t n; | 851 ssize_t n; |
827 ngx_udp_connection_t *uc; | 852 ngx_udp_connection_t *uc; |
828 | 853 |
829 uc = r->udp_connection; | 854 uc = r->udp_connections.elts; |
855 | |
856 uc = &uc[r->last_connection++]; | |
857 if (r->last_connection == r->udp_connections.nelts) { | |
858 r->last_connection = 0; | |
859 } | |
830 | 860 |
831 if (uc->connection == NULL) { | 861 if (uc->connection == NULL) { |
832 if (ngx_udp_connect(uc) != NGX_OK) { | 862 if (ngx_udp_connect(uc) != NGX_OK) { |
833 return NGX_ERROR; | 863 return NGX_ERROR; |
834 } | 864 } |