Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
RetryMiddleware.php
001 <?php
002 namespace GuzzleHttp;
003
004 use GuzzleHttp\Promise\PromiseInterface;
005 use GuzzleHttp\Promise\RejectedPromise;
006 use GuzzleHttp\Psr7;
007 use Psr\Http\Message\RequestInterface;
008 use Psr\Http\Message\ResponseInterface;
009
010 /**
011 * Middleware that retries requests based on the boolean result of
012 * invoking the provided "decider" function.
013 */
014 class RetryMiddleware
015 {
016 /** @var callable */
017 private $nextHandler;
018
019 /** @var callable */
020 private $decider;
021
022 /** @var callable */
023 private $delay;
024
025 /**
026 * @param callable $decider Function that accepts the number of retries,
027 * a request, [response], and [exception] and
028 * returns true if the request is to be
029 * retried.
030 * @param callable $nextHandler Next handler to invoke.
031 * @param callable $delay Function that accepts the number of retries
032 * and [response] and returns the number of
033 * milliseconds to delay.
034 */
035 public function __construct(
036 callable $decider,
037 callable $nextHandler,
038 callable $delay = null
039 ) {
040 $this->decider = $decider;
041 $this->nextHandler = $nextHandler;
042 $this->delay = $delay ?: __CLASS__ . '::exponentialDelay';
043 }
044
045 /**
046 * Default exponential backoff delay function.
047 *
048 * @param int $retries
049 *
050 * @return int milliseconds.
051 */
052 public static function exponentialDelay($retries)
053 {
054 return (int) pow(2, $retries - 1) * 1000;
055 }
056
057 /**
058 * @param RequestInterface $request
059 * @param array $options
060 *
061 * @return PromiseInterface
062 */
063 public function __invoke(RequestInterface $request, array $options)
064 {
065 if (!isset($options['retries'])) {
066 $options['retries'] = 0;
067 }
068
069 $fn = $this->nextHandler;
070 return $fn($request, $options)
071 ->then(
072 $this->onFulfilled($request, $options),
073 $this->onRejected($request, $options)
074 );
075 }
076
077 /**
078 * Execute fulfilled closure
079 *
080 * @return mixed
081 */
082 private function onFulfilled(RequestInterface $req, array $options)
083 {
084 return function ($value) use ($req, $options) {
085 if (!call_user_func(
086 $this->decider,
087 $options['retries'],
088 $req,
089 $value,
090 null
091 )) {
092 return $value;
093 }
094 return $this->doRetry($req, $options, $value);
095 };
096 }
097
098 /**
099 * Execute rejected closure
100 *
101 * @return callable
102 */
103 private function onRejected(RequestInterface $req, array $options)
104 {
105 return function ($reason) use ($req, $options) {
106 if (!call_user_func(
107 $this->decider,
108 $options['retries'],
109 $req,
110 null,
111 $reason
112 )) {
113 return \GuzzleHttp\Promise\rejection_for($reason);
114 }
115 return $this->doRetry($req, $options);
116 };
117 }
118
119 /**
120 * @return self
121 */
122 private function doRetry(RequestInterface $request, array $options, ResponseInterface $response = null)
123 {
124 $options['delay'] = call_user_func($this->delay, ++$options['retries'], $response);
125
126 return $this($request, $options);
127 }
128 }
129