Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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