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