Skip to content

Commit 96a4086

Browse files
committed
fix
1 parent ee5e18b commit 96a4086

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

src/Client.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ class Client extends GuzzleClient {
287287
'timezone' => 'PRC',
288288
'locationWithScheme' => false,
289289
'autoChange' => true,
290+
'limit_flag' => false,
290291
];
291292

292293
public function __construct(array $cosConfig) {
@@ -301,36 +302,47 @@ public function __construct(array $cosConfig) {
301302
$handler = HandlerStack::create();
302303

303304
$handler->push(Middleware::retry(function ($retries, $request, $response, $exception) use (&$retryCount) {
305+
306+
$this->cosConfig['limit_flag'] = false;
307+
304308
$retryCount = $retries;
305309
if ($retryCount >= $this->cosConfig['retry']) {
306310
return false;
307311
}
308312

313+
309314
if ($response) {
310-
if ($response->getStatusCode() >= 500) {
315+
if ($response->getStatusCode() >= 300 && !$response->hasHeader('x-cos-request-id')) {
316+
$this->cosConfig['limit_flag'] = true;
311317
return true;
312318
}
313-
} elseif ($exception) {
314-
return true;
315-
}
316319

317-
if ($response != null && $response->getStatusCode() >= 400 ) {
318-
return true;
319-
}
320-
if ($exception instanceof Exception\ServiceResponseException) {
321-
if ($exception->getStatusCode() >= 400) {
320+
if ($response->getStatusCode() >= 500 ) {
322321
return true;
323322
}
324-
}
323+
} elseif ($exception) {
324+
if ($exception instanceof Exception\ServiceResponseException) {
325+
if ($exception->getStatusCode() >= 500) {
326+
$this->cosConfig['limit_flag'] = true;
327+
return true;
328+
}
325329

326-
if ($exception instanceof ConnectException) {
327-
return true;
330+
}
331+
if ($exception instanceof ConnectException) {
332+
return true;
333+
}
328334
}
329335
return false;
330336
}, $this->retryDelay()));
331337

332338
$handler->push(Middleware::mapRequest(function (RequestInterface $request) use (&$retryCount) {
333-
if ($retryCount > 2 && $this->cosConfig['autoChange']) {
339+
// 获取域名
340+
$origin_host = $request->getUri()->getHost();
341+
342+
// 匹配 *.cos.{Region}.myqcloud.com
343+
$pattern1 = '/\.cos\.[a-z0-9-]+\.myqcloud\.com$/';
344+
345+
if ($retryCount > 2 && $this->cosConfig['autoChange'] && $this->cosConfig['limit_flag'] && preg_match($pattern1, $origin_host)) {
334346
$origin = $request->getUri();
335347
$host = str_replace("myqcloud.com", "tencentcos.cn", $origin->getHost());
336348

@@ -352,7 +364,7 @@ public function __construct(array $cosConfig) {
352364

353365

354366
// 更新请求的 URI 和主机头
355-
$request = $request->withUri($uri)->withHeader('Host', $host);
367+
$request->withUri($uri)->withHeader('Host', $host);
356368
return $request;
357369
}
358370
return $request;
@@ -436,6 +448,7 @@ public function commandToRequestTransformer(CommandInterface $command)
436448

437449
public function responseToResultTransformer(ResponseInterface $response, RequestInterface $request, CommandInterface $command)
438450
{
451+
439452
$transformer = new ResultTransformer($this->cosConfig, $this->operation);
440453
$transformer->writeDataToLocal($command, $request, $response);
441454
$deseri = new Deserializer($this->desc, true);
@@ -667,3 +680,4 @@ public static function handleErrors() {
667680
};
668681
}
669682
}
683+

0 commit comments

Comments
 (0)