Craig Tiller | cd29c58 | 2015-06-24 09:15:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | a93a25f | 2016-01-28 13:55:49 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, 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 | |
| 34 | #include "src/core/client_config/resolvers/dns_resolver.h" |
| 35 | |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
| 38 | #include <grpc/support/alloc.h> |
Craig Tiller | 6b9f5c6 | 2015-07-29 18:47:35 -0700 | [diff] [blame] | 39 | #include <grpc/support/host_port.h> |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 40 | #include <grpc/support/string_util.h> |
| 41 | |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 42 | #include "src/core/client_config/lb_policy_registry.h" |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 43 | #include "src/core/iomgr/resolve_address.h" |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 44 | #include "src/core/iomgr/timer.h" |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 45 | #include "src/core/support/backoff.h" |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 46 | #include "src/core/support/string.h" |
| 47 | |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 48 | #define BACKOFF_MULTIPLIER 1.6 |
| 49 | #define BACKOFF_JITTER 0.2 |
| 50 | #define BACKOFF_MIN_SECONDS 1 |
| 51 | #define BACKOFF_MAX_SECONDS 120 |
| 52 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 53 | typedef struct { |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 54 | /** base class: must be first */ |
| 55 | grpc_resolver base; |
| 56 | /** refcount */ |
| 57 | gpr_refcount refs; |
| 58 | /** name to resolve */ |
| 59 | char *name; |
| 60 | /** default port to use */ |
| 61 | char *default_port; |
| 62 | /** subchannel factory */ |
| 63 | grpc_subchannel_factory *subchannel_factory; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 64 | /** load balancing policy name */ |
| 65 | char *lb_policy_name; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 66 | |
| 67 | /** mutex guarding the rest of the state */ |
| 68 | gpr_mu mu; |
| 69 | /** are we currently resolving? */ |
| 70 | int resolving; |
| 71 | /** which version of resolved_config have we published? */ |
| 72 | int published_version; |
| 73 | /** which version of resolved_config is current? */ |
| 74 | int resolved_version; |
| 75 | /** pending next completion, or NULL */ |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 76 | grpc_closure *next_completion; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 77 | /** target config address for next completion */ |
| 78 | grpc_client_config **target_config; |
| 79 | /** current (fully resolved) config */ |
| 80 | grpc_client_config *resolved_config; |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 81 | /** retry timer */ |
| 82 | bool have_retry_timer; |
| 83 | grpc_timer retry_timer; |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 84 | /** retry backoff state */ |
| 85 | gpr_backoff backoff_state; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 86 | } dns_resolver; |
| 87 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 89 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 90 | static void dns_start_resolving_locked(dns_resolver *r); |
| 91 | static void dns_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx, |
| 92 | dns_resolver *r); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 93 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 94 | static void dns_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 95 | static void dns_channel_saw_error(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 96 | static void dns_next(grpc_exec_ctx *exec_ctx, grpc_resolver *r, |
| 97 | grpc_client_config **target_config, |
| 98 | grpc_closure *on_complete); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 99 | |
| 100 | static const grpc_resolver_vtable dns_resolver_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 101 | dns_destroy, dns_shutdown, dns_channel_saw_error, dns_next}; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 102 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 103 | static void dns_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver) { |
| 104 | dns_resolver *r = (dns_resolver *)resolver; |
| 105 | gpr_mu_lock(&r->mu); |
Craig Tiller | e2327db | 2016-03-11 09:52:42 -0800 | [diff] [blame] | 106 | if (r->have_retry_timer) { |
| 107 | grpc_timer_cancel(exec_ctx, &r->retry_timer); |
| 108 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 109 | if (r->next_completion != NULL) { |
| 110 | *r->target_config = NULL; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 111 | grpc_exec_ctx_enqueue(exec_ctx, r->next_completion, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 112 | r->next_completion = NULL; |
| 113 | } |
| 114 | gpr_mu_unlock(&r->mu); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 117 | static void dns_channel_saw_error(grpc_exec_ctx *exec_ctx, |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 118 | grpc_resolver *resolver) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 119 | dns_resolver *r = (dns_resolver *)resolver; |
| 120 | gpr_mu_lock(&r->mu); |
| 121 | if (!r->resolving) { |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 122 | gpr_backoff_reset(&r->backoff_state); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | dns_start_resolving_locked(r); |
| 124 | } |
| 125 | gpr_mu_unlock(&r->mu); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 128 | static void dns_next(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver, |
| 129 | grpc_client_config **target_config, |
| 130 | grpc_closure *on_complete) { |
| 131 | dns_resolver *r = (dns_resolver *)resolver; |
| 132 | gpr_mu_lock(&r->mu); |
| 133 | GPR_ASSERT(!r->next_completion); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 134 | r->next_completion = on_complete; |
| 135 | r->target_config = target_config; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 136 | if (r->resolved_version == 0 && !r->resolving) { |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 137 | gpr_backoff_reset(&r->backoff_state); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 138 | dns_start_resolving_locked(r); |
| 139 | } else { |
| 140 | dns_maybe_finish_next_locked(exec_ctx, r); |
| 141 | } |
| 142 | gpr_mu_unlock(&r->mu); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 145 | static void dns_on_retry_timer(grpc_exec_ctx *exec_ctx, void *arg, |
| 146 | bool success) { |
| 147 | dns_resolver *r = arg; |
| 148 | |
Craig Tiller | e2327db | 2016-03-11 09:52:42 -0800 | [diff] [blame] | 149 | gpr_mu_lock(&r->mu); |
| 150 | r->have_retry_timer = false; |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 151 | if (success) { |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 152 | if (!r->resolving) { |
| 153 | dns_start_resolving_locked(r); |
| 154 | } |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 155 | } |
Craig Tiller | e2327db | 2016-03-11 09:52:42 -0800 | [diff] [blame] | 156 | gpr_mu_unlock(&r->mu); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 157 | |
| 158 | GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "retry-timer"); |
| 159 | } |
| 160 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 161 | static void dns_on_resolved(grpc_exec_ctx *exec_ctx, void *arg, |
| 162 | grpc_resolved_addresses *addresses) { |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 163 | dns_resolver *r = arg; |
| 164 | grpc_client_config *config = NULL; |
| 165 | grpc_subchannel **subchannels; |
| 166 | grpc_subchannel_args args; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 167 | grpc_lb_policy *lb_policy; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 168 | size_t i; |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 169 | gpr_mu_lock(&r->mu); |
| 170 | GPR_ASSERT(r->resolving); |
| 171 | r->resolving = 0; |
| 172 | if (addresses != NULL) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 173 | grpc_lb_policy_args lb_policy_args; |
| 174 | config = grpc_client_config_create(); |
| 175 | subchannels = gpr_malloc(sizeof(grpc_subchannel *) * addresses->naddrs); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 176 | size_t naddrs = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 177 | for (i = 0; i < addresses->naddrs; i++) { |
| 178 | memset(&args, 0, sizeof(args)); |
| 179 | args.addr = (struct sockaddr *)(addresses->addrs[i].addr); |
| 180 | args.addr_len = (size_t)addresses->addrs[i].len; |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 181 | grpc_subchannel *subchannel = grpc_subchannel_factory_create_subchannel( |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 182 | exec_ctx, r->subchannel_factory, &args); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 183 | if (subchannel != NULL) { |
| 184 | subchannels[naddrs++] = subchannel; |
| 185 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 186 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 187 | memset(&lb_policy_args, 0, sizeof(lb_policy_args)); |
| 188 | lb_policy_args.subchannels = subchannels; |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 189 | lb_policy_args.num_subchannels = naddrs; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 190 | lb_policy = grpc_lb_policy_create(r->lb_policy_name, &lb_policy_args); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 191 | if (lb_policy != NULL) { |
| 192 | grpc_client_config_set_lb_policy(config, lb_policy); |
| 193 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "construction"); |
| 194 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 195 | grpc_resolved_addresses_destroy(addresses); |
| 196 | gpr_free(subchannels); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 197 | } else { |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 198 | gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC); |
| 199 | gpr_timespec next_try = gpr_backoff_step(&r->backoff_state, now); |
| 200 | gpr_timespec timeout = gpr_time_sub(next_try, now); |
| 201 | gpr_log(GPR_DEBUG, "dns resolution failed: retrying in %d.%09d seconds", |
| 202 | timeout.tv_sec, timeout.tv_nsec); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 203 | GPR_ASSERT(!r->have_retry_timer); |
| 204 | r->have_retry_timer = true; |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 205 | GRPC_RESOLVER_REF(&r->base, "retry-timer"); |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 206 | grpc_timer_init(exec_ctx, &r->retry_timer, next_try, dns_on_retry_timer, r, |
| 207 | now); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 208 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 209 | if (r->resolved_config) { |
| 210 | grpc_client_config_unref(exec_ctx, r->resolved_config); |
| 211 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 212 | r->resolved_config = config; |
| 213 | r->resolved_version++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 214 | dns_maybe_finish_next_locked(exec_ctx, r); |
| 215 | gpr_mu_unlock(&r->mu); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 216 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 217 | GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "dns-resolving"); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 220 | static void dns_start_resolving_locked(dns_resolver *r) { |
| 221 | GRPC_RESOLVER_REF(&r->base, "dns-resolving"); |
| 222 | GPR_ASSERT(!r->resolving); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 223 | r->resolving = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 224 | grpc_resolve_address(r->name, r->default_port, dns_on_resolved, r); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 227 | static void dns_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx, |
| 228 | dns_resolver *r) { |
| 229 | if (r->next_completion != NULL && |
| 230 | r->resolved_version != r->published_version) { |
| 231 | *r->target_config = r->resolved_config; |
| 232 | if (r->resolved_config) { |
| 233 | grpc_client_config_ref(r->resolved_config); |
Craig Tiller | abf3638 | 2015-06-29 16:13:27 -0700 | [diff] [blame] | 234 | } |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 235 | grpc_exec_ctx_enqueue(exec_ctx, r->next_completion, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 236 | r->next_completion = NULL; |
| 237 | r->published_version = r->resolved_version; |
| 238 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 241 | static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) { |
| 242 | dns_resolver *r = (dns_resolver *)gr; |
| 243 | gpr_mu_destroy(&r->mu); |
| 244 | if (r->resolved_config) { |
| 245 | grpc_client_config_unref(exec_ctx, r->resolved_config); |
| 246 | } |
| 247 | grpc_subchannel_factory_unref(exec_ctx, r->subchannel_factory); |
| 248 | gpr_free(r->name); |
| 249 | gpr_free(r->default_port); |
| 250 | gpr_free(r->lb_policy_name); |
| 251 | gpr_free(r); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 254 | static grpc_resolver *dns_create(grpc_resolver_args *args, |
| 255 | const char *default_port, |
| 256 | const char *lb_policy_name) { |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 257 | dns_resolver *r; |
Craig Tiller | 06a43f5 | 2015-09-15 07:41:28 -0700 | [diff] [blame] | 258 | const char *path = args->uri->path; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 259 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 260 | if (0 != strcmp(args->uri->authority, "")) { |
| 261 | gpr_log(GPR_ERROR, "authority based dns uri's not supported"); |
| 262 | return NULL; |
| 263 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 264 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 265 | if (path[0] == '/') ++path; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 266 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 267 | r = gpr_malloc(sizeof(dns_resolver)); |
| 268 | memset(r, 0, sizeof(*r)); |
| 269 | gpr_ref_init(&r->refs, 1); |
| 270 | gpr_mu_init(&r->mu); |
| 271 | grpc_resolver_init(&r->base, &dns_resolver_vtable); |
| 272 | r->name = gpr_strdup(path); |
| 273 | r->default_port = gpr_strdup(default_port); |
Craig Tiller | 06a43f5 | 2015-09-15 07:41:28 -0700 | [diff] [blame] | 274 | r->subchannel_factory = args->subchannel_factory; |
Craig Tiller | 476bb3b | 2016-03-11 12:54:40 -0800 | [diff] [blame] | 275 | gpr_backoff_init(&r->backoff_state, BACKOFF_MULTIPLIER, BACKOFF_JITTER, |
| 276 | BACKOFF_MIN_SECONDS * 1000, BACKOFF_MAX_SECONDS * 1000); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 277 | grpc_subchannel_factory_ref(r->subchannel_factory); |
| 278 | r->lb_policy_name = gpr_strdup(lb_policy_name); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 279 | return &r->base; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * FACTORY |
| 284 | */ |
| 285 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 286 | static void dns_factory_ref(grpc_resolver_factory *factory) {} |
| 287 | |
| 288 | static void dns_factory_unref(grpc_resolver_factory *factory) {} |
| 289 | |
| 290 | static grpc_resolver *dns_factory_create_resolver( |
| 291 | grpc_resolver_factory *factory, grpc_resolver_args *args) { |
| 292 | return dns_create(args, "https", "pick_first"); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 295 | char *dns_factory_get_default_host_name(grpc_resolver_factory *factory, |
| 296 | grpc_uri *uri) { |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 297 | const char *path = uri->path; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 298 | if (path[0] == '/') ++path; |
| 299 | return gpr_strdup(path); |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 302 | static const grpc_resolver_factory_vtable dns_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 303 | dns_factory_ref, dns_factory_unref, dns_factory_create_resolver, |
| 304 | dns_factory_get_default_host_name, "dns"}; |
| 305 | static grpc_resolver_factory dns_resolver_factory = {&dns_factory_vtable}; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 306 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 307 | grpc_resolver_factory *grpc_dns_resolver_factory_create() { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 308 | return &dns_resolver_factory; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 309 | } |