|
1
|
|
|
<?php |
|
2
|
|
|
namespace Qiniu\Http\Middleware; |
|
3
|
|
|
|
|
4
|
|
|
use Qiniu\Http\Request; |
|
5
|
|
|
use Qiniu\Http\Response; |
|
6
|
|
|
|
|
7
|
|
|
class RetryDomainsMiddleware implements Middleware |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array<string> backup domains. |
|
11
|
|
|
*/ |
|
12
|
|
|
private $backupDomains; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var numeric max retry times for each backup domains. |
|
|
|
|
|
|
16
|
|
|
*/ |
|
17
|
|
|
private $maxRetryTimes; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var callable args response and request; returns bool; If true will retry with backup domains. |
|
21
|
|
|
*/ |
|
22
|
|
|
private $retryCondition; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param array<string> $backupDomains |
|
26
|
|
|
* @param numeric $maxRetryTimes |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($backupDomains, $maxRetryTimes = 2, $retryCondition = null) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->backupDomains = $backupDomains; |
|
31
|
|
|
$this->maxRetryTimes = $maxRetryTimes; |
|
|
|
|
|
|
32
|
|
|
$this->retryCondition = $retryCondition; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private function shouldRetry($resp, $req) |
|
36
|
|
|
{ |
|
37
|
|
|
if (is_callable($this->retryCondition)) { |
|
38
|
|
|
return call_user_func($this->retryCondition, $resp, $req); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return !$resp || $resp->needRetry(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Request $request |
|
46
|
|
|
* @param callable(Request): Response $next |
|
47
|
|
|
* @return Response |
|
48
|
|
|
*/ |
|
49
|
|
|
public function send($request, $next) |
|
50
|
|
|
{ |
|
51
|
|
|
$response = null; |
|
52
|
|
|
$urlComponents = parse_url($request->url); |
|
53
|
|
|
|
|
54
|
|
|
foreach (array_merge(array($urlComponents["host"]), $this->backupDomains) as $backupDomain) { |
|
55
|
|
|
$urlComponents["host"] = $backupDomain; |
|
56
|
|
|
$request->url = \Qiniu\unparse_url($urlComponents); |
|
57
|
|
|
$retriedTimes = 0; |
|
58
|
|
|
|
|
59
|
|
|
while ($retriedTimes < $this->maxRetryTimes) { |
|
60
|
|
|
$response = $next($request); |
|
61
|
|
|
|
|
62
|
|
|
$retriedTimes += 1; |
|
63
|
|
|
|
|
64
|
|
|
if (!$this->shouldRetry($response, $request)) { |
|
65
|
|
|
return $response; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (!$response) { |
|
71
|
|
|
$response = $next($request); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $response; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths