|
1 | 1 | // Feishu plugin module implements comment shared behavior. |
| 2 | +import { retryAsync } from "openclaw/plugin-sdk/retry-runtime"; |
2 | 3 | import { |
3 | 4 | isRecord as sharedIsRecord, |
4 | 5 | normalizeOptionalString, |
@@ -142,56 +143,39 @@ export async function requestFeishuApi<T>( |
142 | 143 | options: { |
143 | 144 | includeConfigParams?: boolean; |
144 | 145 | includeNestedErrorLogId?: boolean; |
145 | | - /** Base delay per retry attempt in ms; multiplied by attempt index. @internal */ |
| 146 | + /** Base retry delay in ms; doubles on the second retry. @internal */ |
146 | 147 | retryDelayMs?: number; |
147 | 148 | } = {}, |
148 | 149 | ): Promise<T> { |
149 | | - const retryDelayMs = options.retryDelayMs ?? FEISHU_SEND_RETRY_BASE_MS; |
150 | | - let lastFulfilledRateLimit: { response: unknown; code: number } | undefined; |
151 | | - for (let attempt = 0; attempt <= FEISHU_SEND_MAX_RETRIES; attempt++) { |
152 | | - if (attempt > 0) { |
153 | | - // Linear backoff: delay grows with each attempt to give the rate-limit window time to reset. |
154 | | - await new Promise<void>((resolve) => { |
155 | | - setTimeout(resolve, attempt * retryDelayMs); |
156 | | - }); |
157 | | - } |
158 | | - try { |
159 | | - const result = await request(); |
160 | | - // Feishu SDK may fulfill with a rate-limit body (e.g. { code: 11232, ... }) |
161 | | - // instead of throwing. Classify before returning so retry covers both shapes. |
162 | | - const fulfilledRateLimit = getFeishuSendRateLimitCodeFromResponse(result); |
163 | | - if (fulfilledRateLimit !== undefined) { |
164 | | - // Capture for the synthetic-error path below; on a non-final attempt |
165 | | - // continue retrying, on the final attempt fall through so the loop |
166 | | - // exits and the wrapped exhaustion error is thrown. |
167 | | - lastFulfilledRateLimit = { response: result, code: fulfilledRateLimit }; |
168 | | - if (attempt < FEISHU_SEND_MAX_RETRIES) { |
169 | | - continue; |
| 150 | + try { |
| 151 | + return await retryAsync( |
| 152 | + async () => { |
| 153 | + const result = await request(); |
| 154 | + // Feishu SDK may fulfill with a rate-limit body (e.g. { code: 11232, ... }) |
| 155 | + // instead of throwing. Rethrow it in the AxiosError response shape so |
| 156 | + // getFeishuSendRateLimitCode classifies it retryable and exhaustion |
| 157 | + // wraps it exactly like an SDK throw. |
| 158 | + const fulfilledRateLimit = getFeishuSendRateLimitCodeFromResponse(result); |
| 159 | + if (fulfilledRateLimit !== undefined) { |
| 160 | + throw Object.assign( |
| 161 | + new Error(`Request fulfilled with rate-limit code ${fulfilledRateLimit}`), |
| 162 | + { response: { status: 200, data: result } }, |
| 163 | + ); |
170 | 164 | } |
171 | | - break; |
172 | | - } |
173 | | - return result; |
174 | | - } catch (error) { |
175 | | - const isRetryable = |
176 | | - attempt < FEISHU_SEND_MAX_RETRIES && getFeishuSendRateLimitCode(error) !== undefined; |
177 | | - if (!isRetryable) { |
178 | | - throw createFeishuApiError(error, errorPrefix, options); |
179 | | - } |
180 | | - // Rate-limit on a non-final attempt — loop continues to next retry. |
181 | | - } |
182 | | - } |
183 | | - // Exhausted retries while the SDK kept fulfilling rate-limit bodies. Surface |
184 | | - // the last response as an error so callers see the same wrapped shape they |
185 | | - // would have seen if the SDK had thrown. |
186 | | - if (lastFulfilledRateLimit) { |
187 | | - const synthetic = Object.assign( |
188 | | - new Error(`Request fulfilled with rate-limit code ${lastFulfilledRateLimit.code}`), |
189 | | - { response: { status: 200, data: lastFulfilledRateLimit.response } }, |
| 165 | + return result; |
| 166 | + }, |
| 167 | + { |
| 168 | + attempts: FEISHU_SEND_MAX_RETRIES + 1, |
| 169 | + // With a 2-retry budget the core exponential schedule (1x, 2x base) |
| 170 | + // matches the previous linear attempt*base backoff exactly; revisit |
| 171 | + // the delay curve if FEISHU_SEND_MAX_RETRIES grows. |
| 172 | + minDelayMs: options.retryDelayMs ?? FEISHU_SEND_RETRY_BASE_MS, |
| 173 | + shouldRetry: (error) => getFeishuSendRateLimitCode(error) !== undefined, |
| 174 | + }, |
190 | 175 | ); |
191 | | - throw createFeishuApiError(synthetic, errorPrefix, options); |
| 176 | + } catch (error) { |
| 177 | + throw createFeishuApiError(error, errorPrefix, options); |
192 | 178 | } |
193 | | - // Unreachable: every iteration either returns or throws. Required for TypeScript exhaustiveness. |
194 | | - throw createFeishuApiError(new Error("unreachable"), errorPrefix, options); |
195 | 179 | } |
196 | 180 |
|
197 | 181 | type ParsedCommentDocumentRef = { |
|
0 commit comments