Skip to content

Commit e5214b0

Browse files
committed
refactor(channels): adopt core retryAsync for hand-rolled retry loops
1 parent b4a9a4c commit e5214b0

2 files changed

Lines changed: 38 additions & 58 deletions

File tree

‎extensions/feishu/src/comment-shared.ts‎

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Feishu plugin module implements comment shared behavior.
2+
import { retryAsync } from "openclaw/plugin-sdk/retry-runtime";
23
import {
34
isRecord as sharedIsRecord,
45
normalizeOptionalString,
@@ -142,56 +143,39 @@ export async function requestFeishuApi<T>(
142143
options: {
143144
includeConfigParams?: boolean;
144145
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 */
146147
retryDelayMs?: number;
147148
} = {},
148149
): 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+
);
170164
}
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+
},
190175
);
191-
throw createFeishuApiError(synthetic, errorPrefix, options);
176+
} catch (error) {
177+
throw createFeishuApiError(error, errorPrefix, options);
192178
}
193-
// Unreachable: every iteration either returns or throws. Required for TypeScript exhaustiveness.
194-
throw createFeishuApiError(new Error("unreachable"), errorPrefix, options);
195179
}
196180

197181
type ParsedCommentDocumentRef = {

‎extensions/matrix/src/approval-handler.runtime.ts‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Matrix plugin module implements approval handler behavior.
2-
import { setTimeout as sleep } from "node:timers/promises";
32
import type {
43
ChannelApprovalCapabilityHandlerContext,
54
PendingApprovalView,
@@ -21,6 +20,7 @@ import {
2120
listMessageReceiptPlatformIds,
2221
resolveMessageReceiptPrimaryId,
2322
} from "openclaw/plugin-sdk/channel-outbound";
23+
import { retryAsync } from "openclaw/plugin-sdk/retry-runtime";
2424
import { normalizeUniqueStringEntries } from "openclaw/plugin-sdk/string-coerce-runtime";
2525
import {
2626
buildMatrixApprovalReactionHint,
@@ -184,19 +184,15 @@ async function retryMatrixApprovalDelivery<T>(
184184
operation: () => Promise<T>,
185185
params: { shouldRetry?: (error: unknown) => boolean } = {},
186186
): Promise<T> {
187-
let lastError: unknown;
188-
for (let attempt = 1; attempt <= MATRIX_APPROVAL_DELIVERY_ATTEMPTS; attempt += 1) {
189-
try {
190-
return await operation();
191-
} catch (error) {
192-
lastError = error;
193-
if (attempt === MATRIX_APPROVAL_DELIVERY_ATTEMPTS || params.shouldRetry?.(error) === false) {
194-
break;
195-
}
196-
await sleep(MATRIX_APPROVAL_DELIVERY_RETRY_DELAY_MS * attempt);
197-
}
198-
}
199-
throw lastError;
187+
// With a 3-attempt budget the core exponential schedule (250ms, 500ms)
188+
// matches the previous linear attempt*250ms backoff exactly; revisit the
189+
// delay curve if MATRIX_APPROVAL_DELIVERY_ATTEMPTS grows.
190+
return await retryAsync(operation, {
191+
attempts: MATRIX_APPROVAL_DELIVERY_ATTEMPTS,
192+
minDelayMs: MATRIX_APPROVAL_DELIVERY_RETRY_DELAY_MS,
193+
// Deliveries default to retryable; callers opt out per error class.
194+
shouldRetry: (error) => params.shouldRetry?.(error) !== false,
195+
});
200196
}
201197

202198
async function prepareTarget(

0 commit comments

Comments
 (0)