You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by sp...@apache.org on 2009/12/15 04:56:35 UTC

svn commit: r890632 - /mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java

Author: spearce
Date: Tue Dec 15 03:56:35 2009
New Revision: 890632

URL: http://svn.apache.org/viewvc?rev=890632&view=rev
Log:
Aggressively flush during write if the buffer is full

Instead of queuing up all data until the next flush() call made
by the application, queue data until we reach either the allowed
packet size or the current free window space, whichever limit is
encountered first.  As soon as buffer reaches either capacity,
send it immediately to the peer.

By sizing the buffer during write() to fit the available window
space we significantly reduce the chances that a buffer split and
copy is needed during flush().  However, a buffer split could still
occur if different threads are writing to two different streams on
the same channel (e.g. one thread is writing to the stdout stream
and a different thread is writing on the stderr stream).

During write() we give ourselves a credit equal to the size of
the last packet we successfully wrote out of this channel stream.
This allows the application producer to race ahead of the network
handshake by one packet and produce more data while waiting for
the handshake to free up window space.

Modified:
    mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java

Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java?rev=890632&r1=890631&r2=890632&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java (original)
+++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java Tue Dec 15 03:56:35 2009
@@ -60,8 +60,24 @@
         if (closed) {
             throw new SshException("Already closed");
         }
-        buffer.putRawBytes(buf, s, l);
-        bufferLength += l;
+        while (l > 0) {
+            // The maximum amount we should admit without flushing again
+            // is enough to make up one full packet within our allowed
+            // window size.  We give ourselves a credit equal to the last
+            // packet we sent to allow the producer to race ahead and fill
+            // out the next packet before we block and wait for space to
+            // become available again.
+            //
+            int _l = Math.min(l, Math.min(remoteWindow.getSize() + lastSize, remoteWindow.getPacketSize()) - bufferLength);
+            if (_l <= 0) {
+                flush();
+                continue;
+            }
+            buffer.putRawBytes(buf, s, _l);
+            bufferLength += _l;
+            s += _l;
+            l -= _l;
+        }
     }
 
     @Override