diff --git a/src/core/lib/iomgr/resolve_address.h b/src/core/lib/iomgr/resolve_address.h index ddbe3757555..7a770662ccf 100644 --- a/src/core/lib/iomgr/resolve_address.h +++ b/src/core/lib/iomgr/resolve_address.h @@ -66,4 +66,11 @@ extern grpc_error *(*grpc_blocking_resolve_address)( const char *name, const char *default_port, grpc_resolved_addresses **addresses); +/* Returns GRPC_ERROR_CANCELLED by default. If it's overriden and returns + error other than GRPC_ERROR_CANCELLED, grpc_resolve_address will use its + result. Result must be freed with grpc_resolved_addresses_destroy. */ +extern grpc_error *(*grpc_customized_resolve_address)( + const char *name, const char *default_port, + grpc_resolved_addresses **addresses); + #endif /* GRPC_CORE_LIB_IOMGR_RESOLVE_ADDRESS_H */ diff --git a/src/core/lib/iomgr/resolve_address_posix.c b/src/core/lib/iomgr/resolve_address_posix.c index 4e9f978584a..4012dcc61ff 100644 --- a/src/core/lib/iomgr/resolve_address_posix.c +++ b/src/core/lib/iomgr/resolve_address_posix.c @@ -149,6 +149,17 @@ grpc_error *(*grpc_blocking_resolve_address)( const char *name, const char *default_port, grpc_resolved_addresses **addresses) = blocking_resolve_address_impl; +static grpc_error *default_customized_resolve_address_impl( + const char *name, const char *default_port, + grpc_resolved_addresses **addresses) { + return GRPC_ERROR_CANCELLED; +} + +grpc_error *(*grpc_customized_resolve_address)( + const char *name, const char *default_port, + grpc_resolved_addresses **addresses) = + default_customized_resolve_address_impl; + typedef struct { char *name; char *default_port; @@ -183,7 +194,16 @@ static void resolve_address_impl(grpc_exec_ctx *exec_ctx, const char *name, const char *default_port, grpc_closure *on_done, grpc_resolved_addresses **addrs) { - request *r = gpr_malloc(sizeof(request)); + request *r; + grpc_error *err; + + if ((err = grpc_customized_resolve_address(name, default_port, addrs)) != + GRPC_ERROR_CANCELLED) { + grpc_exec_ctx_sched(exec_ctx, on_done, err, NULL); + return; + } + + r = gpr_malloc(sizeof(request)); grpc_closure_init(&r->request_closure, do_request_thread, r); r->name = gpr_strdup(name); r->default_port = gpr_strdup(default_port); diff --git a/src/core/lib/iomgr/resolve_address_windows.c b/src/core/lib/iomgr/resolve_address_windows.c index 2af8af82dcb..e28eaac4923 100644 --- a/src/core/lib/iomgr/resolve_address_windows.c +++ b/src/core/lib/iomgr/resolve_address_windows.c @@ -143,6 +143,17 @@ grpc_error *(*grpc_blocking_resolve_address)( const char *name, const char *default_port, grpc_resolved_addresses **addresses) = blocking_resolve_address_impl; +static grpc_error *default_customized_resolve_address_impl( + const char *name, const char *default_port, + grpc_resolved_addresses **addresses) { + return GRPC_ERROR_CANCELLED; +} + +grpc_error *(*grpc_customized_resolve_address)( + const char *name, const char *default_port, + grpc_resolved_addresses **addresses) = + default_customized_resolve_address_impl; + /* Callback to be passed to grpc_executor to asynch-ify * grpc_blocking_resolve_address */ static void do_request_thread(grpc_exec_ctx *exec_ctx, void *rp, @@ -171,7 +182,16 @@ static void resolve_address_impl(grpc_exec_ctx *exec_ctx, const char *name, const char *default_port, grpc_closure *on_done, grpc_resolved_addresses **addresses) { - request *r = gpr_malloc(sizeof(request)); + request *r; + grpc_error *err; + + if ((err = grpc_customized_resolve_address(name, default_port, addrs)) != + GRPC_ERROR_CANCELLED) { + grpc_exec_ctx_sched(exec_ctx, on_done, err, NULL); + return; + } + + r = gpr_malloc(sizeof(request)); grpc_closure_init(&r->request_closure, do_request_thread, r); r->name = gpr_strdup(name); r->default_port = gpr_strdup(default_port); diff --git a/test/core/client_config/resolvers/dns_resolver_connectivity_test.c b/test/core/client_config/resolvers/dns_resolver_connectivity_test.c index 11e6a89cda5..6002b0b7bb7 100644 --- a/test/core/client_config/resolvers/dns_resolver_connectivity_test.c +++ b/test/core/client_config/resolvers/dns_resolver_connectivity_test.c @@ -123,7 +123,7 @@ int main(int argc, char **argv) { grpc_init(); gpr_mu_init(&g_mu); - grpc_blocking_resolve_address = my_resolve_address; + grpc_customized_resolve_address = my_resolve_address; grpc_resolver *resolver = create_resolver("dns:test"); diff --git a/test/core/end2end/goaway_server_test.c b/test/core/end2end/goaway_server_test.c index 67cc24c74c5..ed8cc3dd68e 100644 --- a/test/core/end2end/goaway_server_test.c +++ b/test/core/end2end/goaway_server_test.c @@ -46,9 +46,6 @@ static void *tag(intptr_t i) { return (void *)i; } static gpr_mu g_mu; static int g_resolve_port = -1; -static grpc_error *(*iomgr_resolve_address)(const char *name, - const char *default_port, - grpc_resolved_addresses **addrs); static void set_resolve_port(int port) { gpr_mu_lock(&g_mu); @@ -59,7 +56,7 @@ static void set_resolve_port(int port) { static grpc_error *my_resolve_address(const char *name, const char *addr, grpc_resolved_addresses **addrs) { if (0 != strcmp(name, "test")) { - return iomgr_resolve_address(name, addr, addrs); + return GRPC_ERROR_CANCELLED; } gpr_mu_lock(&g_mu); @@ -90,8 +87,7 @@ int main(int argc, char **argv) { grpc_test_init(argc, argv); gpr_mu_init(&g_mu); - iomgr_resolve_address = grpc_blocking_resolve_address; - grpc_blocking_resolve_address = my_resolve_address; + grpc_customized_resolve_address = my_resolve_address; grpc_init(); int was_cancelled1;