From 6f540ce2381c07f0ef17abb8cedbef9a7e1fc7b3 Mon Sep 17 00:00:00 2001 From: nick Date: Tue, 25 Feb 2014 16:00:48 +0000 Subject: [PATCH] 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) --- src/proxy/proxy.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/proxy/proxy.c b/src/proxy/proxy.c index 043a36c..830cb48 100644 --- a/src/proxy/proxy.c +++ b/src/proxy/proxy.c @@ -525,11 +525,22 @@ int proxy_write_to_upstream( struct proxier* proxy, int state ) ssize_t count; // 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 ); if ( count == -1 ) { warn( SHOW_ERRNO( "Failed to send request to upstream" ) ); proxy->req.needle = 0; + // We're throwing the socket away so no need to uncork 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 * and resend the reply in that case - so keep it around for now. */ 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; }