proxy: Turn on TCP_CORK

Now that we're using NODELAY, we should definitely use cork around
writes to the upstream server. This prevents each partial write()
from being its own packet, which would be terrible if it actually
happened with any regularity (we'd mostly see it when the kernel
is stressed, and write() is progressing a few bytes at a time as
a result)
This commit is contained in:
nick
2014-02-25 16:00:48 +00:00
parent f9a3447bc9
commit 6f540ce238

View File

@@ -525,11 +525,22 @@ int proxy_write_to_upstream( struct proxier* proxy, int state )
ssize_t count; ssize_t count;
// assert( state == WRITE_TO_UPSTREAM ); // assert( state == WRITE_TO_UPSTREAM );
/* FIXME: We may set cork=1 multiple times as a result of this idiom.
* Not a serious problem, but we could do better
*/
if ( proxy->req.needle == 0 && AF_UNIX != proxy->connect_to.family ) {
if ( sock_set_tcp_cork( proxy->upstream_fd, 1 ) == -1 ) {
warn( SHOW_ERRNO( "Failed to set TCP_CORK" ) );
}
}
count = iobuf_write( proxy->upstream_fd, &proxy->req ); count = iobuf_write( proxy->upstream_fd, &proxy->req );
if ( count == -1 ) { if ( count == -1 ) {
warn( SHOW_ERRNO( "Failed to send request to upstream" ) ); warn( SHOW_ERRNO( "Failed to send request to upstream" ) );
proxy->req.needle = 0; proxy->req.needle = 0;
// We're throwing the socket away so no need to uncork
return CONNECT_TO_UPSTREAM; return CONNECT_TO_UPSTREAM;
} }
@@ -538,6 +549,14 @@ int proxy_write_to_upstream( struct proxier* proxy, int state )
* still need req.size if reading the reply fails - we disconnect * still need req.size if reading the reply fails - we disconnect
* and resend the reply in that case - so keep it around for now. */ * and resend the reply in that case - so keep it around for now. */
proxy->req.needle = 0; proxy->req.needle = 0;
if ( AF_UNIX != proxy->connect_to.family ) {
if ( sock_set_tcp_cork( proxy->upstream_fd, 0 ) == -1 ) {
warn( SHOW_ERRNO( "Failed to unset TCP_CORK" ) );
// TODO: should we return to CONNECT_TO_UPSTREAM in this instance?
}
}
return READ_FROM_UPSTREAM; return READ_FROM_UPSTREAM;
} }