Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c87cf84

Browse files
committed
[Mailer] Fix failover transport
1 parent 28e072a commit c87cf84

File tree

3 files changed

+12
-19
lines changed

3 files changed

+12
-19
lines changed

src/Symfony/Component/Mailer/Tests/Transport/FailoverTransportTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ public function testSendFirstWork()
4646
$t2 = $this->createMock(TransportInterface::class);
4747
$t2->expects($this->never())->method('send');
4848
$t = new FailoverTransport([$t1, $t2]);
49-
$p = new \ReflectionProperty(RoundRobinTransport::class, 'cursor');
50-
$p->setAccessible(true);
51-
$p->setValue($t, 0);
5249
$t->send(new RawMessage(''));
5350
$this->assertTransports($t, 1, []);
5451
$t->send(new RawMessage(''));
@@ -77,9 +74,6 @@ public function testSendOneDead()
7774
$t2 = $this->createMock(TransportInterface::class);
7875
$t2->expects($this->exactly(3))->method('send');
7976
$t = new FailoverTransport([$t1, $t2]);
80-
$p = new \ReflectionProperty(RoundRobinTransport::class, 'cursor');
81-
$p->setAccessible(true);
82-
$p->setValue($t, 0);
8377
$t->send(new RawMessage(''));
8478
$this->assertTransports($t, 0, [$t1]);
8579
$t->send(new RawMessage(''));
@@ -99,9 +93,6 @@ public function testSendOneDeadAndRecoveryWithinRetryPeriod()
9993
$t2->expects($this->at(2))->method('send');
10094
$t2->expects($this->at(3))->method('send')->will($this->throwException(new TransportException()));
10195
$t = new FailoverTransport([$t1, $t2], 6);
102-
$p = new \ReflectionProperty(RoundRobinTransport::class, 'cursor');
103-
$p->setAccessible(true);
104-
$p->setValue($t, 0);
10596
$t->send(new RawMessage('')); // t1>fail - t2>sent
10697
$this->assertTransports($t, 0, [$t1]);
10798
sleep(4);
@@ -148,9 +139,6 @@ public function testSendOneDeadButRecover()
148139
$t2->expects($this->at(1))->method('send');
149140
$t2->expects($this->at(2))->method('send')->will($this->throwException(new TransportException()));
150141
$t = new FailoverTransport([$t1, $t2], 1);
151-
$p = new \ReflectionProperty(RoundRobinTransport::class, 'cursor');
152-
$p->setAccessible(true);
153-
$p->setValue($t, 0);
154142
$t->send(new RawMessage(''));
155143
sleep(1);
156144
$t->send(new RawMessage(''));

src/Symfony/Component/Mailer/Transport/FailoverTransport.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ class FailoverTransport extends RoundRobinTransport
2020
{
2121
private $currentTransport;
2222

23+
public function __construct(array $transports, int $retryPeriod = 60)
24+
{
25+
parent::__construct($transports, $retryPeriod);
26+
27+
$this->cursor = 0;
28+
}
29+
2330
protected function getNextTransport(): ?TransportInterface
2431
{
2532
if (null === $this->currentTransport || $this->isTransportDead($this->currentTransport)) {

src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class RoundRobinTransport implements TransportInterface
2727
private $deadTransports;
2828
private $transports = [];
2929
private $retryPeriod;
30-
private $cursor = -1;
30+
/** @internal */
31+
protected $cursor;
3132

3233
/**
3334
* @param TransportInterface[] $transports
@@ -41,6 +42,9 @@ public function __construct(array $transports, int $retryPeriod = 60)
4142
$this->transports = $transports;
4243
$this->deadTransports = new \SplObjectStorage();
4344
$this->retryPeriod = $retryPeriod;
45+
// the cursor initial value is randomized so that
46+
// when are not in a daemon, we are still rotating the transports
47+
$this->cursor = mt_rand(0, \count($transports) - 1);
4448
}
4549

4650
public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
@@ -66,12 +70,6 @@ public function __toString(): string
6670
*/
6771
protected function getNextTransport(): ?TransportInterface
6872
{
69-
if (-1 === $this->cursor) {
70-
// the cursor initial value is randomized so that
71-
// when are not in a daemon, we are still rotating the transports
72-
$this->cursor = mt_rand(0, \count($this->transports) - 1);
73-
}
74-
7573
$cursor = $this->cursor;
7674
while (true) {
7775
$transport = $this->transports[$cursor];

0 commit comments

Comments
 (0)