Klaus Demo nginx / 6207181
Support of several servers in the "resolver" directive. Patch by Kirill A. Korinskiy. Igor Sysoev 10 years ago
4 changed file(s) with 60 addition(s) and 53 deletion(s). Raw diff Collapse all Expand all
9090
9191
9292 ngx_resolver_t *
93 ngx_resolver_create(ngx_conf_t *cf, ngx_addr_t *addr)
94 {
93 ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n)
94 {
95 ngx_url_t u;
96 ngx_uint_t i;
9597 ngx_resolver_t *r;
9698 ngx_pool_cleanup_t *cln;
9799 ngx_udp_connection_t *uc;
106108 r = ngx_calloc(sizeof(ngx_resolver_t), cf->log);
107109 if (r == NULL) {
108110 return NULL;
111 }
112
113 if (n) {
114 if (ngx_array_init(&r->udp_connections, cf->pool, n,
115 sizeof(ngx_udp_connection_t))
116 != NGX_OK)
117 {
118 return NULL;
119 }
109120 }
110121
111122 cln->data = r;
139150 r->log = &cf->cycle->new_log;
140151 r->log_level = NGX_LOG_ERR;
141152
142 if (addr) {
143 uc = ngx_calloc(sizeof(ngx_udp_connection_t), cf->log);
153 for (i = 0; i < n; i++) {
154 ngx_memzero(&u, sizeof(ngx_url_t));
155
156 u.host = names[i];
157 u.port = 53;
158
159 if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) {
160 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V: %s", &u.host, u.err);
161 return NULL;
162 }
163
164 uc = ngx_array_push(&r->udp_connections);
144165 if (uc == NULL) {
145166 return NULL;
146167 }
147168
148 r->udp_connection = uc;
149
150 uc->sockaddr = addr->sockaddr;
151 uc->socklen = addr->socklen;
152 uc->server = addr->name;
169 ngx_memzero(uc, sizeof(ngx_udp_connection_t));
170
171 uc->sockaddr = u.addrs->sockaddr;
172 uc->socklen = u.addrs->socklen;
173 uc->server = u.addrs->name;
153174
154175 uc->log = cf->cycle->new_log;
155176 uc->log.handler = ngx_resolver_log_error;
166187 {
167188 ngx_resolver_t *r = data;
168189
190 ngx_uint_t i;
191 ngx_udp_connection_t *uc;
192
169193 if (r) {
170194 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
171195 "cleanup resolver");
178202 ngx_free(r->event);
179203 }
180204
181 if (r->udp_connection) {
182 if (r->udp_connection->connection) {
183 ngx_close_connection(r->udp_connection->connection);
205
206 uc = r->udp_connections.elts;
207
208 for (i = 0; i < r->udp_connections.nelts; i++) {
209 if (uc[i].connection) {
210 ngx_close_connection(uc[i].connection);
184211 }
185
186 ngx_free(r->udp_connection);
187212 }
188213
189214 ngx_free(r);
241266 }
242267 }
243268
244 if (r->udp_connection == NULL) {
269 if (r->udp_connections.nelts == 0) {
245270 return NGX_NO_RESOLVER;
246271 }
247272
825850 ssize_t n;
826851 ngx_udp_connection_t *uc;
827852
828 uc = r->udp_connection;
853 uc = r->udp_connections.elts;
854
855 uc = &uc[r->last_connection++];
856 if (r->last_connection == r->udp_connections.nelts) {
857 r->last_connection = 0;
858 }
829859
830860 if (uc->connection == NULL) {
831861 if (ngx_udp_connect(uc) != NGX_OK) {
7676 typedef struct {
7777 /* has to be pointer because of "incomplete type" */
7878 ngx_event_t *event;
79
80 /* TODO: DNS peers balancer */
81 /* STUB */
82 ngx_udp_connection_t *udp_connection;
83
79 void *dummy;
8480 ngx_log_t *log;
8581
8682 /* ident must be after 3 pointers */
8783 ngx_int_t ident;
84
85 /* simple round robin DNS peers balancer */
86 ngx_array_t udp_connections;
87 ngx_uint_t last_connection;
8888
8989 ngx_rbtree_t name_rbtree;
9090 ngx_rbtree_node_t name_sentinel;
122122 in_addr_t *addrs;
123123 in_addr_t addr;
124124
125 /* TODO: DNS peers balancer ctx */
126
127125 ngx_resolver_handler_pt handler;
128126 void *data;
129127 ngx_msec_t timeout;
134132 };
135133
136134
137 ngx_resolver_t *ngx_resolver_create(ngx_conf_t *cf, ngx_addr_t *addr);
135 ngx_resolver_t *ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names,
136 ngx_uint_t n);
138137 ngx_resolver_ctx_t *ngx_resolve_start(ngx_resolver_t *r,
139138 ngx_resolver_ctx_t *temp);
140139 ngx_int_t ngx_resolve_name(ngx_resolver_ctx_t *ctx);
717717 NULL },
718718
719719 { ngx_string("resolver"),
720 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
720 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
721721 ngx_http_core_resolver,
722722 NGX_HTTP_LOC_CONF_OFFSET,
723723 0,
35343534 * to inherit it in all servers
35353535 */
35363536
3537 prev->resolver = ngx_resolver_create(cf, NULL);
3537 prev->resolver = ngx_resolver_create(cf, NULL, 0);
35383538 if (prev->resolver == NULL) {
35393539 return NGX_CONF_ERROR;
35403540 }
45394539 {
45404540 ngx_http_core_loc_conf_t *clcf = conf;
45414541
4542 ngx_url_t u;
45434542 ngx_str_t *value;
45444543
45454544 if (clcf->resolver) {
45484547
45494548 value = cf->args->elts;
45504549
4551 ngx_memzero(&u, sizeof(ngx_url_t));
4552
4553 u.host = value[1];
4554 u.port = 53;
4555
4556 if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) {
4557 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V: %s", &u.host, u.err);
4550 clcf->resolver = ngx_resolver_create(cf, &value[1], cf->args->nelts - 1);
4551 if (clcf->resolver == NULL) {
45584552 return NGX_CONF_ERROR;
4559 }
4560
4561 clcf->resolver = ngx_resolver_create(cf, &u.addrs[0]);
4562 if (clcf->resolver == NULL) {
4563 return NGX_OK;
45644553 }
45654554
45664555 return NGX_CONF_OK;
6868 NULL },
6969
7070 { ngx_string("resolver"),
71 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
71 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
7272 ngx_mail_core_resolver,
7373 NGX_MAIL_SRV_CONF_OFFSET,
7474 0,
492492 {
493493 ngx_mail_core_srv_conf_t *cscf = conf;
494494
495 ngx_url_t u;
496495 ngx_str_t *value;
497496
498497 value = cf->args->elts;
506505 return NGX_CONF_OK;
507506 }
508507
509 ngx_memzero(&u, sizeof(ngx_url_t));
510
511 u.host = value[1];
512 u.port = 53;
513
514 if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) {
515 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V: %s", &u.host, u.err);
516 return NGX_CONF_ERROR;
517 }
518
519 cscf->resolver = ngx_resolver_create(cf, &u.addrs[0]);
508 cscf->resolver = ngx_resolver_create(cf, &value[1], cf->args->nelts - 1);
520509 if (cscf->resolver == NULL) {
521 return NGX_CONF_OK;
510 return NGX_CONF_ERROR;
522511 }
523512
524513 return NGX_CONF_OK;