comparison src/http/ngx_http_upstream.c @ 7007:ed1101bbf19f

Introduced ngx_tcp_nodelay().
author Ruslan Ermilov <ru@nginx.com>
date Fri, 26 May 2017 22:52:48 +0300
parents 9552758a786e
children 610a219b28f6
comparison
equal deleted inserted replaced
7006:9552758a786e 7007:ed1101bbf19f
1604 1604
1605 static void 1605 static void
1606 ngx_http_upstream_ssl_init_connection(ngx_http_request_t *r, 1606 ngx_http_upstream_ssl_init_connection(ngx_http_request_t *r,
1607 ngx_http_upstream_t *u, ngx_connection_t *c) 1607 ngx_http_upstream_t *u, ngx_connection_t *c)
1608 { 1608 {
1609 int tcp_nodelay;
1610 ngx_int_t rc; 1609 ngx_int_t rc;
1611 ngx_http_core_loc_conf_t *clcf; 1610 ngx_http_core_loc_conf_t *clcf;
1612 1611
1613 if (ngx_http_upstream_test_connect(c) != NGX_OK) { 1612 if (ngx_http_upstream_test_connect(c) != NGX_OK) {
1614 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); 1613 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
1644 1643
1645 /* abbreviated SSL handshake may interact badly with Nagle */ 1644 /* abbreviated SSL handshake may interact badly with Nagle */
1646 1645
1647 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); 1646 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1648 1647
1649 if (clcf->tcp_nodelay && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) { 1648 if (clcf->tcp_nodelay && ngx_tcp_nodelay(c) != NGX_OK) {
1650 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay"); 1649 ngx_http_upstream_finalize_request(r, u,
1651
1652 tcp_nodelay = 1;
1653
1654 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
1655 (const void *) &tcp_nodelay, sizeof(int)) == -1)
1656 {
1657 ngx_connection_error(c, ngx_socket_errno,
1658 "setsockopt(TCP_NODELAY) failed");
1659 ngx_http_upstream_finalize_request(r, u,
1660 NGX_HTTP_INTERNAL_SERVER_ERROR); 1650 NGX_HTTP_INTERNAL_SERVER_ERROR);
1661 return; 1651 return;
1662 }
1663
1664 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
1665 } 1652 }
1666 } 1653 }
1667 1654
1668 r->connection->log->action = "SSL handshaking to upstream"; 1655 r->connection->log->action = "SSL handshaking to upstream";
1669 1656
2012 1999
2013 static ngx_int_t 2000 static ngx_int_t
2014 ngx_http_upstream_send_request_body(ngx_http_request_t *r, 2001 ngx_http_upstream_send_request_body(ngx_http_request_t *r,
2015 ngx_http_upstream_t *u, ngx_uint_t do_write) 2002 ngx_http_upstream_t *u, ngx_uint_t do_write)
2016 { 2003 {
2017 int tcp_nodelay;
2018 ngx_int_t rc; 2004 ngx_int_t rc;
2019 ngx_chain_t *out, *cl, *ln; 2005 ngx_chain_t *out, *cl, *ln;
2020 ngx_connection_t *c; 2006 ngx_connection_t *c;
2021 ngx_http_core_loc_conf_t *clcf; 2007 ngx_http_core_loc_conf_t *clcf;
2022 2008
2049 } 2035 }
2050 2036
2051 c = u->peer.connection; 2037 c = u->peer.connection;
2052 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); 2038 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2053 2039
2054 if (clcf->tcp_nodelay && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) { 2040 if (clcf->tcp_nodelay && ngx_tcp_nodelay(c) != NGX_OK) {
2055 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay"); 2041 return NGX_ERROR;
2056
2057 tcp_nodelay = 1;
2058
2059 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
2060 (const void *) &tcp_nodelay, sizeof(int)) == -1)
2061 {
2062 ngx_connection_error(c, ngx_socket_errno,
2063 "setsockopt(TCP_NODELAY) failed");
2064 return NGX_ERROR;
2065 }
2066
2067 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
2068 } 2042 }
2069 2043
2070 r->read_event_handler = ngx_http_upstream_read_request_handler; 2044 r->read_event_handler = ngx_http_upstream_read_request_handler;
2071 2045
2072 } else { 2046 } else {
2820 2794
2821 2795
2822 static void 2796 static void
2823 ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u) 2797 ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u)
2824 { 2798 {
2825 int tcp_nodelay;
2826 ssize_t n; 2799 ssize_t n;
2827 ngx_int_t rc; 2800 ngx_int_t rc;
2828 ngx_event_pipe_t *p; 2801 ngx_event_pipe_t *p;
2829 ngx_connection_t *c; 2802 ngx_connection_t *c;
2830 ngx_http_core_loc_conf_t *clcf; 2803 ngx_http_core_loc_conf_t *clcf;
2901 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) { 2874 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {
2902 ngx_http_upstream_finalize_request(r, u, NGX_ERROR); 2875 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
2903 return; 2876 return;
2904 } 2877 }
2905 2878
2906 if (clcf->tcp_nodelay && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) { 2879 if (clcf->tcp_nodelay && ngx_tcp_nodelay(c) != NGX_OK) {
2907 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay"); 2880 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
2908 2881 return;
2909 tcp_nodelay = 1;
2910
2911 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
2912 (const void *) &tcp_nodelay, sizeof(int)) == -1)
2913 {
2914 ngx_connection_error(c, ngx_socket_errno,
2915 "setsockopt(TCP_NODELAY) failed");
2916 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
2917 return;
2918 }
2919
2920 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
2921 } 2882 }
2922 2883
2923 n = u->buffer.last - u->buffer.pos; 2884 n = u->buffer.last - u->buffer.pos;
2924 2885
2925 if (n) { 2886 if (n) {
3174 3135
3175 3136
3176 static void 3137 static void
3177 ngx_http_upstream_upgrade(ngx_http_request_t *r, ngx_http_upstream_t *u) 3138 ngx_http_upstream_upgrade(ngx_http_request_t *r, ngx_http_upstream_t *u)
3178 { 3139 {
3179 int tcp_nodelay;
3180 ngx_connection_t *c; 3140 ngx_connection_t *c;
3181 ngx_http_core_loc_conf_t *clcf; 3141 ngx_http_core_loc_conf_t *clcf;
3182 3142
3183 c = r->connection; 3143 c = r->connection;
3184 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); 3144 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
3192 u->write_event_handler = ngx_http_upstream_upgraded_write_upstream; 3152 u->write_event_handler = ngx_http_upstream_upgraded_write_upstream;
3193 r->read_event_handler = ngx_http_upstream_upgraded_read_downstream; 3153 r->read_event_handler = ngx_http_upstream_upgraded_read_downstream;
3194 r->write_event_handler = ngx_http_upstream_upgraded_write_downstream; 3154 r->write_event_handler = ngx_http_upstream_upgraded_write_downstream;
3195 3155
3196 if (clcf->tcp_nodelay) { 3156 if (clcf->tcp_nodelay) {
3197 tcp_nodelay = 1; 3157
3198 3158 if (ngx_tcp_nodelay(c) != NGX_OK) {
3199 if (c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) { 3159 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
3200 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay"); 3160 return;
3201 3161 }
3202 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, 3162
3203 (const void *) &tcp_nodelay, sizeof(int)) == -1) 3163 if (ngx_tcp_nodelay(u->peer.connection) != NGX_OK) {
3204 { 3164 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
3205 ngx_connection_error(c, ngx_socket_errno, 3165 return;
3206 "setsockopt(TCP_NODELAY) failed");
3207 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
3208 return;
3209 }
3210
3211 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
3212 }
3213
3214 if (u->peer.connection->tcp_nodelay == NGX_TCP_NODELAY_UNSET) {
3215 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, u->peer.connection->log, 0,
3216 "tcp_nodelay");
3217
3218 if (setsockopt(u->peer.connection->fd, IPPROTO_TCP, TCP_NODELAY,
3219 (const void *) &tcp_nodelay, sizeof(int)) == -1)
3220 {
3221 ngx_connection_error(u->peer.connection, ngx_socket_errno,
3222 "setsockopt(TCP_NODELAY) failed");
3223 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
3224 return;
3225 }
3226
3227 u->peer.connection->tcp_nodelay = NGX_TCP_NODELAY_SET;
3228 } 3166 }
3229 } 3167 }
3230 3168
3231 if (ngx_http_send_special(r, NGX_HTTP_FLUSH) == NGX_ERROR) { 3169 if (ngx_http_send_special(r, NGX_HTTP_FLUSH) == NGX_ERROR) {
3232 ngx_http_upstream_finalize_request(r, u, NGX_ERROR); 3170 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);