Skip to content

Commit

Permalink
[KODO-15365] 修复偶发性无法停止任务的 BUG (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
yinxulai authored Jun 22, 2022
1 parent 3f5526f commit e180bea
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "qiniu-js",
"jsName": "qiniu",
"version": "3.4.0",
"version": "3.4.1",
"private": false,
"description": "Javascript SDK for Qiniu Resource (Cloud) Storage AP",
"main": "lib/index.js",
Expand Down
1 change: 0 additions & 1 deletion site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"byte-size": "^7.0.1",
"create-hmac": "^1.1.7",
"js-base64": "^3.7.2",
"qiniu-js": "^3.4.0",
"react": "^17.0.2",
"react-dom": "^17.0.2"
}
Expand Down
5 changes: 3 additions & 2 deletions site/src/upload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react'
import { upload } from 'qiniu-js'
import { UploadProgress } from 'qiniu-js/esm/upload'

import { upload } from '../../src'
import { UploadProgress } from '../../src/upload'

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

Expand Down
32 changes: 0 additions & 32 deletions site/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@
chalk "^2.0.0"
js-tokens "^4.0.0"

"@babel/runtime-corejs2@^7.10.2":
version "7.14.0"
resolved "https://registry.yarnpkg.com/@babel/runtime-corejs2/-/runtime-corejs2-7.14.0.tgz#5519b92ccc819bd3d0a1ecb25ab3cb5a775485f8"
integrity sha512-btR4E8JiGlmmDI5YgirlG9z3T91rBdxnVh2YuEStrHDcekffaaIeK+CE0S4IaYUyYhMa7rFDfF2GEO79XNbLEA==
dependencies:
core-js "^2.6.5"
regenerator-runtime "^0.13.4"

"@babel/runtime-corejs3@^7.10.2":
version "7.14.0"
resolved "https://registry.yarnpkg.com/@babel/runtime-corejs3/-/runtime-corejs3-7.14.0.tgz#6bf5fbc0b961f8e3202888cb2cd0fb7a0a9a3f66"
Expand Down Expand Up @@ -1077,11 +1069,6 @@ core-js-pure@^3.0.0:
resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.12.1.tgz#934da8b9b7221e2a2443dc71dfa5bd77a7ea00b8"
integrity sha512-1cch+qads4JnDSWsvc7d6nzlKAippwjUlf6vykkTLW53VSV+NkE6muGBToAjEA8pG90cSfcud3JgVmW2ds5TaQ==

core-js@^2.6.5:
version "2.6.12"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.12.tgz#d9333dfa7b065e347cc5682219d6f690859cc2ec"
integrity sha512-Kb2wC0fvsWfQrgk8HU5lW6U/Lcs8+9aaYcy4ZFc6DDlo4nZ7n70dEgE5rtR0oG6ufKDUnrwfWL1mXR5ljDatrQ==

core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
Expand Down Expand Up @@ -3810,15 +3797,6 @@ punycode@^2.1.0:
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==

qiniu-js@^3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/qiniu-js/-/qiniu-js-3.4.0.tgz#36980de07be4bab229ec0193a32c3318aa24b748"
integrity sha512-QJ5ShXQkyPuhMXEruyLza3V++8crvDNLTFOXzuMXrnXCvwtEbP+MZyYo4D96HRXnSFb6lAJ9s2QdzW1W3LBiIQ==
dependencies:
"@babel/runtime-corejs2" "^7.10.2"
querystring "^0.2.1"
spark-md5 "^3.0.0"

[email protected]:
version "6.7.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
Expand All @@ -3829,11 +3807,6 @@ [email protected]:
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620"
integrity sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=

querystring@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.1.tgz#40d77615bb09d16902a85c3e38aa8b5ed761c2dd"
integrity sha512-wkvS7mL/JMugcup3/rMitHmd9ecIGd2lhFhK9N3UUQ450h66d1r3Y9nvXzQAW1Lq+wyx61k/1pfKS5KuKiyEbg==

querystringify@^2.1.1:
version "2.2.0"
resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.2.0.tgz#3345941b4153cb9d082d8eee4cda2016a9aef7f6"
Expand Down Expand Up @@ -4439,11 +4412,6 @@ source-map@~0.7.2:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==

spark-md5@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/spark-md5/-/spark-md5-3.0.1.tgz#83a0e255734f2ab4e5c466e5a2cfc9ba2aa2124d"
integrity sha512-0tF3AGSD1ppQeuffsLDIOWlKUd3lS92tFxcsrh5Pe3ZphhnoK+oXIBTzOAThZCiuINZLvpiLH/1VS1/ANEJVig==

spdx-correct@^3.0.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.1.1.tgz#dece81ac9c1e6713e5f7d1b6f17d468fa53d89a9"
Expand Down
20 changes: 11 additions & 9 deletions src/upload/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,19 +265,22 @@ export default abstract class Base {
this.sendLog(result.reqId, 200)
return
} catch (err) {
this.logger.error(err)
this.clear()
if (this.aborted) {
this.logger.warn('upload is aborted.')
this.sendLog('', -2)
return
}

this.clear()
this.logger.error(err)
if (err instanceof QiniuRequestError) {
const reqId = this.aborted ? '' : err.reqId
const code = this.aborted ? -2 : err.code
this.sendLog(reqId, code)
this.sendLog(err.reqId, err.code)

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

const notReachRetryCount = ++this.retryCount <= this.config.retryCount
const needRetry = !this.aborted && RETRY_CODE_LIST.includes(err.code)
const needRetry = RETRY_CODE_LIST.includes(err.code)

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

private clear() {
this.logger.info('start cleaning all xhr.')
this.xhrList.forEach(xhr => {
xhr.onreadystatechange = null
xhr.abort()
})
this.logger.info('cleanup completed.')
this.xhrList = []
this.logger.info('cleanup uploading xhr.')
}

public stop() {
this.logger.info('stop.')
this.logger.info('aborted.')
this.clear()
this.aborted = true
}
Expand Down
9 changes: 8 additions & 1 deletion src/upload/resume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ export default class Resume extends Base {
await this.initBeforeUploadChunks()

const pool = new utils.Pool(
(chunkInfo: ChunkInfo) => this.uploadChunk(chunkInfo),
async (chunkInfo: ChunkInfo) => {
if (this.aborted) {
pool.abort()
throw new Error('pool is aborted')
}

await this.uploadChunk(chunkInfo)
},
this.config.concurrentRequestLimit
)

Expand Down
2 changes: 1 addition & 1 deletion src/utils/crc32.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function mockFile(size = 4, name = 'mock.jpg', type = 'image/jpg'): File {
return new File([blob], name)
}

describe('test crc32', async () => {
describe('test crc32', () => {
test('file', async () => {
const crc32One = new CRC32()
await expect(crc32One.file(mockFile(0))).resolves.toEqual(0)
Expand Down
11 changes: 9 additions & 2 deletions src/utils/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface QueueContent<T> {
}

export class Pool<T> {
aborted = false
queue: Array<QueueContent<T>> = []
processing: Array<QueueContent<T>> = []

Expand All @@ -23,7 +24,7 @@ export class Pool<T> {
})
}

run(item: QueueContent<T>) {
private run(item: QueueContent<T>) {
this.queue = this.queue.filter(v => v !== item)
this.processing.push(item)
this.runTask(item.task).then(
Expand All @@ -36,11 +37,17 @@ export class Pool<T> {
)
}

check() {
private check() {
if (this.aborted) return
const processingNum = this.processing.length
const availableNum = this.limit - processingNum
this.queue.slice(0, availableNum).forEach(item => {
this.run(item)
})
}

abort() {
this.queue = []
this.aborted = true
}
}

0 comments on commit e180bea

Please sign in to comment.