Skip to content

Commit e180bea

Browse files
authored
[KODO-15365] 修复偶发性无法停止任务的 BUG (#580)
1 parent 3f5526f commit e180bea

File tree

8 files changed

+33
-49
lines changed

8 files changed

+33
-49
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "qiniu-js",
33
"jsName": "qiniu",
4-
"version": "3.4.0",
4+
"version": "3.4.1",
55
"private": false,
66
"description": "Javascript SDK for Qiniu Resource (Cloud) Storage AP",
77
"main": "lib/index.js",

site/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
"byte-size": "^7.0.1",
4444
"create-hmac": "^1.1.7",
4545
"js-base64": "^3.7.2",
46-
"qiniu-js": "^3.4.0",
4746
"react": "^17.0.2",
4847
"react-dom": "^17.0.2"
4948
}

site/src/upload.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react'
2-
import { upload } from 'qiniu-js'
3-
import { UploadProgress } from 'qiniu-js/esm/upload'
2+
3+
import { upload } from '../../src'
4+
import { UploadProgress } from '../../src/upload'
45

56
import { generateUploadToken, loadSetting } from './utils'
67

site/yarn.lock

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@
2323
chalk "^2.0.0"
2424
js-tokens "^4.0.0"
2525

26-
"@babel/runtime-corejs2@^7.10.2":
27-
version "7.14.0"
28-
resolved "https://registry.yarnpkg.com/@babel/runtime-corejs2/-/runtime-corejs2-7.14.0.tgz#5519b92ccc819bd3d0a1ecb25ab3cb5a775485f8"
29-
integrity sha512-btR4E8JiGlmmDI5YgirlG9z3T91rBdxnVh2YuEStrHDcekffaaIeK+CE0S4IaYUyYhMa7rFDfF2GEO79XNbLEA==
30-
dependencies:
31-
core-js "^2.6.5"
32-
regenerator-runtime "^0.13.4"
33-
3426
"@babel/runtime-corejs3@^7.10.2":
3527
version "7.14.0"
3628
resolved "https://registry.yarnpkg.com/@babel/runtime-corejs3/-/runtime-corejs3-7.14.0.tgz#6bf5fbc0b961f8e3202888cb2cd0fb7a0a9a3f66"
@@ -1077,11 +1069,6 @@ core-js-pure@^3.0.0:
10771069
resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.12.1.tgz#934da8b9b7221e2a2443dc71dfa5bd77a7ea00b8"
10781070
integrity sha512-1cch+qads4JnDSWsvc7d6nzlKAippwjUlf6vykkTLW53VSV+NkE6muGBToAjEA8pG90cSfcud3JgVmW2ds5TaQ==
10791071

1080-
core-js@^2.6.5:
1081-
version "2.6.12"
1082-
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.12.tgz#d9333dfa7b065e347cc5682219d6f690859cc2ec"
1083-
integrity sha512-Kb2wC0fvsWfQrgk8HU5lW6U/Lcs8+9aaYcy4ZFc6DDlo4nZ7n70dEgE5rtR0oG6ufKDUnrwfWL1mXR5ljDatrQ==
1084-
10851072
core-util-is@~1.0.0:
10861073
version "1.0.2"
10871074
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
@@ -3810,15 +3797,6 @@ punycode@^2.1.0:
38103797
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
38113798
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==
38123799

3813-
qiniu-js@^3.4.0:
3814-
version "3.4.0"
3815-
resolved "https://registry.yarnpkg.com/qiniu-js/-/qiniu-js-3.4.0.tgz#36980de07be4bab229ec0193a32c3318aa24b748"
3816-
integrity sha512-QJ5ShXQkyPuhMXEruyLza3V++8crvDNLTFOXzuMXrnXCvwtEbP+MZyYo4D96HRXnSFb6lAJ9s2QdzW1W3LBiIQ==
3817-
dependencies:
3818-
"@babel/runtime-corejs2" "^7.10.2"
3819-
querystring "^0.2.1"
3820-
spark-md5 "^3.0.0"
3821-
38223800
38233801
version "6.7.0"
38243802
resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
@@ -3829,11 +3807,6 @@ [email protected]:
38293807
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620"
38303808
integrity sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=
38313809

3832-
querystring@^0.2.1:
3833-
version "0.2.1"
3834-
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.1.tgz#40d77615bb09d16902a85c3e38aa8b5ed761c2dd"
3835-
integrity sha512-wkvS7mL/JMugcup3/rMitHmd9ecIGd2lhFhK9N3UUQ450h66d1r3Y9nvXzQAW1Lq+wyx61k/1pfKS5KuKiyEbg==
3836-
38373810
querystringify@^2.1.1:
38383811
version "2.2.0"
38393812
resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.2.0.tgz#3345941b4153cb9d082d8eee4cda2016a9aef7f6"
@@ -4439,11 +4412,6 @@ source-map@~0.7.2:
44394412
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
44404413
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==
44414414

4442-
spark-md5@^3.0.0:
4443-
version "3.0.1"
4444-
resolved "https://registry.yarnpkg.com/spark-md5/-/spark-md5-3.0.1.tgz#83a0e255734f2ab4e5c466e5a2cfc9ba2aa2124d"
4445-
integrity sha512-0tF3AGSD1ppQeuffsLDIOWlKUd3lS92tFxcsrh5Pe3ZphhnoK+oXIBTzOAThZCiuINZLvpiLH/1VS1/ANEJVig==
4446-
44474415
spdx-correct@^3.0.0:
44484416
version "3.1.1"
44494417
resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.1.1.tgz#dece81ac9c1e6713e5f7d1b6f17d468fa53d89a9"

src/upload/base.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,19 +265,22 @@ export default abstract class Base {
265265
this.sendLog(result.reqId, 200)
266266
return
267267
} catch (err) {
268-
this.logger.error(err)
269-
this.clear()
268+
if (this.aborted) {
269+
this.logger.warn('upload is aborted.')
270+
this.sendLog('', -2)
271+
return
272+
}
270273

274+
this.clear()
275+
this.logger.error(err)
271276
if (err instanceof QiniuRequestError) {
272-
const reqId = this.aborted ? '' : err.reqId
273-
const code = this.aborted ? -2 : err.code
274-
this.sendLog(reqId, code)
277+
this.sendLog(err.reqId, err.code)
275278

276279
// 检查并冻结当前的 host
277280
this.checkAndFreezeHost(err)
278281

279282
const notReachRetryCount = ++this.retryCount <= this.config.retryCount
280-
const needRetry = !this.aborted && RETRY_CODE_LIST.includes(err.code)
283+
const needRetry = RETRY_CODE_LIST.includes(err.code)
281284

282285
// 以下条件满足其中之一则会进行重新上传:
283286
// 1. 满足 needRetry 的条件且 retryCount 不为 0
@@ -294,17 +297,16 @@ export default abstract class Base {
294297
}
295298

296299
private clear() {
297-
this.logger.info('start cleaning all xhr.')
298300
this.xhrList.forEach(xhr => {
299301
xhr.onreadystatechange = null
300302
xhr.abort()
301303
})
302-
this.logger.info('cleanup completed.')
303304
this.xhrList = []
305+
this.logger.info('cleanup uploading xhr.')
304306
}
305307

306308
public stop() {
307-
this.logger.info('stop.')
309+
this.logger.info('aborted.')
308310
this.clear()
309311
this.aborted = true
310312
}

src/upload/resume.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,14 @@ export default class Resume extends Base {
9292
await this.initBeforeUploadChunks()
9393

9494
const pool = new utils.Pool(
95-
(chunkInfo: ChunkInfo) => this.uploadChunk(chunkInfo),
95+
async (chunkInfo: ChunkInfo) => {
96+
if (this.aborted) {
97+
pool.abort()
98+
throw new Error('pool is aborted')
99+
}
100+
101+
await this.uploadChunk(chunkInfo)
102+
},
96103
this.config.concurrentRequestLimit
97104
)
98105

src/utils/crc32.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function mockFile(size = 4, name = 'mock.jpg', type = 'image/jpg'): File {
88
return new File([blob], name)
99
}
1010

11-
describe('test crc32', async () => {
11+
describe('test crc32', () => {
1212
test('file', async () => {
1313
const crc32One = new CRC32()
1414
await expect(crc32One.file(mockFile(0))).resolves.toEqual(0)

src/utils/pool.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface QueueContent<T> {
77
}
88

99
export class Pool<T> {
10+
aborted = false
1011
queue: Array<QueueContent<T>> = []
1112
processing: Array<QueueContent<T>> = []
1213

@@ -23,7 +24,7 @@ export class Pool<T> {
2324
})
2425
}
2526

26-
run(item: QueueContent<T>) {
27+
private run(item: QueueContent<T>) {
2728
this.queue = this.queue.filter(v => v !== item)
2829
this.processing.push(item)
2930
this.runTask(item.task).then(
@@ -36,11 +37,17 @@ export class Pool<T> {
3637
)
3738
}
3839

39-
check() {
40+
private check() {
41+
if (this.aborted) return
4042
const processingNum = this.processing.length
4143
const availableNum = this.limit - processingNum
4244
this.queue.slice(0, availableNum).forEach(item => {
4345
this.run(item)
4446
})
4547
}
48+
49+
abort() {
50+
this.queue = []
51+
this.aborted = true
52+
}
4653
}

0 commit comments

Comments
 (0)