|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mediawiki\Api\Guzzle; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\ConnectException; |
|
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
7
|
|
|
use GuzzleHttp\Middleware; |
|
8
|
|
|
use GuzzleHttp\Psr7\Request; |
|
9
|
|
|
use GuzzleHttp\Psr7\Response; |
|
10
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
use Psr\Log\NullLogger; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @access private |
|
16
|
|
|
* |
|
17
|
|
|
* @author Addshore |
|
18
|
|
|
*/ |
|
19
|
|
|
class MiddlewareFactory implements LoggerAwareInterface { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var LoggerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $logger; |
|
25
|
|
|
|
|
26
|
10 |
|
public function __construct() { |
|
27
|
10 |
|
$this->logger = new NullLogger(); |
|
28
|
10 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param LoggerInterface $logger The new Logger object. |
|
32
|
|
|
*/ |
|
33
|
|
|
public function setLogger( LoggerInterface $logger ) { |
|
34
|
|
|
$this->logger = $logger; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @private |
|
39
|
|
|
* |
|
40
|
|
|
* @param bool $delay default to true, can be false to speed up tests |
|
41
|
|
|
* |
|
42
|
|
|
* @return callable |
|
43
|
|
|
*/ |
|
44
|
10 |
|
public function retry( $delay = true ) { |
|
45
|
10 |
|
if ( $delay ) { |
|
46
|
10 |
|
return Middleware::retry( $this->newRetryDecider(), $this->getRetryDelay() ); |
|
47
|
|
|
} else { |
|
48
|
|
|
return Middleware::retry( $this->newRetryDecider() ); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns a method that takes the number of retries and returns the number of miliseconds |
|
54
|
|
|
* to wait |
|
55
|
|
|
* |
|
56
|
|
|
* @return callable |
|
57
|
|
|
*/ |
|
58
|
10 |
|
private function getRetryDelay() { |
|
59
|
|
|
return function ( $numberOfRetries, Response $response = null ) { |
|
60
|
|
|
// The $response argument is only passed as of Guzzle 6.2.2. |
|
61
|
10 |
|
if ( $response !== null ) { |
|
62
|
|
|
// Retry-After may be a number of seconds or an absolute date (RFC 7231, |
|
63
|
|
|
// section 7.1.3). |
|
64
|
7 |
|
$retryAfter = $response->getHeaderLine( 'Retry-After' ); |
|
65
|
|
|
|
|
66
|
7 |
|
if ( is_numeric( $retryAfter ) ) { |
|
67
|
1 |
|
return 1000 * $retryAfter; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
6 |
|
if ( $retryAfter ) { |
|
71
|
2 |
|
$seconds = strtotime( $retryAfter ) - time(); |
|
72
|
2 |
|
return 1000 * max( 1, $seconds ); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
7 |
|
return 1000 * $numberOfRetries; |
|
77
|
10 |
|
}; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return callable |
|
82
|
|
|
*/ |
|
83
|
|
|
private function newRetryDecider() { |
|
84
|
10 |
|
return function ( |
|
85
|
|
|
$retries, |
|
86
|
|
|
Request $request, |
|
87
|
|
|
Response $response = null, |
|
88
|
|
|
RequestException $exception = null |
|
89
|
|
|
) { |
|
90
|
|
|
// Don't retry if we have run out of retries |
|
91
|
10 |
|
if ( $retries >= 5 ) { |
|
92
|
1 |
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
10 |
|
$shouldRetry = false; |
|
96
|
|
|
|
|
97
|
|
|
// Retry connection exceptions |
|
98
|
10 |
|
if ( $exception instanceof ConnectException ) { |
|
99
|
3 |
|
$shouldRetry = true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
10 |
|
if ( $response ) { |
|
103
|
9 |
|
$data = json_decode( $response->getBody(), true ); |
|
104
|
|
|
|
|
105
|
|
|
// Retry on server errors |
|
106
|
9 |
|
if ( $response->getStatusCode() >= 500 ) { |
|
107
|
2 |
|
$shouldRetry = true; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
9 |
|
foreach ( $response->getHeader( 'Mediawiki-Api-Error' ) as $mediawikiApiErrorHeader ) { |
|
111
|
|
|
if ( |
|
112
|
|
|
// Retry if the API explicitly tells us to: |
|
113
|
|
|
// https://www.mediawiki.org/wiki/Manual:Maxlag_parameter |
|
114
|
5 |
|
$response->getHeaderLine( 'Retry-After' ) |
|
115
|
|
|
|| |
|
116
|
|
|
// Retry if we have a response with an API error worth retrying |
|
117
|
2 |
|
in_array( |
|
118
|
2 |
|
$mediawikiApiErrorHeader, |
|
119
|
|
|
[ |
|
120
|
2 |
|
'ratelimited', |
|
121
|
|
|
'maxlag', |
|
122
|
|
|
'readonly', |
|
123
|
|
|
'internal_api_error_DBQueryError', |
|
124
|
|
|
] |
|
125
|
|
|
) |
|
126
|
|
|
|| |
|
127
|
|
|
// Or if we have been stopped from saving as an 'anti-abuse measure' |
|
128
|
|
|
// Note: this tries to match "actionthrottledtext" i18n messagae for mediawiki |
|
129
|
|
|
( |
|
130
|
2 |
|
$mediawikiApiErrorHeader == 'failed-save' && |
|
131
|
5 |
|
strstr( $data['error']['info'], 'anti-abuse measure' ) |
|
132
|
|
|
) |
|
133
|
|
|
) { |
|
134
|
5 |
|
$shouldRetry = true; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// Log if we are retrying |
|
141
|
10 |
|
if ( $shouldRetry ) { |
|
142
|
10 |
|
$this->logger->warning( |
|
143
|
10 |
|
sprintf( |
|
144
|
10 |
|
'Retrying %s %s %s/5, %s', |
|
145
|
10 |
|
$request->getMethod(), |
|
146
|
10 |
|
$request->getUri(), |
|
147
|
10 |
|
$retries + 1, |
|
148
|
10 |
|
$response ? 'status code: ' . $response->getStatusCode() : |
|
149
|
10 |
|
$exception->getMessage() |
|
|
|
|
|
|
150
|
|
|
) |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
10 |
|
return $shouldRetry; |
|
155
|
10 |
|
}; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: