Skip to content

Commit c33284d

Browse files
committed
Fetch: added keepalive support for ngx.fetch() API.
This closes #957 feature request on Github.
1 parent 0f9e2c6 commit c33284d

11 files changed

+1309
-70
lines changed

nginx/ngx_http_js_module.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,34 @@ static ngx_command_t ngx_http_js_commands[] = {
565565
0,
566566
NULL },
567567

568+
{ ngx_string("js_fetch_keepalive"),
569+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
570+
ngx_conf_set_num_slot,
571+
NGX_HTTP_LOC_CONF_OFFSET,
572+
offsetof(ngx_http_js_loc_conf_t, fetch_keepalive),
573+
NULL },
574+
575+
{ ngx_string("js_fetch_keepalive_requests"),
576+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
577+
ngx_conf_set_num_slot,
578+
NGX_HTTP_LOC_CONF_OFFSET,
579+
offsetof(ngx_http_js_loc_conf_t, fetch_keepalive_requests),
580+
NULL },
581+
582+
{ ngx_string("js_fetch_keepalive_time"),
583+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
584+
ngx_conf_set_msec_slot,
585+
NGX_HTTP_LOC_CONF_OFFSET,
586+
offsetof(ngx_http_js_loc_conf_t, fetch_keepalive_time),
587+
NULL },
588+
589+
{ ngx_string("js_fetch_keepalive_timeout"),
590+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
591+
ngx_conf_set_msec_slot,
592+
NGX_HTTP_LOC_CONF_OFFSET,
593+
offsetof(ngx_http_js_loc_conf_t, fetch_keepalive_timeout),
594+
NULL },
595+
568596
ngx_null_command
569597
};
570598

nginx/ngx_js.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <ngx_core.h>
1111
#include <math.h>
1212
#include "ngx_js.h"
13+
#include "ngx_js_http.h"
1314

1415

1516
typedef struct {
@@ -3986,6 +3987,11 @@ ngx_js_create_conf(ngx_conf_t *cf, size_t size)
39863987
conf->max_response_body_size = NGX_CONF_UNSET_SIZE;
39873988
conf->timeout = NGX_CONF_UNSET_MSEC;
39883989

3990+
conf->fetch_keepalive = NGX_CONF_UNSET_UINT;
3991+
conf->fetch_keepalive_requests = NGX_CONF_UNSET_UINT;
3992+
conf->fetch_keepalive_time = NGX_CONF_UNSET_MSEC;
3993+
conf->fetch_keepalive_timeout = NGX_CONF_UNSET_MSEC;
3994+
39893995
return conf;
39903996
}
39913997

@@ -4097,6 +4103,17 @@ ngx_js_merge_conf(ngx_conf_t *cf, void *parent, void *child,
40974103
ngx_conf_merge_size_value(conf->max_response_body_size,
40984104
prev->max_response_body_size, 1048576);
40994105

4106+
ngx_conf_merge_uint_value(conf->fetch_keepalive, prev->fetch_keepalive, 0);
4107+
ngx_conf_merge_uint_value(conf->fetch_keepalive_requests,
4108+
prev->fetch_keepalive_requests, 1000);
4109+
ngx_conf_merge_msec_value(conf->fetch_keepalive_time,
4110+
prev->fetch_keepalive_time, 3600000);
4111+
ngx_conf_merge_msec_value(conf->fetch_keepalive_timeout,
4112+
prev->fetch_keepalive_timeout, 60000);
4113+
4114+
ngx_queue_init(&conf->fetch_keepalive_cache);
4115+
ngx_queue_init(&conf->fetch_keepalive_free);
4116+
41004117
if (ngx_js_merge_vm(cf, (ngx_js_loc_conf_t *) conf,
41014118
(ngx_js_loc_conf_t *) prev,
41024119
init_vm)

nginx/ngx_js.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <ngx_config.h>
1414
#include <ngx_core.h>
1515
#include <ngx_event.h>
16+
#include <ngx_event_connect.h>
1617
#include <njs.h>
1718
#include <njs_rbtree.h>
1819
#include <njs_arr.h>
@@ -133,7 +134,14 @@ typedef struct {
133134
\
134135
size_t buffer_size; \
135136
size_t max_response_body_size; \
136-
ngx_msec_t timeout
137+
ngx_msec_t timeout; \
138+
\
139+
ngx_uint_t fetch_keepalive; \
140+
ngx_uint_t fetch_keepalive_requests; \
141+
ngx_msec_t fetch_keepalive_time; \
142+
ngx_msec_t fetch_keepalive_timeout; \
143+
ngx_queue_t fetch_keepalive_cache; \
144+
ngx_queue_t fetch_keepalive_free
137145

138146

139147
#if defined(NGX_HTTP_SSL) || defined(NGX_STREAM_SSL)

nginx/ngx_js_fetch.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
*/
88

99

10-
#include <ngx_config.h>
11-
#include <ngx_core.h>
12-
#include <ngx_event.h>
13-
#include <ngx_event_connect.h>
1410
#include "ngx_js.h"
1511
#include "ngx_js_http.h"
1612

@@ -550,6 +546,13 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
550546
goto fail;
551547
}
552548

549+
if (u.host.len >= NGX_JS_HOST_MAX_LEN) {
550+
njs_vm_error(vm, "Host name too long");
551+
goto fail;
552+
}
553+
554+
http->host = u.host;
555+
http->port = u.port;
553556
http->response.url = request.url;
554557
http->buffer_size = http->conf->buffer_size;
555558
http->max_response_body_size = http->conf->max_response_body_size;
@@ -681,18 +684,22 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
681684
continue;
682685
}
683686

687+
if (h[i].key.len == 10
688+
&& ngx_strncasecmp(h[i].key.data, (u_char *) "Connection", 10)
689+
== 0)
690+
{
691+
continue;
692+
}
693+
684694
njs_chb_append(&http->chain, h[i].key.data, h[i].key.len);
685695
njs_chb_append_literal(&http->chain, ": ");
686696
njs_chb_append(&http->chain, h[i].value.data, h[i].value.len);
687697
njs_chb_append_literal(&http->chain, CRLF);
688698
}
689699

690-
njs_chb_append_literal(&http->chain, "Connection: close" CRLF);
691-
692-
#if (NGX_SSL)
693-
http->tls_name.data = u.host.data;
694-
http->tls_name.len = u.host.len;
695-
#endif
700+
if (!http->keepalive) {
701+
njs_chb_append_literal(&http->chain, "Connection: close" CRLF);
702+
}
696703

697704
if (request.body.len != 0) {
698705
njs_chb_sprintf(&http->chain, 32, "Content-Length: %uz" CRLF CRLF,
@@ -1154,7 +1161,8 @@ ngx_js_fetch_alloc(njs_vm_t *vm, ngx_pool_t *pool, ngx_log_t *log,
11541161
http->log = log;
11551162
http->conf = conf;
11561163

1157-
http->http_parse.content_length_n = -1;
1164+
http->content_length_n = -1;
1165+
http->keepalive = (conf->fetch_keepalive > 0);
11581166

11591167
http->append_headers = ngx_js_fetch_append_headers;
11601168
http->ready_handler = ngx_js_fetch_process_done;

0 commit comments

Comments
 (0)