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
|
4 |
|
public function __construct() { |
27
|
4 |
|
$this->logger = new NullLogger(); |
28
|
4 |
|
} |
29
|
|
|
|
30
|
|
|
public function setLogger( LoggerInterface $logger ) { |
31
|
|
|
$this->logger = $logger; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return callable |
36
|
|
|
*/ |
37
|
4 |
|
public function retry() { |
38
|
4 |
|
return Middleware::retry( $this->newRetryDecider() ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return callable |
43
|
|
|
*/ |
44
|
|
|
private function newRetryDecider() { |
45
|
4 |
|
return function ( |
46
|
|
|
$retries, |
47
|
|
|
Request $request, |
48
|
|
|
Response $response = null, |
49
|
|
|
RequestException $exception = null |
50
|
|
|
) { |
51
|
|
|
// Don't retry if we have run out of retries |
52
|
4 |
|
if ( $retries >= 5 ) { |
53
|
1 |
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
$shouldRetry = false; |
57
|
|
|
|
58
|
|
|
// Retry connection exceptions |
59
|
4 |
|
if( $exception instanceof ConnectException ) { |
60
|
2 |
|
$shouldRetry = true; |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
if( $response ) { |
64
|
3 |
|
$headers = $response->getHeaders(); |
65
|
|
|
|
66
|
|
|
// Retry on server errors |
67
|
3 |
|
if( $response->getStatusCode() >= 500 ) { |
68
|
1 |
|
$shouldRetry = true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Retry if we have a response with an API error worth retrying |
72
|
3 |
|
if ( array_key_exists( 'mediawiki-api-error', $headers ) ) { |
73
|
1 |
|
foreach( $headers['mediawiki-api-error'] as $mediawikiApiErrorHeader ) { |
74
|
1 |
|
if ( in_array( |
75
|
|
|
$mediawikiApiErrorHeader, |
76
|
|
|
array( |
77
|
1 |
|
'ratelimited', |
78
|
|
|
'readonly', |
79
|
|
|
'internal_api_error_DBQueryError', |
80
|
|
|
) |
81
|
|
|
) ) { |
82
|
1 |
|
$shouldRetry = true; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Log if we are retrying |
89
|
4 |
|
if( $shouldRetry ) { |
90
|
4 |
|
$this->logger->warning( |
91
|
|
|
sprintf( |
92
|
4 |
|
'Retrying %s %s %s/5, %s', |
93
|
4 |
|
$request->getMethod(), |
94
|
4 |
|
$request->getUri(), |
95
|
4 |
|
$retries + 1, |
96
|
4 |
|
$response ? 'status code: ' . $response->getStatusCode() : |
97
|
4 |
|
$exception->getMessage() |
|
|
|
|
98
|
|
|
) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
4 |
|
return $shouldRetry; |
103
|
4 |
|
}; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: