Rewrite grpc_customized_resolve_address

pull/7900/head
Yuchen Zeng 8 years ago
parent dcd78be8c3
commit a0941f4b8d
  1. 5
      src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.c
  2. 10
      src/core/lib/iomgr/resolve_address.h
  3. 16
      src/core/lib/iomgr/resolve_address_posix.c
  4. 18
      src/core/lib/iomgr/resolve_address_windows.c
  5. 10
      test/core/client_config/resolvers/dns_resolver_connectivity_test.c
  6. 12
      test/core/end2end/goaway_server_test.c

@ -242,11 +242,12 @@ void grpc_resolve_address_ares_impl(grpc_exec_ctx *exec_ctx, const char *name,
grpc_ares_request *r = NULL; grpc_ares_request *r = NULL;
grpc_ares_ev_driver *ev_driver; grpc_ares_ev_driver *ev_driver;
if ((err = grpc_customized_resolve_address(name, default_port, addrs)) != if (grpc_customized_resolve_address(name, default_port, addrs, &err) != 0) {
GRPC_ERROR_CANCELLED) {
grpc_exec_ctx_sched(exec_ctx, on_done, err, NULL); grpc_exec_ctx_sched(exec_ctx, on_done, err, NULL);
return; return;
} }
GRPC_ERROR_UNREF(err);
err = GRPC_ERROR_NONE;
/* parse name, splitting it into host and port parts */ /* parse name, splitting it into host and port parts */
gpr_split_host_port(name, &host, &port); gpr_split_host_port(name, &host, &port);

@ -66,11 +66,11 @@ extern grpc_error *(*grpc_blocking_resolve_address)(
const char *name, const char *default_port, const char *name, const char *default_port,
grpc_resolved_addresses **addresses); grpc_resolved_addresses **addresses);
/* Returns GRPC_ERROR_CANCELLED by default. If it's overriden and returns /* Returns 0 by default. If it's overriden and returns value other than 0,
error other than GRPC_ERROR_CANCELLED, grpc_resolve_address will use its grpc_resolve_address will use its result. Result must be freed with
result. Result must be freed with grpc_resolved_addresses_destroy. */ grpc_resolved_addresses_destroy. */
extern grpc_error *(*grpc_customized_resolve_address)( extern int (*grpc_customized_resolve_address)(
const char *name, const char *default_port, const char *name, const char *default_port,
grpc_resolved_addresses **addresses); grpc_resolved_addresses **addresses, grpc_error **error);
#endif /* GRPC_CORE_LIB_IOMGR_RESOLVE_ADDRESS_H */ #endif /* GRPC_CORE_LIB_IOMGR_RESOLVE_ADDRESS_H */

@ -149,16 +149,17 @@ grpc_error *(*grpc_blocking_resolve_address)(
const char *name, const char *default_port, const char *name, const char *default_port,
grpc_resolved_addresses **addresses) = blocking_resolve_address_impl; grpc_resolved_addresses **addresses) = blocking_resolve_address_impl;
static grpc_error *default_customized_resolve_address_impl( static int default_customized_resolve_address_impl(
const char *name, const char *default_port, const char *name, const char *default_port,
grpc_resolved_addresses **addresses) { grpc_resolved_addresses **addresses, grpc_error **error) {
return GRPC_ERROR_CANCELLED; *error = GRPC_ERROR_NONE;
return 0;
} }
grpc_error *(*grpc_customized_resolve_address)( int (*grpc_customized_resolve_address)(
const char *name, const char *default_port, const char *name, const char *default_port,
grpc_resolved_addresses **addresses) = grpc_resolved_addresses **addresses,
default_customized_resolve_address_impl; grpc_error **error) = default_customized_resolve_address_impl;
typedef struct { typedef struct {
char *name; char *name;
@ -197,8 +198,7 @@ static void resolve_address_impl(grpc_exec_ctx *exec_ctx, const char *name,
request *r; request *r;
grpc_error *err; grpc_error *err;
if ((err = grpc_customized_resolve_address(name, default_port, addrs)) != if (grpc_customized_resolve_address(name, default_port, addrs, &err) != 0) {
GRPC_ERROR_CANCELLED) {
grpc_exec_ctx_sched(exec_ctx, on_done, err, NULL); grpc_exec_ctx_sched(exec_ctx, on_done, err, NULL);
return; return;
} }

@ -143,16 +143,18 @@ grpc_error *(*grpc_blocking_resolve_address)(
const char *name, const char *default_port, const char *name, const char *default_port,
grpc_resolved_addresses **addresses) = blocking_resolve_address_impl; grpc_resolved_addresses **addresses) = blocking_resolve_address_impl;
static grpc_error *default_customized_resolve_address_impl( static int default_customized_resolve_address_impl(
const char *name, const char *default_port, const char *name, const char *default_port,
grpc_resolved_addresses **addresses) { grpc_resolved_addresses **addresses, grpc_error **error) {
return GRPC_ERROR_CANCELLED; *addresses = NULL;
*error = GRPC_ERROR_CANCELLED;
return 0;
} }
grpc_error *(*grpc_customized_resolve_address)( int (*grpc_customized_resolve_address)(
const char *name, const char *default_port, const char *name, const char *default_port,
grpc_resolved_addresses **addresses) = grpc_resolved_addresses **addresses,
default_customized_resolve_address_impl; grpc_error **error) = default_customized_resolve_address_impl;
/* Callback to be passed to grpc_executor to asynch-ify /* Callback to be passed to grpc_executor to asynch-ify
* grpc_blocking_resolve_address */ * grpc_blocking_resolve_address */
@ -185,8 +187,8 @@ static void resolve_address_impl(grpc_exec_ctx *exec_ctx, const char *name,
request *r; request *r;
grpc_error *err; grpc_error *err;
if ((err = grpc_customized_resolve_address(name, default_port, addresses)) != if (grpc_customized_resolve_address(name, default_port, addresses, &err) !=
GRPC_ERROR_CANCELLED) { 0) {
grpc_exec_ctx_sched(exec_ctx, on_done, err, NULL); grpc_exec_ctx_sched(exec_ctx, on_done, err, NULL);
return; return;
} }

@ -67,22 +67,24 @@ static grpc_client_channel_factory cc_factory = {&sc_vtable};
static gpr_mu g_mu; static gpr_mu g_mu;
static bool g_fail_resolution = true; static bool g_fail_resolution = true;
static grpc_error *my_resolve_address(const char *name, const char *addr, static int my_resolve_address(const char *name, const char *addr,
grpc_resolved_addresses **addrs) { grpc_resolved_addresses **addrs,
grpc_error **error) {
gpr_mu_lock(&g_mu); gpr_mu_lock(&g_mu);
GPR_ASSERT(0 == strcmp("test", name)); GPR_ASSERT(0 == strcmp("test", name));
if (g_fail_resolution) { if (g_fail_resolution) {
g_fail_resolution = false; g_fail_resolution = false;
gpr_mu_unlock(&g_mu); gpr_mu_unlock(&g_mu);
return GRPC_ERROR_CREATE("Forced Failure"); *error = GRPC_ERROR_CREATE("Forced Failure");
} else { } else {
gpr_mu_unlock(&g_mu); gpr_mu_unlock(&g_mu);
*addrs = gpr_malloc(sizeof(**addrs)); *addrs = gpr_malloc(sizeof(**addrs));
(*addrs)->naddrs = 1; (*addrs)->naddrs = 1;
(*addrs)->addrs = gpr_malloc(sizeof(*(*addrs)->addrs)); (*addrs)->addrs = gpr_malloc(sizeof(*(*addrs)->addrs));
(*addrs)->addrs[0].len = 123; (*addrs)->addrs[0].len = 123;
return GRPC_ERROR_NONE; *error = GRPC_ERROR_NONE;
} }
return 1;
} }
static grpc_resolver *create_resolver(const char *name) { static grpc_resolver *create_resolver(const char *name) {

@ -53,16 +53,17 @@ static void set_resolve_port(int port) {
gpr_mu_unlock(&g_mu); gpr_mu_unlock(&g_mu);
} }
static grpc_error *my_resolve_address(const char *name, const char *addr, static int my_resolve_address(const char *name, const char *addr,
grpc_resolved_addresses **addrs) { grpc_resolved_addresses **addrs,
grpc_error **error) {
if (0 != strcmp(name, "test")) { if (0 != strcmp(name, "test")) {
return GRPC_ERROR_CANCELLED; return 0;
} }
gpr_mu_lock(&g_mu); gpr_mu_lock(&g_mu);
if (g_resolve_port < 0) { if (g_resolve_port < 0) {
gpr_mu_unlock(&g_mu); gpr_mu_unlock(&g_mu);
return GRPC_ERROR_CREATE("Forced Failure"); *error = GRPC_ERROR_CREATE("Forced Failure");
} else { } else {
*addrs = gpr_malloc(sizeof(**addrs)); *addrs = gpr_malloc(sizeof(**addrs));
(*addrs)->naddrs = 1; (*addrs)->naddrs = 1;
@ -74,8 +75,9 @@ static grpc_error *my_resolve_address(const char *name, const char *addr,
sa->sin_port = htons((uint16_t)g_resolve_port); sa->sin_port = htons((uint16_t)g_resolve_port);
(*addrs)->addrs[0].len = sizeof(*sa); (*addrs)->addrs[0].len = sizeof(*sa);
gpr_mu_unlock(&g_mu); gpr_mu_unlock(&g_mu);
return GRPC_ERROR_NONE; *error = GRPC_ERROR_NONE;
} }
return 1;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {

Loading…
Cancel
Save