RoundRobinTransport.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 Symfony\Component\Mailer\Envelope;
  12. use Symfony\Component\Mailer\Exception\TransportException;
  13. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  14. use Symfony\Component\Mailer\SentMessage;
  15. use Symfony\Component\Mime\RawMessage;
  16. /**
  17. * Uses several Transports using a round robin algorithm.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class RoundRobinTransport implements TransportInterface
  22. {
  23. private $deadTransports;
  24. private $transports = [];
  25. private $retryPeriod;
  26. private $cursor = -1;
  27. /**
  28. * @param TransportInterface[] $transports
  29. */
  30. public function __construct(array $transports, int $retryPeriod = 60)
  31. {
  32. if (!$transports) {
  33. throw new TransportException(sprintf('"%s" must have at least one transport configured.', static::class));
  34. }
  35. $this->transports = $transports;
  36. $this->deadTransports = new \SplObjectStorage();
  37. $this->retryPeriod = $retryPeriod;
  38. }
  39. public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
  40. {
  41. while ($transport = $this->getNextTransport()) {
  42. try {
  43. return $transport->send($message, $envelope);
  44. } catch (TransportExceptionInterface $e) {
  45. $this->deadTransports[$transport] = microtime(true);
  46. }
  47. }
  48. throw new TransportException('All transports failed.');
  49. }
  50. public function __toString(): string
  51. {
  52. return $this->getNameSymbol().'('.implode(' ', array_map('strval', $this->transports)).')';
  53. }
  54. /**
  55. * Rotates the transport list around and returns the first instance.
  56. */
  57. protected function getNextTransport(): ?TransportInterface
  58. {
  59. if (-1 === $this->cursor) {
  60. $this->cursor = $this->getInitialCursor();
  61. }
  62. $cursor = $this->cursor;
  63. while (true) {
  64. $transport = $this->transports[$cursor];
  65. if (!$this->isTransportDead($transport)) {
  66. break;
  67. }
  68. if ((microtime(true) - $this->deadTransports[$transport]) > $this->retryPeriod) {
  69. $this->deadTransports->detach($transport);
  70. break;
  71. }
  72. if ($this->cursor === $cursor = $this->moveCursor($cursor)) {
  73. return null;
  74. }
  75. }
  76. $this->cursor = $this->moveCursor($cursor);
  77. return $transport;
  78. }
  79. protected function isTransportDead(TransportInterface $transport): bool
  80. {
  81. return $this->deadTransports->contains($transport);
  82. }
  83. protected function getInitialCursor(): int
  84. {
  85. // the cursor initial value is randomized so that
  86. // when are not in a daemon, we are still rotating the transports
  87. return mt_rand(0, \count($this->transports) - 1);
  88. }
  89. protected function getNameSymbol(): string
  90. {
  91. return 'roundrobin';
  92. }
  93. private function moveCursor(int $cursor): int
  94. {
  95. return ++$cursor >= \count($this->transports) ? 0 : $cursor;
  96. }
  97. }