RetryManager.js
4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* @Author: AI Assistant
* @Date: 2025-01-28
* @Description: 重试管理器 - 处理上传失败的自动重试逻辑
* @Feature: oss-upload-refactor
*/
/**
* 重试管理器
*
* 功能:
* 1. 执行带重试逻辑的异步函数
* 2. 支持指数退避策略
* 3. 支持自定义重试条件
* 4. 支持重试回调
*
* 设计原则:
* - 通用性:可用于任何异步函数的重试
* - 灵活性:支持自定义重试策略和条件
*/
class RetryManager {
/**
* 执行带重试逻辑的函数
*
* @param {Function} fn - 要执行的异步函数
* @param {Object} options - 重试选项
* @param {number} [options.maxRetries=3] - 最大重试次数
* @param {Function|number} [options.retryDelay] - 重试延迟(毫秒)或延迟计算函数
* @param {Function} [options.shouldRetry] - 判断是否应该重试的函数
* @param {Function} [options.onRetry] - 重试时的回调函数
* @returns {Promise} 函数执行结果
* @throws {Error} 所有重试都失败后抛出最后一次的错误
*
* @example
* // 基本用法
* await retryManager.executeWithRetry(
* () => uploadFile(file),
* { maxRetries: 3 }
* )
*
* @example
* // 自定义重试策略
* await retryManager.executeWithRetry(
* () => uploadFile(file),
* {
* maxRetries: 3,
* retryDelay: (attempt) => Math.pow(2, attempt) * 1000, // 指数退避
* shouldRetry: (error) => !error.message.includes('cancelled'),
* onRetry: (attempt, error) => console.log(`Retry ${attempt}: ${error.message}`)
* }
* )
*/
async executeWithRetry(fn, options = {}) {
const {
maxRetries = 3,
retryDelay = (attempt) => attempt * 1000,
shouldRetry = () => true,
onRetry = () => {}
} = options
let lastError
// 尝试执行函数(初次 + 重试)
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
// 执行函数
const result = await fn()
// 成功,返回结果
if (attempt > 0) {
uni.$logger?.local?.info(`[RetryManager] Success after ${attempt} retries`)
}
return result
} catch (error) {
lastError = error
// 判断是否应该重试
const isLastAttempt = attempt >= maxRetries
const canRetry = shouldRetry(error)
if (!isLastAttempt && canRetry) {
// 计算延迟时间
const delay = typeof retryDelay === 'function'
? retryDelay(attempt)
: retryDelay
// 调用重试回调
onRetry(attempt + 1, error)
uni.$logger?.local?.warn(`[RetryManager] Retry ${attempt + 1}/${maxRetries} after ${delay}ms: ${error.message}`)
// 等待后重试
await this.sleep(delay)
} else {
// 不再重试,跳出循环
if (isLastAttempt) {
uni.$logger?.local?.error(`[RetryManager] All ${maxRetries} retries exhausted`)
} else {
uni.$logger?.local?.error(`[RetryManager] Retry skipped: ${error.message}`)
}
break
}
}
}
// 所有重试都失败,抛出最后一次的错误
throw lastError
}
/**
* 延迟执行
*
* @param {number} ms - 延迟时间(毫秒)
* @returns {Promise<void>}
*/
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
/**
* 创建指数退避延迟函数
*
* @param {number} baseDelay - 基础延迟时间(毫秒)
* @param {number} maxDelay - 最大延迟时间(毫秒)
* @returns {Function} 延迟计算函数
*
* @example
* const retryDelay = RetryManager.createExponentialBackoff(1000, 10000)
* // attempt 0: 1000ms
* // attempt 1: 2000ms
* // attempt 2: 4000ms
* // attempt 3: 8000ms
* // attempt 4: 10000ms (capped)
*/
static createExponentialBackoff(baseDelay = 1000, maxDelay = 60000) {
return (attempt) => {
const delay = baseDelay * Math.pow(2, attempt)
return Math.min(delay, maxDelay)
}
}
/**
* 创建线性延迟函数
*
* @param {number} baseDelay - 基础延迟时间(毫秒)
* @returns {Function} 延迟计算函数
*
* @example
* const retryDelay = RetryManager.createLinearBackoff(1000)
* // attempt 0: 1000ms
* // attempt 1: 2000ms
* // attempt 2: 3000ms
*/
static createLinearBackoff(baseDelay = 1000) {
return (attempt) => baseDelay * (attempt + 1)
}
/**
* 创建固定延迟函数
*
* @param {number} delay - 固定延迟时间(毫秒)
* @returns {Function} 延迟计算函数
*
* @example
* const retryDelay = RetryManager.createFixedDelay(2000)
* // 每次重试都延迟 2000ms
*/
static createFixedDelay(delay = 1000) {
return () => delay
}
}
// 导出管理器类
export default RetryManager