From 791d0697daabaa7739659119cec3e1c7e46b0320 Mon Sep 17 00:00:00 2001 From: drawing Date: Mon, 13 Nov 2023 17:41:20 +0800 Subject: [PATCH] add proxy_sndbuf_size and proxy_rcvbuf_size --- auto/modules | 1 + src/event/ngx_event_connect.c | 11 +++++++ src/event/ngx_event_connect.h | 3 ++ src/http/modules/ngx_http_fastcgi_module.c | 35 ++++++++++++++++++++++ src/http/modules/ngx_http_proxy_module.c | 35 ++++++++++++++++++++++ 5 files changed, 85 insertions(+) diff --git a/auto/modules b/auto/modules index c9942dbaab..561dc49219 100644 --- a/auto/modules +++ b/auto/modules @@ -1485,3 +1485,4 @@ have=T_HTTP_HEADER . auto/have have=T_HTTP_UPSTREAM_TIMEOUT_VAR . auto/have have=T_NGX_HTTPS_ALLOW_HTTP . auto/have have=T_NGX_REQUEST_START_TIME . auto/have +have=T_NGX_SOCKET_BUFFER . auto/have diff --git a/src/event/ngx_event_connect.c b/src/event/ngx_event_connect.c index 668084a7bb..ee2507a4c5 100644 --- a/src/event/ngx_event_connect.c +++ b/src/event/ngx_event_connect.c @@ -72,6 +72,17 @@ ngx_event_connect_peer(ngx_peer_connection_t *pc) goto failed; } } +#if (T_NGX_SOCKET_BUFFER) + if (pc->sndbuf) { + if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, + (const void *) &pc->sndbuf, sizeof(int)) == -1) + { + ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno, + "setsockopt(SO_SNDBUF) failed"); + goto failed; + } + } +#endif if (pc->so_keepalive) { value = 1; diff --git a/src/event/ngx_event_connect.h b/src/event/ngx_event_connect.h index 5d97b1b648..6fa7a268c5 100644 --- a/src/event/ngx_event_connect.h +++ b/src/event/ngx_event_connect.h @@ -60,6 +60,9 @@ struct ngx_peer_connection_s { int type; int rcvbuf; +#if (T_NGX_SOCKET_BUFFER) + int sndbuf; +#endif ngx_log_t *log; diff --git a/src/http/modules/ngx_http_fastcgi_module.c b/src/http/modules/ngx_http_fastcgi_module.c index 31a8715ad2..d05a508bfe 100644 --- a/src/http/modules/ngx_http_fastcgi_module.c +++ b/src/http/modules/ngx_http_fastcgi_module.c @@ -42,6 +42,11 @@ typedef struct { ngx_flag_t keep_conn; +#if (T_NGX_SOCKET_BUFFER) + size_t sndbuf; + size_t rcvbuf; +#endif + #if (NGX_HTTP_CACHE) ngx_http_complex_value_t cache_key; #endif @@ -582,6 +587,22 @@ static ngx_command_t ngx_http_fastcgi_commands[] = { offsetof(ngx_http_fastcgi_loc_conf_t, keep_conn), NULL }, +#if (T_NGX_SOCKET_BUFFER) + { ngx_string("fastcgi_sndbuf_size"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_conf_set_size_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_fastcgi_loc_conf_t, sndbuf), + NULL }, + + { ngx_string("fastcgi_rcvbuf_size"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_conf_set_size_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_fastcgi_loc_conf_t, rcvbuf), + NULL }, +#endif + ngx_null_command }; @@ -737,6 +758,11 @@ ngx_http_fastcgi_handler(ngx_http_request_t *r) u->finalize_request = ngx_http_fastcgi_finalize_request; r->state = 0; +#if (T_NGX_SOCKET_BUFFER) + u->peer.sndbuf = flcf->sndbuf; + u->peer.rcvbuf = flcf->rcvbuf; +#endif + u->buffering = flcf->upstream.buffering; u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t)); @@ -2943,6 +2969,11 @@ ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf) ngx_str_set(&conf->upstream.module, "fastcgi"); +#if (T_NGX_SOCKET_BUFFER) + conf->sndbuf = NGX_CONF_UNSET_SIZE; + conf->rcvbuf = NGX_CONF_UNSET_SIZE; +#endif + return conf; } @@ -3024,6 +3055,10 @@ ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_size_value(conf->upstream.limit_rate, prev->upstream.limit_rate, 0); +#if (T_NGX_SOCKET_BUFFER) + ngx_conf_merge_size_value(conf->sndbuf, prev->sndbuf, (size_t) 0); + ngx_conf_merge_size_value(conf->rcvbuf, prev->rcvbuf, (size_t) 0); +#endif ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs, 8, ngx_pagesize); diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c index 35e7b7b788..2992b38282 100644 --- a/src/http/modules/ngx_http_proxy_module.c +++ b/src/http/modules/ngx_http_proxy_module.c @@ -111,6 +111,10 @@ typedef struct { ngx_http_proxy_vars_t vars; ngx_flag_t redirect; +#if (T_NGX_SOCKET_BUFFER) + size_t sndbuf; + size_t rcvbuf; +#endif ngx_uint_t http_version; @@ -780,6 +784,22 @@ static ngx_command_t ngx_http_proxy_commands[] = { 0, NULL }, +#if (T_NGX_SOCKET_BUFFER) + { ngx_string("proxy_sndbuf_size"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_conf_set_size_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_proxy_loc_conf_t, sndbuf), + NULL }, + + { ngx_string("proxy_rcvbuf_size"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_conf_set_size_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_proxy_loc_conf_t, rcvbuf), + NULL }, +#endif + { ngx_string("proxy_ssl_conf_command"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, ngx_conf_set_keyval_slot, @@ -1043,6 +1063,11 @@ ngx_http_proxy_handler(ngx_http_request_t *r) u->buffering = plcf->upstream.buffering; +#if (T_NGX_SOCKET_BUFFER) + u->peer.sndbuf = plcf->sndbuf; + u->peer.rcvbuf = plcf->rcvbuf; +#endif + u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t)); if (u->pipe == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; @@ -3484,6 +3509,11 @@ ngx_http_proxy_create_loc_conf(ngx_conf_t *cf) ngx_str_set(&conf->upstream.module, "proxy"); +#if (T_NGX_SOCKET_BUFFER) + conf->sndbuf = NGX_CONF_UNSET_SIZE; + conf->rcvbuf = NGX_CONF_UNSET_SIZE; +#endif + return conf; } @@ -3568,6 +3598,11 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_size_value(conf->upstream.limit_rate, prev->upstream.limit_rate, 0); +#if (T_NGX_SOCKET_BUFFER) + ngx_conf_merge_size_value(conf->sndbuf, prev->sndbuf, (size_t) 0); + ngx_conf_merge_size_value(conf->rcvbuf, prev->rcvbuf, (size_t) 0); +#endif + ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs, 8, ngx_pagesize);