Skip to content

Commit 8a4328a

Browse files
committed
feature: added pure C API for setting upstream request connect/send/read timeouts in balancer_by_lua* on a per session basis. thanks Jianhao Dai for the original patch.
1 parent 347bb2f commit 8a4328a

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

src/ngx_http_lua_balancer.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ struct ngx_http_lua_balancer_peer_data_s {
3333
in_port_t port;
3434

3535
int last_peer_state;
36+
37+
#if !(HAVE_UPSTREAM_TIMEOUT_FIELDS)
38+
unsigned cloned_upstream_conf; /* :1 */
39+
#endif
3640
};
3741

3842

@@ -536,6 +540,101 @@ ngx_http_lua_ffi_balancer_set_current_peer(ngx_http_request_t *r,
536540
}
537541

538542

543+
int
544+
ngx_http_lua_ffi_balancer_set_timeouts(ngx_http_request_t *r,
545+
long connect_timeout, long send_timeout, long read_timeout,
546+
char **err)
547+
{
548+
ngx_http_lua_ctx_t *ctx;
549+
ngx_http_upstream_t *u;
550+
551+
#if !(HAVE_UPSTREAM_TIMEOUT_FIELDS)
552+
ngx_http_upstream_conf_t *ucf;
553+
#endif
554+
ngx_http_lua_main_conf_t *lmcf;
555+
ngx_http_lua_balancer_peer_data_t *bp;
556+
557+
if (r == NULL) {
558+
*err = "no request found";
559+
return NGX_ERROR;
560+
}
561+
562+
u = r->upstream;
563+
564+
if (u == NULL) {
565+
*err = "no upstream found";
566+
return NGX_ERROR;
567+
}
568+
569+
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
570+
if (ctx == NULL) {
571+
*err = "no ctx found";
572+
return NGX_ERROR;
573+
}
574+
575+
if ((ctx->context & NGX_HTTP_LUA_CONTEXT_BALANCER) == 0) {
576+
*err = "API disabled in the current context";
577+
return NGX_ERROR;
578+
}
579+
580+
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
581+
582+
bp = lmcf->balancer_peer_data;
583+
if (bp == NULL) {
584+
*err = "no upstream peer data found";
585+
return NGX_ERROR;
586+
}
587+
588+
#if !(HAVE_UPSTREAM_TIMEOUT_FIELDS)
589+
if (!bp->cloned_upstream_conf) {
590+
/* we clone the upstream conf for the current request so that
591+
* we do not affect other requests at all. */
592+
593+
ucf = ngx_palloc(r->pool, sizeof(ngx_http_upstream_conf_t));
594+
595+
if (ucf == NULL) {
596+
*err = "no memory";
597+
return NGX_ERROR;
598+
}
599+
600+
ngx_memcpy(ucf, u->conf, sizeof(ngx_http_upstream_conf_t));
601+
602+
u->conf = ucf;
603+
bp->cloned_upstream_conf = 1;
604+
605+
} else {
606+
ucf = u->conf;
607+
}
608+
#endif
609+
610+
if (connect_timeout > 0) {
611+
#if (HAVE_UPSTREAM_TIMEOUT_FIELDS)
612+
u->connect_timeout = (ngx_msec_t) connect_timeout;
613+
#else
614+
ucf->connect_timeout = (ngx_msec_t) connect_timeout;
615+
#endif
616+
}
617+
618+
if (send_timeout > 0) {
619+
#if (HAVE_UPSTREAM_TIMEOUT_FIELDS)
620+
u->send_timeout = (ngx_msec_t) send_timeout;
621+
#else
622+
ucf->send_timeout = (ngx_msec_t) send_timeout;
623+
#endif
624+
}
625+
626+
if (read_timeout > 0) {
627+
#if (HAVE_UPSTREAM_TIMEOUT_FIELDS)
628+
u->read_timeout = (ngx_msec_t) read_timeout;
629+
#else
630+
ucf->read_timeout = (ngx_msec_t) read_timeout;
631+
#endif
632+
}
633+
634+
return NGX_OK;
635+
}
636+
637+
539638
int
540639
ngx_http_lua_ffi_balancer_set_more_tries(ngx_http_request_t *r,
541640
int count, char **err)

0 commit comments

Comments
 (0)