RoundRobinTransport.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mailer\Transport;
  11. use Psr\Log\LoggerInterface;
  12. use Psr\Log\NullLogger;
  13. use Symfony\Component\Mailer\Envelope;
  14. use Symfony\Component\Mailer\Exception\TransportException;
  15. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  16. use Symfony\Component\Mailer\SentMessage;
  17. use Symfony\Component\Mime\RawMessage;
  18. /**
  19. * Uses several Transports using a round robin algorithm.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class RoundRobinTransport implements TransportInterface
  24. {
  25. /**
  26. * @var \SplObjectStorage<TransportInterface, float>
  27. */
  28. private \SplObjectStorage $deadTransports;
  29. private int $cursor = -1;
  30. /**
  31. * @param TransportInterface[] $transports
  32. */
  33. public function __construct(
  34. private array $transports,
  35. private int $retryPeriod = 60,
  36. private LoggerInterface $logger = new NullLogger(),
  37. ) {
  38. if (!$transports) {
  39. throw new TransportException(\sprintf('"%s" must have at least one transport configured.', static::class));
  40. }
  41. $this->deadTransports = new \SplObjectStorage();
  42. }
  43. public function send(RawMessage $message, ?Envelope $envelope = null): ?SentMessage
  44. {
  45. $exception = null;
  46. while ($transport = $this->getNextTransport()) {
  47. try {
  48. return $transport->send(clone $message, $envelope);
  49. } catch (TransportExceptionInterface $e) {
  50. $exception ??= new TransportException('All transports failed.');
  51. $exception->appendDebug(\sprintf("Transport \"%s\": %s\n", $transport, $e->getDebug()));
  52. $this->logger->error(\sprintf('Transport "%s" failed.', $transport), ['exception' => $e]);
  53. $this->deadTransports[$transport] = microtime(true);
  54. }
  55. }
  56. throw $exception ?? new TransportException('No transports found.');
  57. }
  58. public function __toString(): string
  59. {
  60. return $this->getNameSymbol().'('.implode(' ', array_map('strval', $this->transports)).')';
  61. }
  62. /**
  63. * Rotates the transport list around and returns the first instance.
  64. */
  65. protected function getNextTransport(): ?TransportInterface
  66. {
  67. if (-1 === $this->cursor) {
  68. $this->cursor = $this->getInitialCursor();
  69. }
  70. $cursor = $this->cursor;
  71. while (true) {
  72. $transport = $this->transports[$cursor];
  73. if (!$this->isTransportDead($transport)) {
  74. break;
  75. }
  76. if ((microtime(true) - $this->deadTransports[$transport]) > $this->retryPeriod) {
  77. unset($this->deadTransports[$transport]);
  78. break;
  79. }
  80. if ($this->cursor === $cursor = $this->moveCursor($cursor)) {
  81. return null;
  82. }
  83. }
  84. $this->cursor = $this->moveCursor($cursor);
  85. return $transport;
  86. }
  87. protected function isTransportDead(TransportInterface $transport): bool
  88. {
  89. return $this->deadTransports->offsetExists($transport);
  90. }
  91. protected function getInitialCursor(): int
  92. {
  93. // the cursor initial value is randomized so that
  94. // when are not in a daemon, we are still rotating the transports
  95. return mt_rand(0, \count($this->transports) - 1);
  96. }
  97. protected function getNameSymbol(): string
  98. {
  99. return 'roundrobin';
  100. }
  101. private function moveCursor(int $cursor): int
  102. {
  103. return ++$cursor >= \count($this->transports) ? 0 : $cursor;
  104. }
  105. }