|
|
|
@ -38,6 +38,7 @@ |
|
|
|
|
#include <grpc/support/host_port.h> |
|
|
|
|
#include <grpc/support/string_util.h> |
|
|
|
|
|
|
|
|
|
#include "src/core/ext/client_config/http_connect_handshaker.h" |
|
|
|
|
#include "src/core/ext/client_config/lb_policy_registry.h" |
|
|
|
|
#include "src/core/ext/client_config/resolver_registry.h" |
|
|
|
|
#include "src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.h" |
|
|
|
@ -56,24 +57,22 @@ typedef struct { |
|
|
|
|
grpc_resolver base; |
|
|
|
|
/** refcount */ |
|
|
|
|
gpr_refcount refs; |
|
|
|
|
/** name to resolve */ |
|
|
|
|
char *name; |
|
|
|
|
/** target name */ |
|
|
|
|
char *target_name; |
|
|
|
|
/** name to resolve (usually the same as target_name) */ |
|
|
|
|
char *name_to_resolve; |
|
|
|
|
/** default port to use */ |
|
|
|
|
char *default_port; |
|
|
|
|
/** subchannel factory */ |
|
|
|
|
grpc_client_channel_factory *client_channel_factory; |
|
|
|
|
/** load balancing policy name */ |
|
|
|
|
char *lb_policy_name; |
|
|
|
|
/** polling entity for driving the async IO events */ |
|
|
|
|
grpc_polling_entity *pollent; |
|
|
|
|
|
|
|
|
|
/** mutex guarding the rest of the state */ |
|
|
|
|
gpr_mu mu; |
|
|
|
|
/** are we currently resolving? */ |
|
|
|
|
int resolving; |
|
|
|
|
/** which version of resolved_result have we published? */ |
|
|
|
|
bool resolving; |
|
|
|
|
/** which version of the result have we published? */ |
|
|
|
|
int published_version; |
|
|
|
|
/** which version of resolved_result is current? */ |
|
|
|
|
/** which version of the result is current? */ |
|
|
|
|
int resolved_version; |
|
|
|
|
/** pending next completion, or NULL */ |
|
|
|
|
grpc_closure *next_completion; |
|
|
|
@ -90,6 +89,7 @@ typedef struct { |
|
|
|
|
/** currently resolving addresses */ |
|
|
|
|
grpc_resolved_addresses *addresses; |
|
|
|
|
|
|
|
|
|
grpc_polling_entity *pollent; |
|
|
|
|
} dns_resolver; |
|
|
|
|
|
|
|
|
|
static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
|
|
|
@ -155,29 +155,27 @@ static void dns_on_resolved(grpc_exec_ctx *exec_ctx, void *arg, |
|
|
|
|
grpc_error *error) { |
|
|
|
|
dns_resolver *r = arg; |
|
|
|
|
grpc_resolver_result *result = NULL; |
|
|
|
|
grpc_lb_policy *lb_policy; |
|
|
|
|
gpr_mu_lock(&r->mu); |
|
|
|
|
GPR_ASSERT(r->resolving); |
|
|
|
|
r->resolving = 0; |
|
|
|
|
grpc_resolved_addresses *addresses = r->addresses; |
|
|
|
|
if (addresses != NULL) { |
|
|
|
|
grpc_lb_policy_args lb_policy_args; |
|
|
|
|
result = grpc_resolver_result_create(); |
|
|
|
|
memset(&lb_policy_args, 0, sizeof(lb_policy_args)); |
|
|
|
|
lb_policy_args.addresses = addresses; |
|
|
|
|
lb_policy_args.client_channel_factory = r->client_channel_factory; |
|
|
|
|
lb_policy = |
|
|
|
|
grpc_lb_policy_create(exec_ctx, r->lb_policy_name, &lb_policy_args); |
|
|
|
|
if (lb_policy != NULL) { |
|
|
|
|
grpc_resolver_result_set_lb_policy(result, lb_policy); |
|
|
|
|
GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "construction"); |
|
|
|
|
r->resolving = false; |
|
|
|
|
if (r->addresses != NULL) { |
|
|
|
|
grpc_lb_addresses *addresses = |
|
|
|
|
grpc_lb_addresses_create(r->addresses->naddrs); |
|
|
|
|
for (size_t i = 0; i < r->addresses->naddrs; ++i) { |
|
|
|
|
grpc_lb_addresses_set_address( |
|
|
|
|
addresses, i, &r->addresses->addrs[i].addr, |
|
|
|
|
r->addresses->addrs[i].len, false /* is_balancer */, |
|
|
|
|
NULL /* balancer_name */, NULL /* user_data */); |
|
|
|
|
} |
|
|
|
|
grpc_resolved_addresses_destroy(r->addresses); |
|
|
|
|
result = grpc_resolver_result_create(r->target_name, addresses, |
|
|
|
|
r->lb_policy_name, NULL); |
|
|
|
|
; |
|
|
|
|
if (r->pollent) { |
|
|
|
|
grpc_polling_entity_del_from_pollset_set(exec_ctx, r->pollent, |
|
|
|
|
r->base.pollset_set); |
|
|
|
|
r->pollent = NULL; |
|
|
|
|
} |
|
|
|
|
grpc_resolved_addresses_destroy(addresses); |
|
|
|
|
} else { |
|
|
|
|
gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC); |
|
|
|
|
gpr_timespec next_try = gpr_backoff_step(&r->backoff_state, now); |
|
|
|
@ -220,7 +218,7 @@ static void dns_next(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver, |
|
|
|
|
gpr_backoff_reset(&r->backoff_state); |
|
|
|
|
GRPC_RESOLVER_REF(&r->base, "dns-resolving"); |
|
|
|
|
GPR_ASSERT(!r->resolving); |
|
|
|
|
r->resolving = 1; |
|
|
|
|
r->resolving = true; |
|
|
|
|
r->addresses = NULL; |
|
|
|
|
r->pollent = NULL; |
|
|
|
|
if (grpc_ares_need_poll_entity() && pollent) { |
|
|
|
@ -231,7 +229,7 @@ static void dns_next(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver, |
|
|
|
|
gpr_log(GPR_DEBUG, "dns_next is called without giving a pollent"); |
|
|
|
|
} |
|
|
|
|
grpc_resolve_address_ares( |
|
|
|
|
exec_ctx, r->name, r->default_port, r->base.pollset_set, |
|
|
|
|
exec_ctx, r->name_to_resolve, r->default_port, r->base.pollset_set, |
|
|
|
|
grpc_closure_create(dns_on_resolved, r), &r->addresses); |
|
|
|
|
} else { |
|
|
|
|
dns_maybe_finish_next_locked(exec_ctx, r); |
|
|
|
@ -243,10 +241,10 @@ static void dns_start_resolving_locked(grpc_exec_ctx *exec_ctx, |
|
|
|
|
dns_resolver *r) { |
|
|
|
|
GRPC_RESOLVER_REF(&r->base, "dns-resolving"); |
|
|
|
|
GPR_ASSERT(!r->resolving); |
|
|
|
|
r->resolving = 1; |
|
|
|
|
r->resolving = true; |
|
|
|
|
r->addresses = NULL; |
|
|
|
|
grpc_resolve_address_ares( |
|
|
|
|
exec_ctx, r->name, r->default_port, r->base.pollset_set, |
|
|
|
|
exec_ctx, r->name_to_resolve, r->default_port, r->base.pollset_set, |
|
|
|
|
grpc_closure_create(dns_on_resolved, r), &r->addresses); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -271,8 +269,8 @@ static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) { |
|
|
|
|
if (r->resolved_result) { |
|
|
|
|
grpc_resolver_result_unref(exec_ctx, r->resolved_result); |
|
|
|
|
} |
|
|
|
|
grpc_client_channel_factory_unref(exec_ctx, r->client_channel_factory); |
|
|
|
|
gpr_free(r->name); |
|
|
|
|
gpr_free(r->target_name); |
|
|
|
|
gpr_free(r->name_to_resolve); |
|
|
|
|
gpr_free(r->default_port); |
|
|
|
|
gpr_free(r->lb_policy_name); |
|
|
|
|
gpr_free(r); |
|
|
|
@ -281,34 +279,26 @@ static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) { |
|
|
|
|
static grpc_resolver *dns_create(grpc_resolver_args *args, |
|
|
|
|
const char *default_port, |
|
|
|
|
const char *lb_policy_name) { |
|
|
|
|
dns_resolver *r; |
|
|
|
|
grpc_error *error = GRPC_ERROR_NONE; |
|
|
|
|
const char *path = args->uri->path; |
|
|
|
|
|
|
|
|
|
if (0 != strcmp(args->uri->authority, "")) { |
|
|
|
|
gpr_log(GPR_ERROR, "authority based dns uri's not supported"); |
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
error = grpc_ares_init(); |
|
|
|
|
if (error != GRPC_ERROR_NONE) { |
|
|
|
|
GRPC_LOG_IF_ERROR("ares_library_init() failed", error); |
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Get name from args.
|
|
|
|
|
const char *path = args->uri->path; |
|
|
|
|
if (path[0] == '/') ++path; |
|
|
|
|
|
|
|
|
|
r = gpr_malloc(sizeof(dns_resolver)); |
|
|
|
|
// Get proxy name, if any.
|
|
|
|
|
char *proxy_name = grpc_get_http_proxy_server(); |
|
|
|
|
// Create resolver.
|
|
|
|
|
dns_resolver *r = gpr_malloc(sizeof(dns_resolver)); |
|
|
|
|
memset(r, 0, sizeof(*r)); |
|
|
|
|
gpr_ref_init(&r->refs, 1); |
|
|
|
|
gpr_mu_init(&r->mu); |
|
|
|
|
grpc_resolver_init(&r->base, &dns_resolver_vtable); |
|
|
|
|
r->name = gpr_strdup(path); |
|
|
|
|
r->target_name = gpr_strdup(path); |
|
|
|
|
r->name_to_resolve = proxy_name == NULL ? gpr_strdup(path) : proxy_name; |
|
|
|
|
r->default_port = gpr_strdup(default_port); |
|
|
|
|
r->client_channel_factory = args->client_channel_factory; |
|
|
|
|
gpr_backoff_init(&r->backoff_state, BACKOFF_MULTIPLIER, BACKOFF_JITTER, |
|
|
|
|
BACKOFF_MIN_SECONDS * 1000, BACKOFF_MAX_SECONDS * 1000); |
|
|
|
|
grpc_client_channel_factory_ref(r->client_channel_factory); |
|
|
|
|
r->lb_policy_name = gpr_strdup(lb_policy_name); |
|
|
|
|
return &r->base; |
|
|
|
|
} |
|
|
|
|