[njs] Fetch: refactored out ngx_js_http_resolve().
noreply at nginx.com
noreply at nginx.com
Fri Apr 11 15:29:02 UTC 2025
details: https://github.com/nginx/njs/commit/dbf556f235af1f42a7d9e3b90682de090170cd0f
branches: master
commit: dbf556f235af1f42a7d9e3b90682de090170cd0f
user: Zhidao HONG <z.hong at f5.com>
date: Fri, 11 Apr 2025 22:43:31 +0800
description:
Fetch: refactored out ngx_js_http_resolve().
---
nginx/ngx_js_fetch.c | 59 ++++++++++++++++++++++++++++++++++++----------------
1 file changed, 41 insertions(+), 18 deletions(-)
diff --git a/nginx/ngx_js_fetch.c b/nginx/ngx_js_fetch.c
index 944ed28e..9539a687 100644
--- a/nginx/ngx_js_fetch.c
+++ b/nginx/ngx_js_fetch.c
@@ -166,7 +166,9 @@ static ngx_js_http_t *ngx_js_http_alloc(njs_vm_t *vm, ngx_pool_t *pool,
static void ngx_js_http_resolve_done(ngx_js_http_t *http);
static void ngx_js_http_close_peer(ngx_js_http_t *http);
static void ngx_js_http_destructor(ngx_js_event_t *event);
-static void ngx_js_resolve_handler(ngx_resolver_ctx_t *ctx);
+static ngx_resolver_ctx_t *ngx_js_http_resolve(ngx_js_http_t *http,
+ ngx_resolver_t *r, ngx_str_t *host, in_port_t port, ngx_msec_t timeout);
+static void ngx_js_http_resolve_handler(ngx_resolver_ctx_t *ctx);
static njs_int_t ngx_js_fetch_promissified_result(njs_vm_t *vm,
njs_value_t *result, njs_int_t rc, njs_value_t *retval);
static void ngx_js_http_fetch_done(ngx_js_http_t *http,
@@ -832,7 +834,9 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
}
if (u.addrs == NULL) {
- ctx = ngx_resolve_start(ngx_external_resolver(vm, external), NULL);
+ ctx = ngx_js_http_resolve(http, ngx_external_resolver(vm, external),
+ &u.host, u.port,
+ ngx_external_resolver_timeout(vm, external));
if (ctx == NULL) {
njs_vm_memory_error(vm);
return NJS_ERROR;
@@ -843,21 +847,6 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
goto fail;
}
- http->ctx = ctx;
- http->port = u.port;
-
- ctx->name = u.host;
- ctx->handler = ngx_js_resolve_handler;
- ctx->data = http;
- ctx->timeout = ngx_external_resolver_timeout(vm, external);
-
- ret = ngx_resolve_name(http->ctx);
- if (ret != NGX_OK) {
- http->ctx = NULL;
- njs_vm_memory_error(vm);
- return NJS_ERROR;
- }
-
njs_value_assign(retval, njs_value_arg(&http->promise));
return NJS_OK;
@@ -1341,8 +1330,42 @@ ngx_js_http_error(ngx_js_http_t *http, const char *fmt, ...)
}
+static ngx_resolver_ctx_t *
+ngx_js_http_resolve(ngx_js_http_t *http, ngx_resolver_t *r, ngx_str_t *host,
+ in_port_t port, ngx_msec_t timeout)
+{
+ ngx_int_t ret;
+ ngx_resolver_ctx_t *ctx;
+
+ ctx = ngx_resolve_start(r, NULL);
+ if (ctx == NULL) {
+ return NULL;
+ }
+
+ if (ctx == NGX_NO_RESOLVER) {
+ return ctx;
+ }
+
+ http->ctx = ctx;
+ http->port = port;
+
+ ctx->name = *host;
+ ctx->handler = ngx_js_http_resolve_handler;
+ ctx->data = http;
+ ctx->timeout = timeout;
+
+ ret = ngx_resolve_name(ctx);
+ if (ret != NGX_OK) {
+ http->ctx = NULL;
+ return NULL;
+ }
+
+ return ctx;
+}
+
+
static void
-ngx_js_resolve_handler(ngx_resolver_ctx_t *ctx)
+ngx_js_http_resolve_handler(ngx_resolver_ctx_t *ctx)
{
u_char *p;
size_t len;
More information about the nginx-devel
mailing list