Skip to content

Commit

Permalink
Resolver: factored out setting a timer for resolver timeout.
Browse files Browse the repository at this point in the history
No functional changes.
  • Loading branch information
pluknet committed Jul 4, 2017
1 parent d0071f0 commit 225a5c7
Showing 1 changed file with 33 additions and 50 deletions.
83 changes: 33 additions & 50 deletions src/core/ngx_resolver.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ static void ngx_resolver_rbtree_insert_value(ngx_rbtree_node_t *temp,
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
static ngx_int_t ngx_resolver_copy(ngx_resolver_t *r, ngx_str_t *name,
u_char *buf, u_char *src, u_char *last);
static ngx_int_t ngx_resolver_set_timeout(ngx_resolver_t *r,
ngx_resolver_ctx_t *ctx);
static void ngx_resolver_timeout_handler(ngx_event_t *ev);
static void ngx_resolver_free_node(ngx_resolver_t *r, ngx_resolver_node_t *rn);
static void *ngx_resolver_alloc(ngx_resolver_t *r, size_t size);
Expand Down Expand Up @@ -728,19 +730,8 @@ ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx,
}

if (rn->waiting) {

if (ctx->event == NULL && ctx->timeout) {
ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
if (ctx->event == NULL) {
return NGX_ERROR;
}

ctx->event->handler = ngx_resolver_timeout_handler;
ctx->event->data = ctx;
ctx->event->log = r->log;
ctx->ident = -1;

ngx_add_timer(ctx->event, ctx->timeout);
if (ngx_resolver_set_timeout(r, ctx) != NGX_OK) {
return NGX_ERROR;
}

last->next = rn->waiting;
Expand Down Expand Up @@ -864,18 +855,8 @@ ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx,
goto failed;
}

if (ctx->event == NULL && ctx->timeout) {
ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
if (ctx->event == NULL) {
goto failed;
}

ctx->event->handler = ngx_resolver_timeout_handler;
ctx->event->data = ctx;
ctx->event->log = r->log;
ctx->ident = -1;

ngx_add_timer(ctx->event, ctx->timeout);
if (ngx_resolver_set_timeout(r, ctx) != NGX_OK) {
goto failed;
}

if (ngx_resolver_resend_empty(r)) {
Expand Down Expand Up @@ -1007,19 +988,8 @@ ngx_resolve_addr(ngx_resolver_ctx_t *ctx)
}

if (rn->waiting) {

if (ctx->event == NULL && ctx->timeout) {
ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
if (ctx->event == NULL) {
return NGX_ERROR;
}

ctx->event->handler = ngx_resolver_timeout_handler;
ctx->event->data = ctx;
ctx->event->log = r->log;
ctx->ident = -1;

ngx_add_timer(ctx->event, ctx->timeout);
if (ngx_resolver_set_timeout(r, ctx) != NGX_OK) {
return NGX_ERROR;
}

ctx->next = rn->waiting;
Expand Down Expand Up @@ -1089,18 +1059,8 @@ ngx_resolve_addr(ngx_resolver_ctx_t *ctx)
goto failed;
}

if (ctx->event == NULL && ctx->timeout) {
ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
if (ctx->event == NULL) {
goto failed;
}

ctx->event->handler = ngx_resolver_timeout_handler;
ctx->event->data = ctx;
ctx->event->log = r->log;
ctx->ident = -1;

ngx_add_timer(ctx->event, ctx->timeout);
if (ngx_resolver_set_timeout(r, ctx) != NGX_OK) {
goto failed;
}

if (ngx_resolver_resend_empty(r)) {
Expand Down Expand Up @@ -4033,6 +3993,29 @@ ngx_resolver_copy(ngx_resolver_t *r, ngx_str_t *name, u_char *buf, u_char *src,
}


static ngx_int_t
ngx_resolver_set_timeout(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx)
{
if (ctx->event || ctx->timeout == 0) {
return NGX_OK;
}

ctx->event = ngx_resolver_calloc(r, sizeof(ngx_event_t));
if (ctx->event == NULL) {
return NGX_ERROR;
}

ctx->event->handler = ngx_resolver_timeout_handler;
ctx->event->data = ctx;
ctx->event->log = r->log;
ctx->ident = -1;

ngx_add_timer(ctx->event, ctx->timeout);

return NGX_OK;
}


static void
ngx_resolver_timeout_handler(ngx_event_t *ev)
{
Expand Down

0 comments on commit 225a5c7

Please sign in to comment.