Skip to content

[Mailer] Fix failover transport #37611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public function testSendFirstWork()
$t2 = $this->createMock(TransportInterface::class);
$t2->expects($this->never())->method('send');
$t = new FailoverTransport([$t1, $t2]);
$p = new \ReflectionProperty(RoundRobinTransport::class, 'cursor');
$p->setAccessible(true);
$p->setValue($t, 0);
$t->send(new RawMessage(''));
$this->assertTransports($t, 1, []);
$t->send(new RawMessage(''));
Expand Down Expand Up @@ -77,9 +74,6 @@ public function testSendOneDead()
$t2 = $this->createMock(TransportInterface::class);
$t2->expects($this->exactly(3))->method('send');
$t = new FailoverTransport([$t1, $t2]);
$p = new \ReflectionProperty(RoundRobinTransport::class, 'cursor');
$p->setAccessible(true);
$p->setValue($t, 0);
$t->send(new RawMessage(''));
$this->assertTransports($t, 0, [$t1]);
$t->send(new RawMessage(''));
Expand All @@ -99,9 +93,6 @@ public function testSendOneDeadAndRecoveryWithinRetryPeriod()
$t2->expects($this->at(2))->method('send');
$t2->expects($this->at(3))->method('send')->will($this->throwException(new TransportException()));
$t = new FailoverTransport([$t1, $t2], 6);
$p = new \ReflectionProperty(RoundRobinTransport::class, 'cursor');
$p->setAccessible(true);
$p->setValue($t, 0);
$t->send(new RawMessage('')); // t1>fail - t2>sent
$this->assertTransports($t, 0, [$t1]);
sleep(4);
Expand Down Expand Up @@ -148,9 +139,6 @@ public function testSendOneDeadButRecover()
$t2->expects($this->at(1))->method('send');
$t2->expects($this->at(2))->method('send')->will($this->throwException(new TransportException()));
$t = new FailoverTransport([$t1, $t2], 1);
$p = new \ReflectionProperty(RoundRobinTransport::class, 'cursor');
$p->setAccessible(true);
$p->setValue($t, 0);
$t->send(new RawMessage(''));
sleep(1);
$t->send(new RawMessage(''));
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Transport/FailoverTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ protected function getNextTransport(): ?TransportInterface
return $this->currentTransport;
}

protected function getInitialCursor(): int
{
return 0;
}

protected function getNameSymbol(): string
{
return 'failover';
Expand Down
11 changes: 8 additions & 3 deletions src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public function __toString(): string
protected function getNextTransport(): ?TransportInterface
{
if (-1 === $this->cursor) {
// the cursor initial value is randomized so that
// when are not in a daemon, we are still rotating the transports
$this->cursor = mt_rand(0, \count($this->transports) - 1);
$this->cursor = $this->getInitialCursor();
}

$cursor = $this->cursor;
Expand Down Expand Up @@ -101,6 +99,13 @@ protected function isTransportDead(TransportInterface $transport): bool
return $this->deadTransports->contains($transport);
}

protected function getInitialCursor(): int
{
// the cursor initial value is randomized so that
// when are not in a daemon, we are still rotating the transports
return mt_rand(0, \count($this->transports) - 1);
}

protected function getNameSymbol(): string
{
return 'roundrobin';
Expand Down