summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Kenny <elkenny@gmail.com>2015-05-04 02:56:30 +0100
committerEugene Kenny <elkenny@gmail.com>2016-03-11 15:30:25 +0000
commit1da352d06dac8ac3fa2fe0ceb4856c2203555134 (patch)
tree8f19758283b3eabcd4f28dbed61d18a8bf50eff8
parent9682f42c3020254edefdbc66acf421858163f005 (diff)
downloadnet-ssh-multi-1da352d06dac8ac3fa2fe0ceb4856c2203555134.tar.gz
Use keepalive_interval option as IO select timeout
Net::SSH supports sending keepalive packets to the server to check whether the connection is still alive. For this to work, IO#select needs to be given a timeout so that the event loop can run periodically and send the packets.
-rw-r--r--lib/net/ssh/multi/session.rb11
-rw-r--r--test/session_test.rb21
2 files changed, 31 insertions, 1 deletions
diff --git a/lib/net/ssh/multi/session.rb b/lib/net/ssh/multi/session.rb
index 2e2468f..409aeae 100644
--- a/lib/net/ssh/multi/session.rb
+++ b/lib/net/ssh/multi/session.rb
@@ -425,7 +425,7 @@ module Net; module SSH; module Multi
# +false+ (the block returned +false+).
def process(wait=nil, &block)
realize_pending_connections!
- wait = @connect_threads.any? ? 0 : wait
+ wait = @connect_threads.any? ? 0 : io_select_wait(wait)
return false unless preprocess(&block)
@@ -441,6 +441,15 @@ module Net; module SSH; module Multi
end
end
+ def io_select_wait(wait)
+ [wait, keepalive_interval].compact.min
+ end
+
+ def keepalive_interval
+ servers = server_list.select { |s| s.options[:keepalive] }
+ servers.map { |s| s.options[:keepalive_interval] }.compact.min
+ end
+
# Runs the preprocess stage on all servers. Returns false if the block
# returns false, and true if there either is no block, or it returns true.
# This is called as part of the #process method.
diff --git a/test/session_test.rb b/test/session_test.rb
index 42a56b2..c027db9 100644
--- a/test/session_test.rb
+++ b/test/session_test.rb
@@ -198,4 +198,25 @@ class SessionTest < Minitest::Test
IO.expects(:select).with([:a, :b, :c], [:a, :c], nil, 5).returns([[:b, :c], [:a, :c]])
@session.process(5)
end
+
+ def test_process_should_pass_minimum_keepalive_interval_as_io_select_timeout
+ @session.use('h1', :keepalive => true)
+ @session.use('h2', :keepalive_interval => 1)
+ @session.use('h3', :keepalive => true, :keepalive_interval => 2)
+ @session.use('h4', :keepalive => true, :keepalive_interval => 3)
+ IO.expects(:select).with([], [], nil, 2)
+ @session.process
+ end
+
+ def test_process_should_pass_wait_as_io_select_timeout_if_provided_and_minimum
+ @session.use('h1', :keepalive => true, :keepalive_interval => 1)
+ IO.expects(:select).with([], [], nil, 1)
+ @session.process(2)
+ end
+
+ def test_process_should_pass_nil_as_io_select_timeout_by_default
+ @session.use('h1')
+ IO.expects(:select).with([], [], nil, nil)
+ @session.process
+ end
end