Skip to content

Commit 889b1f1

Browse files
committed
update needRetry
1 parent 244d7b3 commit 889b1f1

File tree

2 files changed

+31
-59
lines changed

2 files changed

+31
-59
lines changed

qiniu/httpc/responseWrapper.js

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,18 @@ ResponseWrapper.prototype.ok = function () {
1717
* @return {boolean}
1818
*/
1919
ResponseWrapper.prototype.needRetry = function () {
20-
if (this.ok()) {
20+
if (this.resp.statusCode > 0 && this.resp.statusCode < 500) {
2121
return false;
2222
}
2323

24-
if (!this.resp || !this.resp.statusCode || this.resp.statusCode < 0) {
25-
return true;
26-
}
27-
28-
const statusCode = this.resp.statusCode;
29-
30-
// 需要重试的特例
31-
if ([996].includes(statusCode)) {
32-
return true;
33-
}
34-
35-
// 不需要重试的特例
36-
// 579 上传成功,回调失败
37-
// 612 app/AK 不存在
38-
// 631 bucket 不存在
39-
if ([579, 612, 631].includes(statusCode)) {
40-
return false;
41-
}
42-
43-
// 需要重试的状态码
44-
const statusFirstDigit = Math.floor(statusCode / 100);
45-
if ([5, 6].includes(statusFirstDigit)) {
46-
return true;
47-
}
48-
49-
// 不需要重试的状态码
50-
if ([4].includes(statusFirstDigit)) {
24+
// https://developer.qiniu.com/fusion/kb/1352/the-http-request-return-a-status-code
25+
if ([
26+
501, 509, 573, 579, 608, 612, 614, 616, 618, 630, 631, 632, 640, 701
27+
].includes(this.resp.statusCode)) {
5128
return false;
5229
}
5330

54-
return false;
31+
return true;
5532
};
5633

5734
exports.ResponseWrapper = ResponseWrapper;

test/httpc.test.js

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,32 @@ describe('test http module', function () {
1212
const { ResponseWrapper } = qiniu.httpc;
1313

1414
it('needRetry', function () {
15-
const rule = [
16-
['-1', true],
17-
['100,499', false],
18-
['500,578', true],
19-
['579', false],
20-
['580,599', true],
21-
['600,611', true],
22-
['612', false],
23-
['613,630', true],
24-
['631', false],
25-
['632,699', true]
26-
];
27-
const cases = [];
28-
for (const [codeRange, shouldRetry] of rule) {
29-
let [start, end] = codeRange.split(',');
30-
start = parseInt(start);
31-
end = parseInt(end);
32-
if (!end) {
33-
cases.push({
34-
code: start,
35-
shouldRetry
36-
});
37-
} else {
38-
for (let i = start; i <= end; i++) {
39-
cases.push({
40-
code: i,
41-
shouldRetry
42-
});
43-
}
15+
const cases = Array.from({
16+
length: 800
17+
}, (_, i) => {
18+
if (i > 0 && i < 500) {
19+
return {
20+
code: i,
21+
shouldRetry: false
22+
};
4423
}
45-
}
24+
if ([
25+
501, 509, 573, 579, 608, 612, 614, 616, 618, 630, 631, 632, 640, 701
26+
].includes(i)) {
27+
return {
28+
code: i,
29+
shouldRetry: false
30+
};
31+
}
32+
return {
33+
code: i,
34+
shouldRetry: true
35+
};
36+
});
37+
cases.unshift({
38+
code: -1,
39+
shouldRetry: true
40+
});
4641

4742
const mockedResponseWrapper = new ResponseWrapper({
4843
data: [],

0 commit comments

Comments
 (0)