Skip to content

Commit b60698e

Browse files
committed
添加重试场景
1 parent 00aec82 commit b60698e

File tree

6 files changed

+43
-38
lines changed

6 files changed

+43
-38
lines changed

src/upload/base.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { Host, HostPool } from './hosts'
66

77
export const DEFAULT_CHUNK_SIZE = 4 // 单位 MB
88

9+
export const RETRY_CODE = [502, 503, 504, 599]
10+
911
/** 上传文件的资源信息配置 */
1012
export interface Extra {
1113
/** 文件原文件名 */
@@ -186,6 +188,7 @@ export default abstract class Base {
186188

187189
// 检查并更新解冻当前的 host
188190
protected checkAndUnfreezeHost() {
191+
this.logger.info('check unfreeze host.')
189192
if (this.uploadHost != null && this.uploadHost.isFrozen()) {
190193
this.uploadHost.unfreeze()
191194
this.logger.warn(`${this.uploadHost.host} will be unfreeze.`)
@@ -194,10 +197,11 @@ export default abstract class Base {
194197

195198
// 检查并更新冻结当前的 host
196199
private checkAndFreezeHost(error: utils.QiniuError) {
197-
if (utils.isResponseError(error)) {
198-
if ([502, 503, 504, 599].includes(error.code)) {
199-
this.uploadHost.freeze()
200+
this.logger.info('check freeze host.')
201+
if (utils.isResponseError(error) && this.uploadHost != null) {
202+
if (RETRY_CODE.includes(error.code)) {
200203
this.logger.warn(`${this.uploadHost.host} will be temporarily frozen.`)
204+
this.uploadHost.freeze()
201205
}
202206
}
203207
}
@@ -256,8 +260,8 @@ export default abstract class Base {
256260

257261
// 检查并冻结当前的 host
258262
this.checkAndFreezeHost(err)
259-
const needRetry = err.isRequestError && err.code === 0 && !this.aborted
260263
const notReachRetryCount = ++this.retryCount <= this.config.retryCount
264+
const needRetry = err.isRequestError && !this.aborted && [0, ...RETRY_CODE].includes(err.code)
261265

262266
// 以下条件满足其中之一则会进行重新上传:
263267
// 1. 满足 needRetry 的条件且 retryCount 不为 0

src/upload/hosts.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ export class HostPool {
6767
* @param {string[]} hosts
6868
* @description 如果在构造时传入 hosts,则该 host 池始终使用传入的 host 做为可用的数据
6969
*/
70-
constructor(hosts?: string[]) {
70+
constructor(hosts?: string[] | string) {
7171
if (hosts == null) return
72-
this.hosts = hosts
72+
if (Array.isArray(hosts)) this.hosts = hosts
73+
if (typeof hosts === 'string') this.hosts = [hosts]
7374
}
7475

7576
/**

src/upload/index.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import Resume from './resume'
22
import Direct from './direct'
33
import Logger from '../logger'
4-
import { regionUphostMap } from '../config'
54
import { UploadCompleteData } from '../api'
6-
import { QiniuError, Observable, IObserver, MB } from '../utils'
5+
import { QiniuError, Observable, IObserver, MB, normalizeUploadConfig } from '../utils'
76

87
import { Config, Extra, UploadHandler, UploadOptions, UploadProgress } from './base'
98
import { HostPool } from './hosts'
@@ -52,32 +51,11 @@ export default function upload(
5251
key,
5352
token,
5453
putExtra,
55-
config
56-
}
57-
58-
const hostList: string[] = []
59-
60-
// 如果用户传了 region,添加指定 region 的 host 到可用 host 列表
61-
if (config?.region) {
62-
const hostMap = regionUphostMap[config?.region]
63-
if (config.useCdnDomain) {
64-
hostList.push(hostMap.cdnUphost)
65-
} else {
66-
hostList.push(hostMap.srcUphost)
67-
}
68-
}
69-
70-
// 如果同时指定了 uphost 参数,添加到可用 host 列表
71-
if (config?.uphost != null) {
72-
if (Array.isArray(config?.uphost)) {
73-
hostList.push(...config?.uphost)
74-
} else {
75-
hostList.push(config?.uphost)
76-
}
54+
config: normalizeUploadConfig(config)
7755
}
7856

7957
// 创建 host 池
80-
const hostPool = new HostPool(hostList)
58+
const hostPool = new HostPool(options.config?.uphost)
8159

8260
// 为每个任务创建单独的 Logger
8361
const logger = new Logger(options.token, config?.disableStatisticsReport, config?.debugLogLevel)

src/upload/resume.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export default class Resume extends Base {
187187
}
188188
)
189189

190-
this.logger.info('finishResumeProgress.')
190+
this.logger.info('finish Resume Progress.')
191191
this.updateMkFileProgress(1)
192192
this.checkAndUnfreezeHost()
193193
return result

src/utils/config.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Config, DEFAULT_CHUNK_SIZE } from 'upload'
1+
import { regionUphostMap } from '../config'
2+
import { Config, DEFAULT_CHUNK_SIZE } from '../upload'
23

3-
export function normalizeUploadConfig(config: Config): Config {
4-
if (config == null) return config
5-
const { upprotocol } = config
4+
export function normalizeUploadConfig(config?: Partial<Config>): Config {
5+
const { upprotocol } = { ...config }
66

7-
const normalizeData: Config = {
7+
const normalizeConfig: Config = {
88
uphost: '',
99
retryCount: 3,
1010

@@ -21,5 +21,26 @@ export function normalizeUploadConfig(config: Config): Config {
2121
disableStatisticsReport: false
2222
}
2323

24-
return { ...normalizeData, ...config }
24+
const hostList: string[] = []
25+
26+
// 如果用户传了 region,添加指定 region 的 host 到可用 host 列表
27+
if (config?.region) {
28+
const hostMap = regionUphostMap[config?.region]
29+
if (config.useCdnDomain) {
30+
hostList.push(hostMap.cdnUphost)
31+
} else {
32+
hostList.push(hostMap.srcUphost)
33+
}
34+
}
35+
36+
// 如果同时指定了 uphost 参数,添加到可用 host 列表
37+
if (config?.uphost != null) {
38+
if (Array.isArray(config?.uphost)) {
39+
hostList.push(...config?.uphost)
40+
} else {
41+
hostList.push(config?.uphost)
42+
}
43+
}
44+
45+
return { ...config, ...normalizeConfig, uphost: hostList }
2546
}

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export * from './observable'
33

44
export * from './base64'
55
export * from './helper'
6+
export * from './config'
67

78
export { default as compressImage, CompressResult } from './compress'

0 commit comments

Comments
 (0)