See the commit message for the details.

The other five patches I have previously sent to this list should be
applied first.
>From 510468d4734a931408e8b40f03dcb975ce70ec9f Mon Sep 17 00:00:00 2001
From: Salvador <sfand...@yahoo.com>
Date: Wed, 16 Oct 2013 13:31:31 +0200
Subject: [PATCH 6/6] Honour window_size_initial from _libssh2_channel_read

_libssh2_channel_read was using an arbitrary hard-coded limit to
trigger the window adjusting code. The adjustment used was also
hard-coded and arbitrary, 15MB actually, which would limit the
usability of libssh2 on systems with little RAM.

This patch, uses the window_size parameter passed to
libssh2_channel_open_ex (stored as remote.window_size_initial)
plus the buflen as the base for the trigger and the adjustment
calculation.

The memory usage when using the default window size is reduced
from 22MB to 256KB per channel (actually, if compression is
used, these numbers should be incremented by ~50% to account
for the errors between the decompressed packet sizes and the
predicted sizes).

My tests indicate that this change does not impact the
performance of transfers across localhost or a LAN, being it
on par with that of OpenSSH. On the other hand, it will probably
slow down transfers on networks with high bandwidth*delay when
the default window size (LIBSSH2_CHANNEL_WINDOW_DEFAULT=256KB)
is used.

Signed-off-by: Salvador <sfand...@yahoo.com>
---
 src/channel.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/channel.c b/src/channel.c
index 9df2f8d..d6bfb98 100644
--- a/src/channel.c
+++ b/src/channel.c
@@ -1761,14 +1761,17 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id,
                    stream_id);
 
     /* expand the receiving window first if it has become too narrow */
-    if((channel->read_state == libssh2_NB_state_jump1) ||
-       (channel->remote.window_size < (LIBSSH2_CHANNEL_WINDOW_DEFAULT*30))) {
+    if( (channel->read_state == libssh2_NB_state_jump1) ||
+        (channel->remote.window_size < channel->remote.window_size_initial / 4 * 3 + buflen) ) {
+
+        uint32_t adjustment = channel->remote.window_size_initial + buflen - channel->remote.window_size;
+        if (adjustment < LIBSSH2_CHANNEL_MINADJUST)
+            adjustment = LIBSSH2_CHANNEL_MINADJUST;
 
         /* the actual window adjusting may not finish so we need to deal with
            this special state here */
         channel->read_state = libssh2_NB_state_jump1;
-        rc = _libssh2_channel_receive_window_adjust(channel,
-                                                    (LIBSSH2_CHANNEL_WINDOW_DEFAULT*60),
+        rc = _libssh2_channel_receive_window_adjust(channel, adjustment,
                                                     0, NULL);
         if (rc)
             return rc;
-- 
1.8.3.2

_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel

Reply via email to