Skip to content

Commit

Permalink
Merge pull request #1892 from drawing/master
Browse files Browse the repository at this point in the history
set socket buffer when proxy request
  • Loading branch information
lianglli authored Nov 28, 2023
2 parents 8a7529d + 4e43d0f commit 12a0cd0
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions auto/modules
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions src/event/ngx_event_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/event/ngx_event_connect.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
35 changes: 35 additions & 0 deletions src/http/modules/ngx_http_fastcgi_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
};

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions src/http/modules/ngx_http_proxy_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 12a0cd0

Please sign in to comment.