Skip to content

Commit c9b30b6

Browse files
committed
reuse needRetry in RetryDomainsMiddleware and add tests for needRetry
1 parent bdbfacf commit c9b30b6

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

src/Qiniu/Http/Middleware/RetryDomainsMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private function shouldRetry($resp, $req)
3838
return call_user_func($this->retryCondition, $resp, $req);
3939
}
4040

41-
return !$resp || !$resp->ok();
41+
return !$resp || $resp->needRetry();
4242
}
4343

4444
/**

src/Qiniu/Http/Response.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,18 @@ public function ok()
199199

200200
public function needRetry()
201201
{
202-
$code = $this->statusCode;
203-
if ($code < 0 || ($code / 100 === 5 and $code !== 579) || $code === 996) {
204-
return true;
202+
if ($this->statusCode > 0 && $this->statusCode < 500) {
203+
return false;
205204
}
205+
206+
// https://developer.qiniu.com/fusion/kb/1352/the-http-request-return-a-status-code
207+
if (in_array($this->statusCode, array(
208+
501, 509, 573, 579, 608, 612, 614, 616, 618, 630, 631, 632, 640, 701
209+
))) {
210+
return false;
211+
}
212+
213+
return true;
206214
}
207215

208216
private static function isJson($headers)

src/Qiniu/Region.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,7 @@ public static function queryRegion($ak, $bucket, $ucHost = null, $backupUcHosts
184184
$reqOpt->middlewares = array(
185185
new RetryDomainsMiddleware(
186186
$backupUcHosts,
187-
$retryTimes,
188-
function ($resp) {
189-
// 612 is app/accesskey is not found
190-
// 631 is no such bucket
191-
return !$resp->ok() && !in_array($resp->statusCode, array(612, 631));
192-
}
187+
$retryTimes
193188
)
194189
);
195190
$ret = Client::Get($url, array(), $reqOpt);

tests/Qiniu/Tests/HttpTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Qiniu\Http\Client;
77
use Qiniu\Http\RequestOptions;
8+
use Qiniu\Http\Response;
89

910
class HttpTest extends TestCase
1011
{
@@ -122,4 +123,29 @@ public function testPutTimeout()
122123
$response = Client::put('localhost:9000/timeout.php', null, array(), $reqOpt);
123124
$this->assertEquals(-1, $response->statusCode);
124125
}
126+
127+
public function testNeedRetry()
128+
{
129+
$testCases = array_merge(
130+
array(array(-1, true)),
131+
array_map(function ($i) {
132+
return array($i, false);
133+
}, range(100, 499)),
134+
array_map(function ($i) {
135+
if (in_array($i, array(
136+
501, 509, 573, 579, 608, 612, 614, 616, 618, 630, 631, 632, 640, 701
137+
))) {
138+
return array($i, false);
139+
}
140+
return array($i, true);
141+
}, range(500, 799))
142+
);
143+
$resp = new Response(-1, 222, array(), '{"msg": "mock"}', null);
144+
foreach ($testCases as $testCase) {
145+
list($code, $expectNeedRetry) = $testCase;
146+
$resp->statusCode = $code;
147+
$msg = $resp->statusCode . ' need' . ($expectNeedRetry ? '' : ' NOT') . ' retry';
148+
$this->assertEquals($expectNeedRetry, $resp->needRetry(), $msg);
149+
}
150+
}
125151
}

0 commit comments

Comments
 (0)