Skip to content

Commit 023386b

Browse files
committed
Change $retries to $tries
1 parent cecd24a commit 023386b

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,15 @@ All classes are located in the `ConnectionManager\Extra` namespace.
4444

4545
### Repeat
4646

47-
The `ConnectionManagerRepeat($connector, $repeat)` retries connecting to the given location up to a maximum
48-
of `$repeat` times when the connection fails.
47+
The `ConnectionManagerRepeat($connector, $tries)` tries connecting to the given location up to a maximum
48+
of `$tries` times when the connection fails.
49+
50+
If you pass a value of `3` to it, it will first issue a normal connection attempt
51+
and then retry up to 2 times if the connection attempt fails:
4952

5053
```php
51-
$connectorRepeater = new \ConnectionManager\Extra\ConnectionManagerRepeat($connector, 3);
54+
$connectorRepeater = new ConnectionManagerRepeat($connector, 3);
55+
5256
$connectorRepeater->create('www.google.com', 80)->then(function ($stream) {
5357
echo 'connection successfully established';
5458
$stream->close();

src/ConnectionManagerRepeat.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,20 @@
1111
class ConnectionManagerRepeat implements ConnectorInterface
1212
{
1313
protected $connectionManager;
14-
protected $maximumRepetitions;
14+
protected $maximumTries;
1515

16-
public function __construct(ConnectorInterface $connectionManager, $maximumRepetitons)
16+
public function __construct(ConnectorInterface $connectionManager, $maximumTries)
1717
{
18-
if ($maximumRepetitons < 1) {
19-
throw new InvalidArgumentException('Maximum number of repetitions must be >= 1');
18+
if ($maximumTries < 1) {
19+
throw new InvalidArgumentException('Maximum number of tries must be >= 1');
2020
}
2121
$this->connectionManager = $connectionManager;
22-
$this->maximumRepetitions = $maximumRepetitons;
22+
$this->maximumTries = $maximumTries;
2323
}
2424

2525
public function create($host, $port)
2626
{
27-
return $this->tryConnection($this->maximumRepetitions, $host, $port);
28-
}
29-
30-
public function tryConnection($repeat, $host, $port)
31-
{
32-
$tries = $repeat + 1;
27+
$tries = $this->maximumTries;
3328
$connector = $this->connectionManager;
3429

3530
return new Promise(function ($resolve, $reject) use ($host, $port, &$pending, &$tries, $connector) {
@@ -39,7 +34,7 @@ public function tryConnection($repeat, $host, $port)
3934
$pending = $connector->create($host, $port);
4035
$pending->then($resolve, $try);
4136
} else {
42-
$reject(new Exception('Connection still fails even after repeating', 0, $error));
37+
$reject(new Exception('Connection still fails even after retrying', 0, $error));
4338
}
4439
};
4540

tests/ConnectionManagerRepeatTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public function testRepeatRejected()
1717
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
1818
}
1919

20-
public function testOneRepetitionWillStartTwoConnectionAttempts()
20+
public function testTwoTriesWillStartTwoConnectionAttempts()
2121
{
2222
$promise = Promise\reject(new \RuntimeException('nope'));
2323

2424
$connector = $this->getMock('React\SocketClient\ConnectorInterface');
2525
$connector->expects($this->exactly(2))->method('create')->with('google.com', 80)->willReturn($promise);
2626

27-
$cm = new ConnectionManagerRepeat($connector, 1);
27+
$cm = new ConnectionManagerRepeat($connector, 2);
2828

2929
$promise = $cm->create('google.com', 80);
3030

0 commit comments

Comments
 (0)