From d8cf2c9e11a9cbf355f74c1769442aa559f57ecc Mon Sep 17 00:00:00 2001 From: Radkesvat <134321679+radkesvat@users.noreply.github.com> Date: Wed, 19 Jun 2024 15:22:43 +0000 Subject: [PATCH] http2 tweaks --- tunnels/client/http2/helpers.h | 10 +++++----- tunnels/client/http2/http2_client.c | 25 ++++++++++++++++++++----- tunnels/server/http2/helpers.h | 25 ++++++++++++------------- tunnels/server/http2/http2_server.c | 9 +++++++-- tunnels/server/http2/types.h | 11 +++++------ 5 files changed, 49 insertions(+), 31 deletions(-) diff --git a/tunnels/client/http2/helpers.h b/tunnels/client/http2/helpers.h index 83e79ec7..b7283eea 100644 --- a/tunnels/client/http2/helpers.h +++ b/tunnels/client/http2/helpers.h @@ -44,22 +44,22 @@ static void onStreamLineResumed(void *arg) static void onH2LinePaused(void *arg) { http2_client_con_state_t *con = (http2_client_con_state_t *) arg; - ++(con->pause_counter); - if (con->pause_counter > 8) - { + // ++(con->pause_counter); + // if (con->pause_counter > 8) + // { http2_client_child_con_state_t *stream_i; for (stream_i = con->root.next; stream_i;) { pauseLineDownSide(stream_i->line); stream_i = stream_i->next; } - } + // } } static void onH2LineResumed(void *arg) { http2_client_con_state_t *con = (http2_client_con_state_t *) arg; - con->pause_counter = con->pause_counter > 0 ? (con->pause_counter - 1) : con->pause_counter; + // con->pause_counter = con->pause_counter > 0 ? (con->pause_counter - 1) : con->pause_counter; http2_client_child_con_state_t *stream_i; for (stream_i = con->root.next; stream_i;) { diff --git a/tunnels/client/http2/http2_client.c b/tunnels/client/http2/http2_client.c index 31c6083e..62e8c39c 100644 --- a/tunnels/client/http2/http2_client.c +++ b/tunnels/client/http2/http2_client.c @@ -300,11 +300,12 @@ static int onFrameRecvCallback(nghttp2_session *session, const nghttp2_frame *fr { return 0; } + // http2_client_state_t *state = STATE(self); resumeLineUpSide(stream->parent); nghttp2_session_set_stream_user_data(con->session, stream->stream_id, NULL); - context_t *fc = newFinContext(stream->line); + context_t *fc = newFinContext(stream->line); CSTATE_DROP(fc); - tunnel_t *dest = stream->tunnel->dw; + tunnel_t *dest = stream->tunnel->dw; removeStream(con, stream); deleteHttp2Stream(stream); dest->downStream(dest, fc); @@ -397,11 +398,18 @@ static void upStream(tunnel_t *self, context_t *c) if (con->content_type == kApplicationGrpc && con->handshake_completed) { sendGrpcFinalData(self, con->line, stream->stream_id); + if (! isAlive(c->line)) + { + destroyContext(c); + return; + } } resumeLineUpSide(con->line); nghttp2_session_set_stream_user_data(con->session, stream->stream_id, NULL); removeStream(con, stream); + deleteHttp2Stream(stream); + if (con->root.next == NULL && con->childs_added >= state->concurrency && isAlive(c->line)) { context_t *con_fc = newFinContext(con->line); @@ -409,8 +417,7 @@ static void upStream(tunnel_t *self, context_t *c) deleteHttp2Connection(con); con_dest->upStream(con_dest, con_fc); } - - deleteHttp2Stream(stream); + destroyContext(c); return; } @@ -419,7 +426,8 @@ static void upStream(tunnel_t *self, context_t *c) static void downStream(tunnel_t *self, context_t *c) { - http2_client_con_state_t *con = CSTATE(c); + http2_client_state_t *state = STATE(self); + http2_client_con_state_t *con = CSTATE(c); if (c->payload != NULL) { size_t len = 0; @@ -471,6 +479,13 @@ static void downStream(tunnel_t *self, context_t *c) } } + if (con->root.next == NULL && con->childs_added >= state->concurrency) + { + context_t *con_fc = newFinContext(con->line); + deleteHttp2Connection(con); + self->up->upStream(self->up, con_fc); + } + reuseContextBuffer(c); destroyContext(c); } diff --git a/tunnels/server/http2/helpers.h b/tunnels/server/http2/helpers.h index 83f47634..8df49cc5 100644 --- a/tunnels/server/http2/helpers.h +++ b/tunnels/server/http2/helpers.h @@ -58,16 +58,15 @@ static void onH2LinePaused(void *arg) { http2_server_con_state_t *con = (http2_server_con_state_t *) arg; - ++(con->pause_counter); if (con->pause_counter > 16) { - http2_server_child_con_state_t *stream_i; - for (stream_i = con->root.next; stream_i;) - { - pauseLineUpSide(stream_i->line); - stream_i = stream_i->next; - } + http2_server_child_con_state_t *stream_i; + for (stream_i = con->root.next; stream_i;) + { + pauseLineUpSide(stream_i->line); + stream_i = stream_i->next; + } } } @@ -84,18 +83,18 @@ static void onH2LineResumed(void *arg) } http2_server_child_con_state_t *createHttp2Stream(http2_server_con_state_t *con, line_t *this_line, tunnel_t *self, - int32_t stream_id) + int32_t stream_id) { http2_server_child_con_state_t *stream; stream = malloc(sizeof(http2_server_child_con_state_t)); memset(stream, 0, sizeof(http2_server_child_con_state_t)); - stream->stream_id = stream_id; - stream->chunkbs = newBufferStream(getLineBufferPool(this_line)); - stream->parent = this_line; - stream->line = newLine(this_line->tid); + stream->stream_id = stream_id; + stream->chunkbs = newBufferStream(getLineBufferPool(this_line)); + stream->parent = this_line; + stream->line = newLine(this_line->tid); LSTATE_MUT(stream->line) = stream; - stream->tunnel = self; + stream->tunnel = self; nghttp2_session_set_stream_user_data(con->session, stream_id, stream); setupLineDownSide(stream->line, onStreamLinePaused, stream, onStreamLineResumed); diff --git a/tunnels/server/http2/http2_server.c b/tunnels/server/http2/http2_server.c index 1440a45c..548e9511 100644 --- a/tunnels/server/http2/http2_server.c +++ b/tunnels/server/http2/http2_server.c @@ -189,7 +189,7 @@ static int onFrameRecvCallback(nghttp2_session *session, const nghttp2_frame *fr tunnel_t *dest = stream->tunnel->up; CSTATE_DROP(fc); removeStream(con, stream); - deleteHttp2Stream(stream); + deleteHttp2Stream(stream); dest->upStream(dest, fc); return 0; } @@ -353,7 +353,7 @@ static void upStream(tunnel_t *self, context_t *c) return; } } - + reuseContextBuffer(c); destroyContext(c); } @@ -423,6 +423,11 @@ static void downStream(tunnel_t *self, context_t *c) return; } } + if (! isAlive(c->line)) + { + destroyContext(c); + return; + } if (nghttp2_session_want_read(con->session) == 0 && nghttp2_session_want_write(con->session) == 0) { diff --git a/tunnels/server/http2/types.h b/tunnels/server/http2/types.h index 64e0913f..bf134039 100644 --- a/tunnels/server/http2/types.h +++ b/tunnels/server/http2/types.h @@ -30,12 +30,11 @@ typedef struct http2_server_child_con_state_s char *request_path; int32_t stream_id; bool first_sent; - - buffer_stream_t *chunkbs; // used for grpc - size_t bytes_needed; - line_t *parent; - line_t *line; - tunnel_t *tunnel; + buffer_stream_t *chunkbs; // used for grpc + size_t bytes_needed; + line_t *parent; + line_t *line; + tunnel_t *tunnel; } http2_server_child_con_state_t; typedef struct http2_server_con_state_s