Skip to content

Commit 6de1e10

Browse files
committed
Merge pull request #327 from byroot/pooling-without-idle-timeout
Allow to disable both connection pooling and pruning idependently
2 parents 112d527 + 15716b7 commit 6de1e10

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ This file is written in reverse chronological order, newer releases will
44
appear at the top.
55

66
## `master` (Unreleased)
7-
7+
* `SSHKit::Backend::Netssh.pool.idle_timeout = 0` doesn't disable connection pooling anymore,
8+
only connection expiration. To disable connection polling use `SSHKit::Backend::Netssh.pool.enabled = false`
89
* Add your entries below here, remember to credit yourself however you want
910
to be credited!
1011
* make sure working directory for commands is properly cleared after `within` blocks

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,18 @@ seconds. This timeout can be changed as follows:
500500
SSHKit::Backend::Netssh.pool.idle_timeout = 60 # seconds
501501
```
502502

503-
If you suspect the connection pooling is causing problems, you can disable the
504-
pooling behaviour entirely by setting the idle_timeout to zero:
503+
If you suspect the connection pruning is causing problems, you can disable the
504+
expiration behaviour entirely by setting the idle_timeout to zero:
505505

506506
```ruby
507507
SSHKit::Backend::Netssh.pool.idle_timeout = 0 # disabled
508508
```
509509

510+
Or even disable the pooling entirely, but it will severely reduce performance:
511+
```
512+
SSHKit::Backend::Netssh.pool.enabled = false # disabled
513+
```
514+
510515
## Tunneling and other related SSH themes
511516

512517
In order to do special gymnasitcs with SSH, tunneling, aliasing, complex options, etc with SSHKit it is possible to use [the underlying Net::SSH API](https://github.com/capistrano/sshkit/blob/master/EXAMPLES.md#setting-global-ssh-options) however in many cases it is preferred to use the system SSH configuration file at [`~/.ssh/config`](http://man.cx/ssh_config). This allows you to have personal configuration tied to your machine that does not have to be committed with the repository. If this is not suitable (everyone on the team needs a proxy command, or some special aliasing) a file in the same format can be placed in the project directory at `~/yourproject/.ssh/config`, this will be merged with the system settings in `~/.ssh/config`, and with any configuration specified in [`SSHKit::Backend::Netssh.config.ssh_options`](https://github.com/capistrano/sshkit/blob/master/lib/sshkit/backends/netssh.rb#L133).

lib/sshkit/backends/connection_pool.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,35 @@ module Backend
1919

2020
class ConnectionPool
2121

22-
attr_accessor :idle_timeout
22+
attr_accessor :idle_timeout, :enabled
2323

2424
def initialize
2525
self.idle_timeout = 30
26+
self.enabled = true
2627
@mutex = Mutex.new
2728
@pool = {}
2829
end
2930

31+
def prune_expired?
32+
idle_timeout && idle_timeout != 0
33+
end
34+
3035
def checkout(*new_connection_args, &block)
3136
entry = nil
3237
key = new_connection_args.to_s
33-
if idle_timeout
34-
prune_expired
38+
if enabled
39+
prune_expired if prune_expired?
3540
entry = find_live_entry(key)
3641
end
3742
entry || create_new_entry(new_connection_args, key, &block)
3843
end
3944

4045
def checkin(entry)
41-
if idle_timeout
42-
prune_expired
43-
entry.expires_at = Time.now + idle_timeout
46+
if enabled
47+
if prune_expired?
48+
entry.expires_at = Time.now + idle_timeout
49+
prune_expired
50+
end
4451
@mutex.synchronize do
4552
@pool[entry.key] ||= []
4653
@pool[entry.key] << entry

test/unit/backends/test_connection_pool.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,16 @@ def test_connections_are_reused_across_threads_multiple_times
7373
assert_equal t2[:conn], t3[:conn]
7474
end
7575

76-
def test_zero_idle_timeout_disables_reuse
76+
def test_zero_idle_timeout_disables_pruning
7777
pool.idle_timeout = 0
7878

79+
conn1 = pool.checkout("conn", &connect)
80+
assert_nil conn1.expires_at
81+
end
82+
83+
def test_enabled_false_disables_pooling
84+
pool.enabled = false
85+
7986
conn1 = pool.checkout("conn", &connect)
8087
pool.checkin conn1
8188

0 commit comments

Comments
 (0)