Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 38... Línea 38...
38
     * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke.
38
     * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke.
39
     * @param (callable(int): int)|null                           $delay       Function that accepts the number of retries
39
     * @param (callable(int): int)|null                           $delay       Function that accepts the number of retries
40
     *                                                                         and returns the number of
40
     *                                                                         and returns the number of
41
     *                                                                         milliseconds to delay.
41
     *                                                                         milliseconds to delay.
42
     */
42
     */
43
    public function __construct(callable $decider, callable $nextHandler, callable $delay = null)
43
    public function __construct(callable $decider, callable $nextHandler, ?callable $delay = null)
44
    {
44
    {
45
        $this->decider = $decider;
45
        $this->decider = $decider;
46
        $this->nextHandler = $nextHandler;
46
        $this->nextHandler = $nextHandler;
47
        $this->delay = $delay ?: __CLASS__ . '::exponentialDelay';
47
        $this->delay = $delay ?: __CLASS__.'::exponentialDelay';
48
    }
48
    }
Línea 49... Línea 49...
49
 
49
 
50
    /**
50
    /**
51
     * Default exponential backoff delay function.
51
     * Default exponential backoff delay function.
52
     *
52
     *
53
     * @return int milliseconds.
53
     * @return int milliseconds.
54
     */
54
     */
55
    public static function exponentialDelay(int $retries): int
55
    public static function exponentialDelay(int $retries): int
56
    {
56
    {
57
        return (int) \pow(2, $retries - 1) * 1000;
57
        return (int) 2 ** ($retries - 1) * 1000;
Línea 58... Línea 58...
58
    }
58
    }
59
 
59
 
60
    public function __invoke(RequestInterface $request, array $options): PromiseInterface
60
    public function __invoke(RequestInterface $request, array $options): PromiseInterface
61
    {
61
    {
62
        if (!isset($options['retries'])) {
62
        if (!isset($options['retries'])) {
Línea 63... Línea 63...
63
            $options['retries'] = 0;
63
            $options['retries'] = 0;
-
 
64
        }
64
        }
65
 
65
 
66
        $fn = $this->nextHandler;
66
        $fn = $this->nextHandler;
67
 
67
        return $fn($request, $options)
68
        return $fn($request, $options)
68
            ->then(
69
            ->then(
Línea 83... Línea 84...
83
                $value,
84
                $value,
84
                null
85
                null
85
            )) {
86
            )) {
86
                return $value;
87
                return $value;
87
            }
88
            }
-
 
89
 
88
            return $this->doRetry($request, $options, $value);
90
            return $this->doRetry($request, $options, $value);
89
        };
91
        };
90
    }
92
    }
Línea 91... Línea 93...
91
 
93
 
Línea 101... Línea 103...
101
                null,
103
                null,
102
                $reason
104
                $reason
103
            )) {
105
            )) {
104
                return P\Create::rejectionFor($reason);
106
                return P\Create::rejectionFor($reason);
105
            }
107
            }
-
 
108
 
106
            return $this->doRetry($req, $options);
109
            return $this->doRetry($req, $options);
107
        };
110
        };
108
    }
111
    }
Línea 109... Línea 112...
109
 
112
 
110
    private function doRetry(RequestInterface $request, array $options, ResponseInterface $response = null): PromiseInterface
113
    private function doRetry(RequestInterface $request, array $options, ?ResponseInterface $response = null): PromiseInterface
111
    {
114
    {
Línea 112... Línea 115...
112
        $options['delay'] = ($this->delay)(++$options['retries'], $response, $request);
115
        $options['delay'] = ($this->delay)(++$options['retries'], $response, $request);
113
 
116