Skip to content

Commit bdbfacf

Browse files
committed
add retryCondition to RetryDomainsMiddleware
1 parent c399260 commit bdbfacf

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

src/Qiniu/Http/Middleware/RetryDomainsMiddleware.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,29 @@ class RetryDomainsMiddleware implements Middleware
1616
*/
1717
private $maxRetryTimes;
1818

19+
/**
20+
* @var callable args response and request; returns bool; If true will retry with backup domains.
21+
*/
22+
private $retryCondition;
23+
1924
/**
2025
* @param array<string> $backupDomains
2126
* @param numeric $maxRetryTimes
2227
*/
23-
public function __construct($backupDomains, $maxRetryTimes = 2)
28+
public function __construct($backupDomains, $maxRetryTimes = 2, $retryCondition = null)
2429
{
2530
$this->backupDomains = $backupDomains;
2631
$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->ok();
2742
}
2843

2944
/**
@@ -46,7 +61,7 @@ public function send($request, $next)
4661

4762
$retriedTimes += 1;
4863

49-
if ($response->ok()) {
64+
if (!$this->shouldRetry($response, $request)) {
5065
return $response;
5166
}
5267
}

src/Qiniu/Region.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,12 @@ public static function queryRegion($ak, $bucket, $ucHost = null, $backupUcHosts
184184
$reqOpt->middlewares = array(
185185
new RetryDomainsMiddleware(
186186
$backupUcHosts,
187-
$retryTimes
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+
}
188193
)
189194
);
190195
$ret = Client::Get($url, array(), $reqOpt);

tests/Qiniu/Tests/MiddlewareTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,41 @@ public function testSendWithRetryDomains()
120120
$this->assertEquals($expectRecords, $orderRecorder);
121121
$this->assertEquals(200, $response->statusCode);
122122
}
123+
124+
public function testSendFailFastWithRetryDomains()
125+
{
126+
$orderRecorder = array();
127+
128+
$reqOpt = new RequestOptions();
129+
$reqOpt->middlewares = array(
130+
new Middleware\RetryDomainsMiddleware(
131+
array(
132+
"unavailable.phpsdk.qiniu.com",
133+
"qiniu.com",
134+
),
135+
3,
136+
function () {
137+
return false;
138+
}
139+
),
140+
new RecorderMiddleware($orderRecorder, "rec")
141+
);
142+
143+
$request = new Request(
144+
"GET",
145+
"https://fake.phpsdk.qiniu.com/index.html",
146+
array(),
147+
null,
148+
$reqOpt
149+
);
150+
$response = Client::sendRequestWithMiddleware($request);
151+
152+
$expectRecords = array(
153+
// 'fake.phpsdk.qiniu.com' will fail fast
154+
'bef_rec0',
155+
'aft_rec1'
156+
);
157+
$this->assertEquals($expectRecords, $orderRecorder);
158+
$this->assertEquals(-1, $response->statusCode);
159+
}
123160
}

0 commit comments

Comments
 (0)