-
Notifications
You must be signed in to change notification settings - Fork 516
加入单元测试和并发控制,解决页面卡顿 #350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
加入单元测试和并发控制,解决页面卡顿 #350
Conversation
winddies
commented
Mar 19, 2018
- 解决demo页面因更新chunk进度导致的卡顿问题
- 加入分片上传请求的并发控制
- 加入单元测试
README.md
Outdated
@@ -170,6 +170,8 @@ subscription.unsubscribe() // 上传取消 | |||
* config.disableStatisticsReport: 是否禁用日志报告,为布尔值,默认为 `false`。 | |||
* config.region: 选择上传域名区域;当为 `null` 或 `undefined` 时,自动分析上传域名区域。 | |||
* config.retryCount: 上传自动重试次数(整体重试次数,而不是某个分片的重试次数);默认 3 次(即上传失败后最多重试两次);**目前仅在上传过程中产生 `599` 内部错误时生效**,**但是未来很可能会扩展为支持更多的情况**。 | |||
* config.thread: 分片上传的并发请求量,`number`,默认为3;因为浏览器本身也会限制最大并发量,所以最大并发量与浏览器有关。 | |||
* config.md5: md5 校验,为布尔值,默认 `false`;为 `true` 时,会对分片进行 md5 校验,在断点续传时,若分片 md5 值与断点储存的 md5 不一致,则重传文件。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
解释一下为什么默认是 false
吧
package.json
Outdated
@@ -1,12 +1,13 @@ | |||
{ | |||
"name": "qiniu-js", | |||
"jsName": "qiniu", | |||
"version": "2.1.3", | |||
"version": "2.1.4", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2.2.0
src/pool.js
Outdated
insertQueue(task){ | ||
return new Promise((resolve, reject) => { | ||
this.queue.push({ | ||
...task, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果 task
里有个字段 resolve
,这里就有问题了吧;这边不用把 task
展开的,直接拿 { task, resolve, reject }
作为一个 item 就好了
src/pool.js
Outdated
toDo(){ | ||
let onDoingNum = this.onDoing.length; | ||
let availableNum = this.limit - onDoingNum; | ||
this.onDoing.forEach(task => this.queue = this.queue.filter(item => item !== task)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这事儿为什么在这做啊,为什么不是放在 start
里,this.onDoing.push(task);
之后做?不然在这两行代码运行的间隔里,数据处于一个不一致/合法的状态(onDoing
里跟 queue
里都有)
src/pool.js
Outdated
constructor(doTask, limit) { | ||
this.doTask = doTask; | ||
this.queue = []; | ||
this.onDoing = []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么是 onDoing
,一般 onFoo
这种命名不都是表示这是个处理 foo
事件的 handler 嘛?
src/upload.js
Outdated
} else { | ||
this.sendLog(err.reqId, err.code); | ||
} | ||
} | ||
if (err.code === 599 && ++this.retryCount < this.config.retryCount) { | ||
|
||
if (err.code === 0 && !this.abort){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
应对 599?注释说明
src/upload.js
Outdated
if (err.code === 599 && ++this.retryCount < this.config.retryCount) { | ||
|
||
if (err.code === 0 && !this.abort){ | ||
if (--this.config.retryCount >= 0){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
维护 this.tryCount
src/upload.js
Outdated
} | ||
} | ||
|
||
if (err.code === 701 || (err.message && err.message === "md5不一致")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
701 是啥,注释说明下?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
外面不需要接触 md5 的逻辑
src/pool.js
Outdated
this.abort = false; | ||
} | ||
|
||
insertQueue(task){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
insertQueue
-> enqueue
start
-> run
doTask
-> runTask
todo
-> check
?
README.md
Outdated
@@ -170,6 +170,8 @@ subscription.unsubscribe() // 上传取消 | |||
* config.disableStatisticsReport: 是否禁用日志报告,为布尔值,默认为 `false`。 | |||
* config.region: 选择上传域名区域;当为 `null` 或 `undefined` 时,自动分析上传域名区域。 | |||
* config.retryCount: 上传自动重试次数(整体重试次数,而不是某个分片的重试次数);默认 3 次(即上传失败后最多重试两次);**目前仅在上传过程中产生 `599` 内部错误时生效**,**但是未来很可能会扩展为支持更多的情况**。 | |||
* config.thread: 分片上传的并发请求量,`number`,默认为3;因为浏览器本身也会限制最大并发量,所以最大并发量与浏览器有关。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pool 那边改成 limit 了,这边就改成 concurrentRequestLimit
吧
README.md
Outdated
@@ -170,6 +170,8 @@ subscription.unsubscribe() // 上传取消 | |||
* config.disableStatisticsReport: 是否禁用日志报告,为布尔值,默认为 `false`。 | |||
* config.region: 选择上传域名区域;当为 `null` 或 `undefined` 时,自动分析上传域名区域。 | |||
* config.retryCount: 上传自动重试次数(整体重试次数,而不是某个分片的重试次数);默认 3 次(即上传失败后最多重试两次);**目前仅在上传过程中产生 `599` 内部错误时生效**,**但是未来很可能会扩展为支持更多的情况**。 | |||
* config.thread: 分片上传的并发请求量,`number`,默认为3;因为浏览器本身也会限制最大并发量,所以最大并发量与浏览器有关。 | |||
* config.md5: md5 校验,为布尔值;在断点续传时,开启md5校验会对已上传的也进行md5计算比对,所以会增加上传时间,默认 `false`;为 `true` 时,会对分片进行 md5 校验,在断点续传时,若分片 md5 值与断点储存的 md5 不一致,则重传该分片。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
md5
-> checkByMD5
?
src/upload.js
Outdated
@@ -42,6 +47,7 @@ export class UploadManager { | |||
this.progress = null; | |||
this.xhrList = []; | |||
this.xhrHandler = xhr => this.xhrList.push(xhr); | |||
this.abort = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
abort
-> aborted
README.md
Outdated
@@ -170,6 +170,8 @@ subscription.unsubscribe() // 上传取消 | |||
* config.disableStatisticsReport: 是否禁用日志报告,为布尔值,默认为 `false`。 | |||
* config.region: 选择上传域名区域;当为 `null` 或 `undefined` 时,自动分析上传域名区域。 | |||
* config.retryCount: 上传自动重试次数(整体重试次数,而不是某个分片的重试次数);默认 3 次(即上传失败后最多重试两次);**目前仅在上传过程中产生 `599` 内部错误时生效**,**但是未来很可能会扩展为支持更多的情况**。 | |||
* config.currentRequestLimit: 分片上传的并发请求量,`number`,默认为3;因为浏览器本身也会限制最大并发量,所以最大并发量与浏览器有关。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不是 current
,是 concurrent
...
src/upload.js
Outdated
} | ||
|
||
let needRetry = err.isRequestError && err.code === 0 && !this.aborted; | ||
let reachRetryCount = ++this.retryCount <= this.config.retryCount; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个明显是 have not reached retry count 啊...
test/demo1/scripts/uploadWithSDK.js
Outdated
@@ -42,11 +47,21 @@ function uploadWithSDK(token, putExtra, config, domain) { | |||
imageDeal(board, res.key, domain); | |||
} | |||
}; | |||
|
|||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
空格
src/upload.js
Outdated
removeLocalFileInfo(this.file); | ||
}, | ||
err => { | ||
// ctx错误或者过期情况下,或者md5不匹配时,清除本地存储数据 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
“或者md5不匹配”?
README.md
Outdated
@@ -170,6 +170,8 @@ subscription.unsubscribe() // 上传取消 | |||
* config.disableStatisticsReport: 是否禁用日志报告,为布尔值,默认为 `false`。 | |||
* config.region: 选择上传域名区域;当为 `null` 或 `undefined` 时,自动分析上传域名区域。 | |||
* config.retryCount: 上传自动重试次数(整体重试次数,而不是某个分片的重试次数);默认 3 次(即上传失败后最多重试两次);**目前仅在上传过程中产生 `599` 内部错误时生效**,**但是未来很可能会扩展为支持更多的情况**。 | |||
* config.currentRequestLimit: 分片上传的并发请求量,`number`,默认为3;因为浏览器本身也会限制最大并发量,所以最大并发量与浏览器有关。 | |||
* config.checkByMD5: md5 校验,为布尔值;在断点续传时,开启 md5 校验会对已上传的进行 md5 计算比对,防止文件被修改,若分片 md5 值与断点储存的 md5 不一致,则重传该分片,所以会增加上传时间,默认 `false`。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- config.checkByMD5: 是否开启 MD5 校验,为布尔值;在断点续传时,开启 MD5 校验会将已上传的分片与当前分片进行 MD5 值比对,若不一致,则重传该分片,避免使用错误的分片。读取分片内容并计算 MD5 需要花费一定的时间,因此会稍微增加断点续传时的耗时。默认为
false
,不开启。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚗