Skip to content

Commit f85817a

Browse files
committed
add connection limits
1 parent d779e82 commit f85817a

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

src/Dplr/Dplr.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ class Dplr
4141
*/
4242
protected $publicKey;
4343

44+
/** @var int */
45+
protected $maxSSHAgentConnections;
46+
47+
/** @var int */
48+
protected $maxConcurrency;
49+
4450
/**
4551
* @var string
4652
*/
@@ -50,11 +56,18 @@ class Dplr
5056

5157
protected $state = self::STATE_INIT;
5258

53-
public function __construct(string $user, string $gosshaPath, string $publicKey = null)
54-
{
59+
public function __construct(
60+
string $user,
61+
string $gosshaPath,
62+
string $publicKey = null,
63+
int $maxSSHAgentConnections = 128,
64+
int $maxConcurrency = 0
65+
) {
5566
$this->user = $user;
5667
$this->publicKey = $publicKey;
5768
$this->gosshaPath = $gosshaPath;
69+
$this->maxSSHAgentConnections = $maxSSHAgentConnections;
70+
$this->maxConcurrency = $maxConcurrency;
5871

5972
$this->resetTasks();
6073
}
@@ -376,9 +389,22 @@ protected function runTasks(callable $callback = null): void
376389
$this->reports = [];
377390

378391
if ($this->publicKey) {
379-
$pl = sprintf('%s -l %s -i %s', $this->gosshaPath, $this->user, $this->publicKey);
392+
$pl = sprintf(
393+
'%s -l %s -i %s -m %d -c %d',
394+
$this->gosshaPath,
395+
$this->user,
396+
$this->publicKey,
397+
$this->maxConcurrency,
398+
$this->maxSSHAgentConnections
399+
);
380400
} else {
381-
$pl = sprintf('%s -l %s', $this->gosshaPath, $this->user);
401+
$pl = sprintf(
402+
'%s -l %s -m %d -c %d',
403+
$this->gosshaPath,
404+
$this->user,
405+
$this->maxConcurrency,
406+
$this->maxSSHAgentConnections
407+
);
382408
}
383409

384410
$descriptorSpec = [

tests/Dplr/Tests/DplrTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,41 @@ public function testMultiThread(): void
173173
}
174174
}
175175

176+
public function testLimitedConcurrency(): void
177+
{
178+
$d = new Dplr(self::USER, self::GOSSHA_PATH, self::SSH_KEY, 16, 1);
179+
$d
180+
->addServer('remote_1', ['app', 'all'])
181+
->addServer('remote_2', ['app', 'all'])
182+
->addServer('remote_3', ['job', 'all'])
183+
;
184+
$d->setDefaultTimeout(60);
185+
186+
$d->command('ls -a', 'all');
187+
$this->assertTrue($d->hasTasks());
188+
189+
$output = '';
190+
$d->run(function (string $s) use (&$output) {
191+
$output .= $s;
192+
});
193+
194+
$this->assertTrue($d->isSuccessful());
195+
$this->assertFalse($d->hasTasks());
196+
$this->assertEquals("CMD ls -a ...\n", $output);
197+
198+
$report = $d->getReport();
199+
$this->assertEquals(3, $report['total']);
200+
$this->assertEquals(3, $report['successful']);
201+
$this->assertEquals(0, $report['failed']);
202+
203+
$this->assertCount(0, $d->getFailed());
204+
$this->assertCount(3, $d->getReports());
205+
206+
foreach ($d->getReports() as $report) {
207+
$this->assertTrue($report->isSuccessful());
208+
}
209+
}
210+
176211
private static function getFixturesPath(): string
177212
{
178213
return realpath(__DIR__ . '/../Fixtures');

0 commit comments

Comments
 (0)