Craig Tiller | cd29c58 | 2015-06-24 09:15:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | cd29c58 | 2015-06-24 09:15:15 -0700 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 34 | #include <string.h> |
| 35 | |
| 36 | #include <grpc/support/alloc.h> |
Craig Tiller | 6b9f5c6 | 2015-07-29 18:47:35 -0700 | [diff] [blame] | 37 | #include <grpc/support/host_port.h> |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 38 | #include <grpc/support/string_util.h> |
| 39 | |
Mark D. Roth | 2137cd8 | 2016-09-14 09:04:00 -0700 | [diff] [blame] | 40 | #include "src/core/ext/client_channel/lb_policy_registry.h" |
| 41 | #include "src/core/ext/client_channel/resolver_registry.h" |
Mark D. Roth | 98abfd3 | 2016-10-21 08:10:51 -0700 | [diff] [blame] | 42 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 43 | #include "src/core/lib/iomgr/combiner.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 44 | #include "src/core/lib/iomgr/resolve_address.h" |
| 45 | #include "src/core/lib/iomgr/timer.h" |
| 46 | #include "src/core/lib/support/backoff.h" |
| 47 | #include "src/core/lib/support/string.h" |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 48 | |
David Garcia Quintas | 1edfb95 | 2016-11-22 17:15:34 -0800 | [diff] [blame] | 49 | #define GRPC_DNS_MIN_CONNECT_TIMEOUT_SECONDS 1 |
| 50 | #define GRPC_DNS_INITIAL_CONNECT_BACKOFF_SECONDS 1 |
| 51 | #define GRPC_DNS_RECONNECT_BACKOFF_MULTIPLIER 1.6 |
| 52 | #define GRPC_DNS_RECONNECT_MAX_BACKOFF_SECONDS 120 |
| 53 | #define GRPC_DNS_RECONNECT_JITTER 0.2 |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 54 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 55 | typedef struct { |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 56 | /** base class: must be first */ |
| 57 | grpc_resolver base; |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 58 | /** name to resolve */ |
Mark D. Roth | 28ea7e2 | 2016-07-25 11:06:22 -0700 | [diff] [blame] | 59 | char *name_to_resolve; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 60 | /** default port to use */ |
| 61 | char *default_port; |
Mark D. Roth | 98abfd3 | 2016-10-21 08:10:51 -0700 | [diff] [blame] | 62 | /** channel args. */ |
| 63 | grpc_channel_args *channel_args; |
Yuchen Zeng | 63e3e3b | 2016-12-15 12:06:33 -0800 | [diff] [blame] | 64 | /** pollset_set to drive the name resolution process */ |
| 65 | grpc_pollset_set *interested_parties; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 66 | |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 67 | /** are we currently resolving? */ |
Mark D. Roth | e273b03 | 2016-07-22 13:24:21 -0700 | [diff] [blame] | 68 | bool resolving; |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 69 | /** which version of the result have we published? */ |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 70 | int published_version; |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 71 | /** which version of the result is current? */ |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 72 | int resolved_version; |
| 73 | /** pending next completion, or NULL */ |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 74 | grpc_closure *next_completion; |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 75 | /** target result address for next completion */ |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 76 | grpc_channel_args **target_result; |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 77 | /** current (fully resolved) result */ |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 78 | grpc_channel_args *resolved_result; |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 79 | /** retry timer */ |
| 80 | bool have_retry_timer; |
| 81 | grpc_timer retry_timer; |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 82 | grpc_closure on_retry; |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 83 | /** retry backoff state */ |
| 84 | gpr_backoff backoff_state; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 85 | |
| 86 | /** currently resolving addresses */ |
| 87 | grpc_resolved_addresses *addresses; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 88 | } dns_resolver; |
| 89 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 90 | static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 91 | |
Craig Tiller | 24d687e | 2016-04-13 19:47:27 -0700 | [diff] [blame] | 92 | static void dns_start_resolving_locked(grpc_exec_ctx *exec_ctx, |
| 93 | dns_resolver *r); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 94 | static void dns_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx, |
| 95 | dns_resolver *r); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 96 | |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 97 | static void dns_shutdown_locked(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
| 98 | static void dns_channel_saw_error_locked(grpc_exec_ctx *exec_ctx, |
| 99 | grpc_resolver *r); |
| 100 | static void dns_next_locked(grpc_exec_ctx *exec_ctx, grpc_resolver *r, |
| 101 | grpc_channel_args **target_result, |
| 102 | grpc_closure *on_complete); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 103 | |
| 104 | static const grpc_resolver_vtable dns_resolver_vtable = { |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 105 | dns_destroy, dns_shutdown_locked, dns_channel_saw_error_locked, |
| 106 | dns_next_locked}; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 107 | |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 108 | static void dns_shutdown_locked(grpc_exec_ctx *exec_ctx, |
| 109 | grpc_resolver *resolver) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 110 | dns_resolver *r = (dns_resolver *)resolver; |
Craig Tiller | e2327db | 2016-03-11 09:52:42 -0800 | [diff] [blame] | 111 | if (r->have_retry_timer) { |
| 112 | grpc_timer_cancel(exec_ctx, &r->retry_timer); |
| 113 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 114 | if (r->next_completion != NULL) { |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 115 | *r->target_result = NULL; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 116 | grpc_closure_sched(exec_ctx, r->next_completion, |
| 117 | GRPC_ERROR_CREATE("Resolver Shutdown")); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 118 | r->next_completion = NULL; |
| 119 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 122 | static void dns_channel_saw_error_locked(grpc_exec_ctx *exec_ctx, |
| 123 | grpc_resolver *resolver) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 124 | dns_resolver *r = (dns_resolver *)resolver; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 125 | if (!r->resolving) { |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 126 | gpr_backoff_reset(&r->backoff_state); |
Craig Tiller | 24d687e | 2016-04-13 19:47:27 -0700 | [diff] [blame] | 127 | dns_start_resolving_locked(exec_ctx, r); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 128 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 131 | static void dns_next_locked(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver, |
| 132 | grpc_channel_args **target_result, |
| 133 | grpc_closure *on_complete) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 134 | dns_resolver *r = (dns_resolver *)resolver; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 135 | GPR_ASSERT(!r->next_completion); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 136 | r->next_completion = on_complete; |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 137 | r->target_result = target_result; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 138 | if (r->resolved_version == 0 && !r->resolving) { |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 139 | gpr_backoff_reset(&r->backoff_state); |
Craig Tiller | 24d687e | 2016-04-13 19:47:27 -0700 | [diff] [blame] | 140 | dns_start_resolving_locked(exec_ctx, r); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 141 | } else { |
| 142 | dns_maybe_finish_next_locked(exec_ctx, r); |
| 143 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 146 | static void dns_on_retry_timer_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 147 | grpc_error *error) { |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 148 | dns_resolver *r = arg; |
| 149 | |
Craig Tiller | e2327db | 2016-03-11 09:52:42 -0800 | [diff] [blame] | 150 | r->have_retry_timer = false; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 151 | if (error == GRPC_ERROR_NONE) { |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 152 | if (!r->resolving) { |
Craig Tiller | 24d687e | 2016-04-13 19:47:27 -0700 | [diff] [blame] | 153 | dns_start_resolving_locked(exec_ctx, r); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 154 | } |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "retry-timer"); |
| 158 | } |
| 159 | |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 160 | static void dns_on_resolved_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 161 | grpc_error *error) { |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 162 | dns_resolver *r = arg; |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 163 | grpc_channel_args *result = NULL; |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 164 | GPR_ASSERT(r->resolving); |
Mark D. Roth | e273b03 | 2016-07-22 13:24:21 -0700 | [diff] [blame] | 165 | r->resolving = false; |
Mark D. Roth | e011b1e | 2016-09-07 08:28:00 -0700 | [diff] [blame] | 166 | if (r->addresses != NULL) { |
Mark D. Roth | 16883a3 | 2016-10-21 10:30:58 -0700 | [diff] [blame] | 167 | grpc_lb_addresses *addresses = grpc_lb_addresses_create( |
| 168 | r->addresses->naddrs, NULL /* user_data_vtable */); |
Mark D. Roth | e011b1e | 2016-09-07 08:28:00 -0700 | [diff] [blame] | 169 | for (size_t i = 0; i < r->addresses->naddrs; ++i) { |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 170 | grpc_lb_addresses_set_address( |
Mark D. Roth | ee5173f | 2016-09-16 10:14:18 -0700 | [diff] [blame] | 171 | addresses, i, &r->addresses->addrs[i].addr, |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 172 | r->addresses->addrs[i].len, false /* is_balancer */, |
| 173 | NULL /* balancer_name */, NULL /* user_data */); |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 174 | } |
Mark D. Roth | 3686996 | 2016-10-21 10:43:45 -0700 | [diff] [blame] | 175 | grpc_arg new_arg = grpc_lb_addresses_create_channel_arg(addresses); |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 176 | result = grpc_channel_args_copy_and_add(r->channel_args, &new_arg, 1); |
Mark D. Roth | e011b1e | 2016-09-07 08:28:00 -0700 | [diff] [blame] | 177 | grpc_resolved_addresses_destroy(r->addresses); |
Craig Tiller | 4cc1c35 | 2016-12-27 08:48:01 -0800 | [diff] [blame] | 178 | grpc_lb_addresses_destroy(exec_ctx, addresses); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 179 | } else { |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 180 | gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC); |
| 181 | gpr_timespec next_try = gpr_backoff_step(&r->backoff_state, now); |
| 182 | gpr_timespec timeout = gpr_time_sub(next_try, now); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 183 | gpr_log(GPR_INFO, "dns resolution failed (will retry): %s", |
| 184 | grpc_error_string(error)); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 185 | GPR_ASSERT(!r->have_retry_timer); |
| 186 | r->have_retry_timer = true; |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 187 | GRPC_RESOLVER_REF(&r->base, "retry-timer"); |
David Garcia Quintas | 8c2e8b4 | 2016-10-30 15:13:04 +0100 | [diff] [blame] | 188 | if (gpr_time_cmp(timeout, gpr_time_0(timeout.clock_type)) > 0) { |
Craig Tiller | 13c0940 | 2016-06-15 09:41:33 -0700 | [diff] [blame] | 189 | gpr_log(GPR_DEBUG, "retrying in %" PRId64 ".%09d seconds", timeout.tv_sec, |
Craig Tiller | df30bc5 | 2016-06-01 14:08:50 -0700 | [diff] [blame] | 190 | timeout.tv_nsec); |
Craig Tiller | 8cdc4e5 | 2016-06-01 14:03:28 -0700 | [diff] [blame] | 191 | } else { |
| 192 | gpr_log(GPR_DEBUG, "retrying immediately"); |
| 193 | } |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 194 | grpc_closure_init(&r->on_retry, dns_on_retry_timer_locked, r, |
Craig Tiller | 0bfad14 | 2017-02-17 16:01:08 -0800 | [diff] [blame] | 195 | grpc_combiner_scheduler(r->base.combiner, false)); |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 196 | grpc_timer_init(exec_ctx, &r->retry_timer, next_try, &r->on_retry, now); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 197 | } |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 198 | if (r->resolved_result != NULL) { |
Craig Tiller | 4cc1c35 | 2016-12-27 08:48:01 -0800 | [diff] [blame] | 199 | grpc_channel_args_destroy(exec_ctx, r->resolved_result); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 200 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 201 | r->resolved_result = result; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 202 | r->resolved_version++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 203 | dns_maybe_finish_next_locked(exec_ctx, r); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 204 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 205 | GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "dns-resolving"); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Craig Tiller | 24d687e | 2016-04-13 19:47:27 -0700 | [diff] [blame] | 208 | static void dns_start_resolving_locked(grpc_exec_ctx *exec_ctx, |
| 209 | dns_resolver *r) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 210 | GRPC_RESOLVER_REF(&r->base, "dns-resolving"); |
| 211 | GPR_ASSERT(!r->resolving); |
Mark D. Roth | e273b03 | 2016-07-22 13:24:21 -0700 | [diff] [blame] | 212 | r->resolving = true; |
Craig Tiller | d039722 | 2016-05-12 16:53:27 -0700 | [diff] [blame] | 213 | r->addresses = NULL; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 214 | grpc_resolve_address( |
| 215 | exec_ctx, r->name_to_resolve, r->default_port, r->interested_parties, |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 216 | grpc_closure_create(dns_on_resolved_locked, r, |
Craig Tiller | 0bfad14 | 2017-02-17 16:01:08 -0800 | [diff] [blame] | 217 | grpc_combiner_scheduler(r->base.combiner, false)), |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 218 | &r->addresses); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 221 | static void dns_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx, |
| 222 | dns_resolver *r) { |
| 223 | if (r->next_completion != NULL && |
| 224 | r->resolved_version != r->published_version) { |
Mark D. Roth | b367c1b | 2016-10-24 12:59:07 -0700 | [diff] [blame] | 225 | *r->target_result = r->resolved_result == NULL |
| 226 | ? NULL |
| 227 | : grpc_channel_args_copy(r->resolved_result); |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 228 | grpc_closure_sched(exec_ctx, r->next_completion, GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 229 | r->next_completion = NULL; |
| 230 | r->published_version = r->resolved_version; |
| 231 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 234 | static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) { |
| 235 | dns_resolver *r = (dns_resolver *)gr; |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 236 | if (r->resolved_result != NULL) { |
Craig Tiller | 4cc1c35 | 2016-12-27 08:48:01 -0800 | [diff] [blame] | 237 | grpc_channel_args_destroy(exec_ctx, r->resolved_result); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 238 | } |
Craig Tiller | 9e5ac1b | 2017-02-14 22:25:50 -0800 | [diff] [blame] | 239 | grpc_pollset_set_destroy(exec_ctx, r->interested_parties); |
Mark D. Roth | 28ea7e2 | 2016-07-25 11:06:22 -0700 | [diff] [blame] | 240 | gpr_free(r->name_to_resolve); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 241 | gpr_free(r->default_port); |
Craig Tiller | 4cc1c35 | 2016-12-27 08:48:01 -0800 | [diff] [blame] | 242 | grpc_channel_args_destroy(exec_ctx, r->channel_args); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 243 | gpr_free(r); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Yuchen Zeng | 63e3e3b | 2016-12-15 12:06:33 -0800 | [diff] [blame] | 246 | static grpc_resolver *dns_create(grpc_exec_ctx *exec_ctx, |
| 247 | grpc_resolver_args *args, |
Mark D. Roth | 88405f7 | 2016-10-03 08:24:52 -0700 | [diff] [blame] | 248 | const char *default_port) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 249 | if (0 != strcmp(args->uri->authority, "")) { |
| 250 | gpr_log(GPR_ERROR, "authority based dns uri's not supported"); |
| 251 | return NULL; |
| 252 | } |
Mark D. Roth | 39b5871 | 2016-09-06 12:50:42 -0700 | [diff] [blame] | 253 | // Get name from args. |
Mark D. Roth | 37225c4 | 2017-01-18 07:23:36 -0800 | [diff] [blame] | 254 | char *path = args->uri->path; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 255 | if (path[0] == '/') ++path; |
Mark D. Roth | 28ea7e2 | 2016-07-25 11:06:22 -0700 | [diff] [blame] | 256 | // Create resolver. |
| 257 | dns_resolver *r = gpr_malloc(sizeof(dns_resolver)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 258 | memset(r, 0, sizeof(*r)); |
Craig Tiller | 0bfad14 | 2017-02-17 16:01:08 -0800 | [diff] [blame] | 259 | grpc_resolver_init(&r->base, &dns_resolver_vtable, args->combiner); |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 260 | r->name_to_resolve = gpr_strdup(path); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 261 | r->default_port = gpr_strdup(default_port); |
Mark D. Roth | 201db7d | 2016-12-12 09:36:02 -0800 | [diff] [blame] | 262 | r->channel_args = grpc_channel_args_copy(args->args); |
Yuchen Zeng | 63e3e3b | 2016-12-15 12:06:33 -0800 | [diff] [blame] | 263 | r->interested_parties = grpc_pollset_set_create(); |
| 264 | if (args->pollset_set != NULL) { |
| 265 | grpc_pollset_set_add_pollset_set(exec_ctx, r->interested_parties, |
| 266 | args->pollset_set); |
| 267 | } |
David Garcia Quintas | 1edfb95 | 2016-11-22 17:15:34 -0800 | [diff] [blame] | 268 | gpr_backoff_init(&r->backoff_state, GRPC_DNS_INITIAL_CONNECT_BACKOFF_SECONDS, |
| 269 | GRPC_DNS_RECONNECT_BACKOFF_MULTIPLIER, |
| 270 | GRPC_DNS_RECONNECT_JITTER, |
| 271 | GRPC_DNS_MIN_CONNECT_TIMEOUT_SECONDS * 1000, |
| 272 | GRPC_DNS_RECONNECT_MAX_BACKOFF_SECONDS * 1000); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 273 | return &r->base; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * FACTORY |
| 278 | */ |
| 279 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 280 | static void dns_factory_ref(grpc_resolver_factory *factory) {} |
| 281 | |
| 282 | static void dns_factory_unref(grpc_resolver_factory *factory) {} |
| 283 | |
| 284 | static grpc_resolver *dns_factory_create_resolver( |
Yuchen Zeng | 63e3e3b | 2016-12-15 12:06:33 -0800 | [diff] [blame] | 285 | grpc_exec_ctx *exec_ctx, grpc_resolver_factory *factory, |
| 286 | grpc_resolver_args *args) { |
| 287 | return dns_create(exec_ctx, args, "https"); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 290 | static char *dns_factory_get_default_host_name(grpc_resolver_factory *factory, |
| 291 | grpc_uri *uri) { |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 292 | const char *path = uri->path; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 293 | if (path[0] == '/') ++path; |
| 294 | return gpr_strdup(path); |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 297 | static const grpc_resolver_factory_vtable dns_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 298 | dns_factory_ref, dns_factory_unref, dns_factory_create_resolver, |
| 299 | dns_factory_get_default_host_name, "dns"}; |
| 300 | static grpc_resolver_factory dns_resolver_factory = {&dns_factory_vtable}; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 301 | |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 302 | static grpc_resolver_factory *dns_resolver_factory_create() { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 303 | return &dns_resolver_factory; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 304 | } |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 305 | |
| 306 | void grpc_resolver_dns_native_init(void) { |
| 307 | grpc_register_resolver_type(dns_resolver_factory_create()); |
| 308 | } |
| 309 | |
| 310 | void grpc_resolver_dns_native_shutdown(void) {} |