Skip to content

Commit

Permalink
Add retry_period option for email transport
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien Despont authored and fabpot committed Jan 4, 2025
1 parent e4d3587 commit 29c39e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Tests/TransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public static function fromStringProvider(): iterable
'roundrobin(dummy://a failover(dummy://b dummy://a) dummy://b)',
new RoundRobinTransport([$transportA, new FailoverTransport([$transportB, $transportA]), $transportB]),
];

yield 'round robin transport with retry period' => [
'roundrobin(dummy://a dummy://b)?retry_period=15',
new RoundRobinTransport([$transportA, $transportB], 15),
];
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public function fromStrings(#[\SensitiveParameter] array $dsns): Transports
public function fromString(#[\SensitiveParameter] string $dsn): TransportInterface
{
[$transport, $offset] = $this->parseDsn($dsn);
if ($offset !== \strlen($dsn)) {
$dnsWithoutMainOptions = preg_replace('/[?&]retry_period=\d+/', '', $dsn);
if ($offset !== \strlen($dnsWithoutMainOptions)) {
throw new InvalidArgumentException('The mailer DSN has some garbage at the end.');
}

Expand All @@ -121,6 +122,10 @@ private function parseDsn(#[\SensitiveParameter] string $dsn, int $offset = 0):
'roundrobin' => RoundRobinTransport::class,
];

$parsedUrl = parse_url($dsn);
parse_str($parsedUrl['query'] ?? '', $query);
$retryPeriod = min((int) ($query['retry_period'] ?? 60), 60);

while (true) {
foreach ($keywords as $name => $class) {
$name .= '(';
Expand All @@ -145,7 +150,7 @@ private function parseDsn(#[\SensitiveParameter] string $dsn, int $offset = 0):
}
}

return [new $class($args), $offset];
return [new $class($args, $retryPeriod), $offset];
}
}

Expand Down

0 comments on commit 29c39e6

Please sign in to comment.