a97b9ab
From 9e56b84c41efcaf3349f82a93c3dc854e172e5c4 Mon Sep 17 00:00:00 2001
a97b9ab
From: Kamil Dudka <kdudka@redhat.com>
a97b9ab
Date: Fri, 9 Aug 2013 16:22:08 +0200
a97b9ab
Subject: [PATCH 4/5] partially revert "window_size: explicit adjustments only"
a97b9ab
a97b9ab
This partially reverts commit 03ca9020756a4e16f0294e5b35e9826ee6af2364
a97b9ab
in order to fix extreme slowdown when uploading to localhost via SFTP.
a97b9ab
a97b9ab
I was able to repeat the issue on RHEL-7 on localhost only.  It did not
a97b9ab
occur when uploading via network and it did not occur on a RHEL-6 box
a97b9ab
with the same version of libssh2.
a97b9ab
a97b9ab
The problem was that sftp_read() used a read-ahead logic to figure out
a97b9ab
the window_size, but sftp_packet_read() called indirectly from
a97b9ab
sftp_write() did not use any read-ahead logic.
a97b9ab
---
a97b9ab
 src/channel.c |   29 +++++++++++++++++++++++++++++
a97b9ab
 1 files changed, 29 insertions(+), 0 deletions(-)
a97b9ab
a97b9ab
diff --git a/src/channel.c b/src/channel.c
a97b9ab
index 4f41e1f..d4ffdce 100644
a97b9ab
--- a/src/channel.c
a97b9ab
+++ b/src/channel.c
a97b9ab
@@ -1759,6 +1759,15 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id,
a97b9ab
         channel->read_state = libssh2_NB_state_created;
a97b9ab
     }
a97b9ab
 
a97b9ab
+    /*
a97b9ab
+     * =============================== NOTE ===============================
a97b9ab
+     * I know this is very ugly and not a really good use of "goto", but
a97b9ab
+     * this case statement would be even uglier to do it any other way
a97b9ab
+     */
a97b9ab
+    if (channel->read_state == libssh2_NB_state_jump1) {
a97b9ab
+        goto channel_read_window_adjust;
a97b9ab
+    }
a97b9ab
+
a97b9ab
     rc = 1; /* set to >0 to let the while loop start */
a97b9ab
 
a97b9ab
     /* Process all pending incoming packets in all states in order to "even
a97b9ab
@@ -1867,6 +1876,26 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id,
a97b9ab
            more off the network again */
a97b9ab
         channel->read_state = libssh2_NB_state_created;
a97b9ab
 
a97b9ab
+    if(channel->remote.window_size < (LIBSSH2_CHANNEL_WINDOW_DEFAULT*30)) {
a97b9ab
+        /* the window is getting too narrow, expand it! */
a97b9ab
+
a97b9ab
+      channel_read_window_adjust:
a97b9ab
+        channel->read_state = libssh2_NB_state_jump1;
a97b9ab
+        /* the actual window adjusting may not finish so we need to deal with
a97b9ab
+           this special state here */
a97b9ab
+        rc = _libssh2_channel_receive_window_adjust(channel,
a97b9ab
+                                                    (LIBSSH2_CHANNEL_WINDOW_DEFAULT*60), 0, NULL);
a97b9ab
+        if (rc)
a97b9ab
+            return rc;
a97b9ab
+
a97b9ab
+        _libssh2_debug(session, LIBSSH2_TRACE_CONN,
a97b9ab
+                       "channel_read() filled %d adjusted %d",
a97b9ab
+                       bytes_read, buflen);
a97b9ab
+        /* continue in 'created' state to drain the already read packages
a97b9ab
+           first before starting to empty the socket further */
a97b9ab
+        channel->read_state = libssh2_NB_state_created;
a97b9ab
+    }
a97b9ab
+
a97b9ab
     return bytes_read;
a97b9ab
 }
a97b9ab
 
a97b9ab
-- 
a97b9ab
1.7.1
a97b9ab