Skip to content

Commit

Permalink
Improve SSHKit backend with Channel.select
Browse files Browse the repository at this point in the history
  • Loading branch information
eagletmt committed Feb 6, 2016
1 parent 50dafdf commit 6781a80
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 41 deletions.
25 changes: 7 additions & 18 deletions example/exec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env ruby
require 'libssh'
require 'io/wait'

GC.stress = true
puts "libssh #{LibSSH::LIBSSH_VERSION}"
Expand Down Expand Up @@ -43,24 +42,14 @@
channel.open_session do
channel.request_exec('ps auxf')
until channel.eof?
io.wait_readable

loop do
out = channel.read_nonblocking(bufsiz)
if out && !out.empty?
$stdout.write(out)
else
break
end
LibSSH::Channel.select([channel], [], [], nil)
out = channel.read_nonblocking(bufsiz)
if out && !out.empty?
$stdout.write(out)
end

loop do
err = channel.read_nonblocking(bufsiz, true)
if err && !err.empty?
$stderr.write(err)
else
break
end
err = channel.read_nonblocking(bufsiz, stderr: true)
if err && !err.empty?
$stderr.write(err)
end
end
end
2 changes: 1 addition & 1 deletion ext/libssh_ruby/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ static void set_select_channels(ssh_channel **c_channels, VALUE rb_channels) {
* @param [Array<Channel>] read_channels
* @param [Array<Channel>] write_channels
* @param [Array<Channel>] except_channels
* @param [Fixnum] timeout timeout in seconds.
* @param [Fixnum, nil] timeout timeout in seconds.
* @return [nil]
* @see http://api.libssh.org/stable/group__libssh__channel.html
* ssh_channel_select
Expand Down
37 changes: 15 additions & 22 deletions lib/sshkit/backends/libssh.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'libssh'
require 'io/wait'
require 'sshkit/backends/abstract'
require 'sshkit/backends/connection_pool'

Expand Down Expand Up @@ -53,8 +52,11 @@ def upload!(local, remote, _options = {})
scp.init do
scp.push_file(File.basename(remote), io.size, mode)
info "Uploading #{remote}"
while (buf = io.read(BUFSIZ))
scp.write(buf)
begin
loop do
scp.write(io.readpartial(BUFSIZ))
end
rescue EOFError
end
end
end
Expand Down Expand Up @@ -110,33 +112,24 @@ def execute_command(cmd)

with_session do |session|
channel = LibSSH::Channel.new(session)
io = IO.for_fd(session.fd, autoclose: false)
channel.open_session do
if Libssh.config.pty
channel.request_pty
end
channel.request_exec(cmd.to_command)
until channel.eof?
io.wait_readable

loop do
buf = channel.read_nonblocking(BUFSIZ)
if buf && !buf.empty?
cmd.on_stdout(channel, buf)
output.log_command_data(cmd, :stdout, buf)
else
break
end
LibSSH::Channel.select([channel], [], [], nil)

buf = channel.read_nonblocking(BUFSIZ)
if buf && !buf.empty?
cmd.on_stdout(channel, buf)
output.log_command_data(cmd, :stdout, buf)
end

loop do
buf = channel.read_nonblocking(BUFSIZ, true)
if buf && !buf.empty?
cmd.on_stderr(channel, buf)
output.log_command_data(cmd, :stderr, buf)
else
break
end
buf = channel.read_nonblocking(BUFSIZ, stderr: true)
if buf && !buf.empty?
cmd.on_stderr(channel, buf)
output.log_command_data(cmd, :stderr, buf)
end
end

Expand Down

0 comments on commit 6781a80

Please sign in to comment.