From 0eb581f6b53a66c36a4a7562451ce2ce79c07883 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 25 Feb 2023 21:16:53 +1300 Subject: [PATCH] Non-blocking hostname -> numeric address. --- contrib/ruby/ext/trilogy-ruby/cext.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/contrib/ruby/ext/trilogy-ruby/cext.c b/contrib/ruby/ext/trilogy-ruby/cext.c index 46cd4db4..a7b8a592 100644 --- a/contrib/ruby/ext/trilogy-ruby/cext.c +++ b/contrib/ruby/ext/trilogy-ruby/cext.c @@ -413,7 +413,7 @@ static void *no_gvl_resolve(void *data) return NULL; } -static int try_connect(struct trilogy_ctx *ctx, trilogy_handshake_t *handshake, const trilogy_sockopt_t *opts) +static int try_connect(struct trilogy_ctx *ctx, trilogy_handshake_t *handshake, trilogy_sockopt_t *opts) { trilogy_sock_t *sock = trilogy_sock_new(opts); if (sock == NULL) { @@ -422,6 +422,28 @@ static int try_connect(struct trilogy_ctx *ctx, trilogy_handshake_t *handshake, struct nogvl_sock_args args = {.rc = 0, .sock = sock}; + // Attempt to resolve a non-numeric hostname using the fiber scheduler if possible. +#ifdef TRILOGY_RB_IO_WAIT + if (opts->hostname != NULL) { + VALUE scheduler = rb_fiber_scheduler_current(); + + if (scheduler != Qnil) { + VALUE addresses = rb_fiber_scheduler_address_resolve(scheduler, rb_str_new_cstr(opts->hostname)); + + if (RARRAY_LEN(addresses) == 0) { + return TRILOGY_DNS_ERR; + } + + free(opts->hostname); + opts->hostname = NULL; + VALUE address = rb_ary_entry(addresses, 0); + StringValue(address); + + opts->hostname = strndup(RSTRING_PTR(address), RSTRING_LEN(address)); + } + } +#endif + // Do the DNS resolving with the GVL unlocked. At this point all // configuration data is copied and available to the trilogy socket. rb_thread_call_without_gvl(no_gvl_resolve, (void *)&args, RUBY_UBF_IO, NULL);