From bdd8648bae96001a06cf8718d4e4f813cb7421fc Mon Sep 17 00:00:00 2001 From: David Wilson Date: Fri, 16 Feb 2018 08:25:05 +0545 Subject: [PATCH] ssh: enable compression by default Using the same test as in 7af97c0365d86ec851c56d2d3736be2cc281af29, transmitted wire bytes drops from 135,531 to 133,071 (-1.81%), while received drops from 21,073 to 14,775 (-30%). Combined, both changes shave 13,914 bytes (-8.6%) off aggregate bandwidth usage. Make it configurable as compression hurts in some scenarios. --- docs/api.rst | 6 +++++- mitogen/ssh.py | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index d1d8a705..1b6b0b58 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -562,7 +562,7 @@ Router Class a password is requested, :py:class:`mitogen.sudo.PasswordError` is raised. - .. method:: ssh (hostname, username=None, ssh_path=None, port=None, check_host_keys=True, password=None, identity_file=None, \**kwargs) + .. method:: ssh (hostname, username=None, ssh_path=None, port=None, check_host_keys=True, password=None, identity_file=None, compression_level=6, \**kwargs) Arrange for a context to be constructed over a ``ssh`` invocation. The ``ssh`` process is started in a newly allocated pseudo-terminal, and @@ -594,6 +594,10 @@ Router Class the SSH client to perform authenticaion; agent authentication is automatically disabled, as is reading the default private key from ``~/.ssh/id_rsa``, or ``~/.ssh/id_dsa``. + :param int compression_level: + Integer 0-9 representing the zlib compression level to use on the + connection, with 0 indicating compression is disabled. Defaults to + 6. Context Class diff --git a/mitogen/ssh.py b/mitogen/ssh.py index adab955d..a1bc8f18 100644 --- a/mitogen/ssh.py +++ b/mitogen/ssh.py @@ -59,7 +59,7 @@ class Stream(mitogen.parent.Stream): def construct(self, hostname, username=None, ssh_path=None, port=None, check_host_keys=True, password=None, identity_file=None, - **kwargs): + compression_level=6, **kwargs): super(Stream, self).construct(**kwargs) self.hostname = hostname self.username = username @@ -67,6 +67,7 @@ class Stream(mitogen.parent.Stream): self.check_host_keys = check_host_keys self.password = password self.identity_file = identity_file + self.compression_level = compression_level if ssh_path: self.ssh_path = ssh_path @@ -82,6 +83,11 @@ class Stream(mitogen.parent.Stream): bits += ['-o', 'IdentitiesOnly yes'] if self.identity_file: bits += ['-i', self.identity_file] + if self.compression_level: + bits += [ + '-o', 'Compression yes', + '-o', 'CompressionLevel %d' % (self.compression_level,) + ] if not self.check_host_keys: bits += [ '-o', 'StrictHostKeyChecking no',