Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = 'hashicorp/precise64'
config.vm.provision "shell", inline: <<-SHELL
echo 'ClientAliveInterval 1' >> /etc/ssh/sshd_config
echo 'ClientAliveCountMax 1' >> /etc/ssh/sshd_config
service ssh restart
SHELL

json_config_path = File.join("test", "boxes.json")
list = File.open(json_config_path).read
Expand Down
17 changes: 12 additions & 5 deletions lib/sshkit/backends/connection_pool/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def push(conn)
def evict
# Peek at the first connection to see if it is still fresh. If so, we can
# return right away without needing to use `synchronize`.
first_expires_at, _connection = connections.first
return if first_expires_at.nil? || fresh?(first_expires_at)
first_expires_at, first_conn = connections.first
return if (first_expires_at.nil? || fresh?(first_expires_at)) && !closed?(first_conn)

connections.synchronize do
fresh, stale = connections.partition do |expires_at, _|
fresh?(expires_at)
fresh, stale = connections.partition do |expires_at, conn|
fresh?(expires_at) && !closed?(conn)
end
connections.replace(fresh)
stale.each { |_, conn| closer.call(conn) }
Expand Down Expand Up @@ -71,6 +71,13 @@ def fresh?(expires_at)
end

def closed?(conn)
conn.respond_to?(:closed?) && conn.closed?
return true if conn.respond_to?(:closed?) && conn.closed?
# test if connection is alive
conn.process(0) if conn.respond_to?(:process)
return false
rescue IOError => e
# connection is closed by server
return true if e.message == 'closed stream'
raise
end
end
14 changes: 14 additions & 0 deletions test/functional/backends/test_netssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,20 @@ def test_interaction_handler
end.run
assert_equal("Enter Data\nCaptured SOME DATA", captured_command_result)
end

def test_connection_pool_keepalive
# ensure we enable connection pool
SSHKit::Backend::Netssh.pool.idle_timeout = 10
Netssh.new(a_host) do |_host|
test :false
end.run
sleep 2.5
captured_command_result = nil
Netssh.new(a_host) do |_host|
captured_command_result = capture(:echo, 'some_value')
end.run
assert_equal "some_value", captured_command_result
end
end

end
Expand Down