HTTP/3: keepalive timeout.

This timeout limits the time when no client request streams exist.
This commit is contained in:
Roman Arutyunyan 2021-03-30 16:48:38 +03:00
parent 9533df5b72
commit f4ab680bcb
3 changed files with 85 additions and 0 deletions

View File

@ -74,6 +74,11 @@
#define NGX_HTTP_V3_ERR_DECODER_STREAM_ERROR 0x202
#define ngx_http_v3_get_module_loc_conf(c, module) \
ngx_http_get_module_loc_conf( \
((ngx_http_v3_connection_t *) c->quic->parent->data)->hc.conf_ctx, \
module)
#define ngx_http_v3_get_module_srv_conf(c, module) \
ngx_http_get_module_srv_conf( \
((ngx_http_v3_connection_t *) c->quic->parent->data)->hc.conf_ctx, \
@ -128,6 +133,9 @@ typedef struct {
ngx_http_connection_t hc;
ngx_http_v3_dynamic_table_t table;
ngx_event_t keepalive;
ngx_uint_t nrequests;
ngx_queue_t blocked;
ngx_uint_t nblocked;

View File

@ -10,6 +10,7 @@
#include <ngx_http.h>
static void ngx_http_v3_cleanup_request(void *data);
static void ngx_http_v3_process_request(ngx_event_t *rev);
static ngx_int_t ngx_http_v3_process_header(ngx_http_request_t *r,
ngx_str_t *name, ngx_str_t *value);
@ -55,8 +56,10 @@ ngx_http_v3_init(ngx_connection_t *c)
uint64_t n;
ngx_buf_t *b;
ngx_event_t *rev;
ngx_pool_cleanup_t *cln;
ngx_http_request_t *r;
ngx_http_connection_t *hc;
ngx_http_v3_connection_t *h3c;
ngx_http_core_loc_conf_t *clcf;
ngx_http_core_srv_conf_t *cscf;
@ -98,6 +101,22 @@ ngx_http_v3_init(ngx_connection_t *c)
"reached maximum number of requests");
}
cln = ngx_pool_cleanup_add(c->pool, 0);
if (cln == NULL) {
ngx_http_close_connection(c);
return;
}
cln->handler = ngx_http_v3_cleanup_request;
cln->data = c;
h3c = c->quic->parent->data;
h3c->nrequests++;
if (h3c->keepalive.timer_set) {
ngx_del_timer(&h3c->keepalive);
}
cscf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_core_module);
size = cscf->client_header_buffer_size;
@ -154,6 +173,23 @@ ngx_http_v3_init(ngx_connection_t *c)
}
static void
ngx_http_v3_cleanup_request(void *data)
{
ngx_connection_t *c = data;
ngx_http_core_loc_conf_t *clcf;
ngx_http_v3_connection_t *h3c;
h3c = c->quic->parent->data;
if (--h3c->nrequests == 0) {
clcf = ngx_http_v3_get_module_loc_conf(c, ngx_http_core_module);
ngx_add_timer(&h3c->keepalive, clcf->keepalive_timeout);
}
}
static void
ngx_http_v3_process_request(ngx_event_t *rev)
{

View File

@ -29,6 +29,8 @@ typedef struct {
} ngx_http_v3_push_t;
static void ngx_http_v3_keepalive_handler(ngx_event_t *ev);
static void ngx_http_v3_cleanup_session(void *data);
static void ngx_http_v3_close_uni_stream(ngx_connection_t *c);
static void ngx_http_v3_read_uni_stream_type(ngx_event_t *rev);
static void ngx_http_v3_uni_read_handler(ngx_event_t *rev);
@ -43,6 +45,7 @@ ngx_int_t
ngx_http_v3_init_session(ngx_connection_t *c)
{
ngx_connection_t *pc;
ngx_pool_cleanup_t *cln;
ngx_http_connection_t *phc;
ngx_http_v3_connection_t *h3c;
@ -67,12 +70,50 @@ ngx_http_v3_init_session(ngx_connection_t *c)
ngx_queue_init(&h3c->blocked);
ngx_queue_init(&h3c->pushing);
h3c->keepalive.log = pc->log;
h3c->keepalive.data = pc;
h3c->keepalive.handler = ngx_http_v3_keepalive_handler;
h3c->keepalive.cancelable = 1;
cln = ngx_pool_cleanup_add(pc->pool, 0);
if (cln == NULL) {
return NGX_ERROR;
}
cln->handler = ngx_http_v3_cleanup_session;
cln->data = h3c;
pc->data = h3c;
return ngx_http_v3_send_settings(c);
}
static void
ngx_http_v3_keepalive_handler(ngx_event_t *ev)
{
ngx_connection_t *c;
c = ev->data;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 keepalive handler");
ngx_quic_finalize_connection(c, NGX_HTTP_V3_ERR_NO_ERROR,
"keepalive timeout");
}
static void
ngx_http_v3_cleanup_session(void *data)
{
ngx_http_v3_connection_t *h3c = data;
if (h3c->keepalive.timer_set) {
ngx_del_timer(&h3c->keepalive);
}
}
void
ngx_http_v3_init_uni_stream(ngx_connection_t *c)
{