changeset 4938:e3539fafe74f HEAD

Delay setting the TCP cork until something is actually sent to the network.
author Timo Sirainen <tss@iki.fi>
date Wed, 20 Dec 2006 17:40:22 +0200
parents 4936071e7a96
children ff2272c228cc
files src/lib/ostream-file.c
diffstat 1 files changed, 20 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/ostream-file.c	Wed Dec 20 17:18:45 2006 +0200
+++ b/src/lib/ostream-file.c	Wed Dec 20 17:40:22 2006 +0200
@@ -42,6 +42,7 @@
 	unsigned int file:1;
 	unsigned int corked:1;
 	unsigned int flush_pending:1;
+	unsigned int socket_cork_set:1;
 	unsigned int no_socket_cork:1;
 	unsigned int no_sendfile:1;
 	unsigned int autoclose_fd:1;
@@ -120,6 +121,18 @@
 		fstream->head = 0;
 }
 
+static void o_stream_socket_cork(struct file_ostream *fstream)
+{
+	if (fstream->corked && !fstream->socket_cork_set) {
+		if (!fstream->no_socket_cork) {
+			if (net_set_cork(fstream->fd, TRUE) < 0)
+				fstream->no_socket_cork = TRUE;
+			else
+				fstream->socket_cork_set = TRUE;
+		}
+	}
+}
+
 static ssize_t o_stream_writev(struct file_ostream *fstream,
 			       const struct const_iovec *iov, int iov_size)
 {
@@ -127,6 +140,7 @@
 	size_t size, sent;
 	int i;
 
+	o_stream_socket_cork(fstream);
 	if (iov_size == 1)
 		ret = write(fstream->fd, iov->iov_base, iov->iov_len);
 	else {
@@ -226,9 +240,11 @@
 			}
 		}
 
-		if (!fstream->no_socket_cork) {
-			if (net_set_cork(fstream->fd, set) < 0)
+		if (fstream->socket_cork_set) {
+			i_assert(!set);
+			if (net_set_cork(fstream->fd, FALSE) < 0)
 				fstream->no_socket_cork = TRUE;
+			fstream->socket_cork_set = FALSE;
 		}
 		fstream->corked = set;
 	}
@@ -487,6 +503,8 @@
 	uoff_t offset, send_size, v_offset;
 	ssize_t ret;
 
+	o_stream_socket_cork(foutstream);
+
 	/* flush out any data in buffer */
 	if ((ret = buffer_flush(foutstream)) <= 0)
 		return ret;