Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix retry

Thiago Araujo da Silva296ccf97b427
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
index 97cc0fbf3b..e03d2ae735 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
@@ -95,7 +95,9 @@ export const auditLogStreamOutboxDALFactory = (db: TDbClient) => {
// Release a claim by flipping rows back to 'retry' with an updated attempt count
// and a future nextRetryAt. Used when the provider call fails for the whole batch.
- const markBatchForRetry = async (ids: number[], nextRetryAt: Date, tx?: Knex) => {
+ // nextRetryAt is computed DB-side as NOW() + delay so it uses the same clock the
+ // claim query compares against (no app/DB clock-skew drift on the retry schedule).
+ const markBatchForRetry = async (ids: number[], nextRetryDelayMs: number, tx?: Knex) => {
if (ids.length === 0) return;
try {
await (tx || db)(TableName.AuditLogStreamOutbox)
@@ -103,7 +105,7 @@ export const auditLogStreamOutboxDALFactory = (db: TDbClient) => {
.update({
status: AuditLogStreamOutboxStatus.Retry,
attempts: db.raw('"attempts" + 1'),
- nextRetryAt,
+ nextRetryAt: db.raw(`NOW() + (? || ' milliseconds')::INTERVAL`, [nextRetryDelayMs]),
lockedAt: null
});
} catch (error) {
@@ -166,22 +168,21 @@ export const auditLogStreamOutboxDALFactory = (db: TDbClient) => {
// claim on the next pass.
const commitDeliveryResult = async (input: {
successIds: number[];
- retriable: { rows: TFailedStreamRow[]; nextRetryAt: Date } | null;
+ retriable: { groups: { ids: number[]; nextRetryDelayMs: number }[] } | null;
exhausted: TFailedStreamRow[];
}) => {
- const retriableRows = input.retriable?.rows ?? [];
- if (input.successIds.length === 0 && retriableRows.length === 0 && input.exhausted.length === 0) return;
+ const retriableCount = input.retriable?.groups.reduce((n, g) => n + g.ids.length, 0) ?? 0;
+ if (input.successIds.length === 0 && retriableCount === 0 && input.exhausted.length === 0) return;
try {
await db.transaction(async (tx) => {
if (input.successIds.length > 0) {
await markBatchAsDelivered(input.successIds, tx);
}
- if (input.retriable && retriableRows.length > 0) {
- await markBatchForRetry(
- retriableRows.map(({ row }) => row.id),
- input.retriable.nextRetryAt,
- tx
- );
+ if (input.retriable) {
+ for (const group of input.retriable.groups) {
+ // eslint-disable-next-line no-await-in-loop
+ await markBatchForRetry(group.ids, group.nextRetryDelayMs, tx);
+ }
}
if (input.exhausted.length > 0) {
const rowsByError = new Map<string, TAuditLogStreamOutboxRow[]>();
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts
index 45317e6782..47581f5d89 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts
@@ -121,12 +121,11 @@ describe("audit-log-stream-outbox-service drainStream failure wiring", () => {
expect(auditLogStreamOutboxDAL.commitDeliveryResult).toHaveBeenCalledTimes(1);
const call = auditLogStreamOutboxDAL.commitDeliveryResult.mock.calls[0][0] as {
successIds: number[];
- retriable: { rows: TFailedStreamRow[]; nextRetryAt: Date } | null;
+ retriable: { groups: { ids: number[]; nextRetryDelayMs: number }[] } | null;
exhausted: TFailedStreamRow[];
};
expect(call.successIds).toEqual([]);
- expect(call.retriable?.rows.map(({ row }) => row.id)).toEqual([1]);
- expect(call.retriable?.rows[0].errorMessage).toEqual(expect.stringContaining(FAILURE_MESSAGE));
+ expect(call.retriable?.groups.flatMap((g) => g.ids)).toEqual([1]);
expect(call.exhausted).toEqual([]);
});
@@ -145,7 +144,7 @@ describe("audit-log-stream-outbox-service drainStream failure wiring", () => {
expect(auditLogStreamOutboxDAL.commitDeliveryResult).toHaveBeenCalledTimes(1);
const call = auditLogStreamOutboxDAL.commitDeliveryResult.mock.calls[0][0] as {
successIds: number[];
- retriable: { rows: TFailedStreamRow[]; nextRetryAt: Date } | null;
+ retriable: { groups: { ids: number[]; nextRetryDelayMs: number }[] } | null;
exhausted: TFailedStreamRow[];
};
expect(call.successIds).toEqual([]);
@@ -166,7 +165,7 @@ describe("audit-log-stream-outbox-service drainStream failure wiring", () => {
expect(auditLogStreamOutboxDAL.commitDeliveryResult).toHaveBeenCalledTimes(1);
const call = auditLogStreamOutboxDAL.commitDeliveryResult.mock.calls[0][0] as {
successIds: number[];
- retriable: { rows: TFailedStreamRow[]; nextRetryAt: Date } | null;
+ retriable: { groups: { ids: number[]; nextRetryDelayMs: number }[] } | null;
exhausted: TFailedStreamRow[];
};
expect(call.successIds).toEqual([1]);
@@ -202,13 +201,48 @@ describe("audit-log-stream-outbox-service drainStream failure wiring", () => {
expect(auditLogStreamOutboxDAL.commitDeliveryResult).toHaveBeenCalledTimes(1);
const call = auditLogStreamOutboxDAL.commitDeliveryResult.mock.calls[0][0] as {
successIds: number[];
- retriable: { rows: TFailedStreamRow[]; nextRetryAt: Date } | null;
+ retriable: { groups: { ids: number[]; nextRetryDelayMs: number }[] } | null;
exhausted: TFailedStreamRow[];
};
expect(call.successIds).toEqual([1]);
- expect(call.retriable?.rows.map(({ row }) => row.id)).toEqual([2]);
+ expect(call.retriable?.groups.flatMap((g) => g.ids)).toEqual([2]);
expect(call.exhausted).toEqual([]);
});
+
+ test("per-attempt backoff: rows at different attempts retry in separate groups with distinct delays", async () => {
+ const { service, auditLogStreamOutboxDAL } = createService();
+
+ // One chunk (default maxLogs) carrying two rows with different attempt counts.
+ // The single send rejects, so both are retriable but must land in separate
+ // groups, each backed off by its own attempt count — not the batch max.
+ auditLogStreamOutboxDAL.claimBatchForStream
+ .mockResolvedValueOnce([
+ buildRow({ id: 1, auditLogId: "log-1", attempts: 0 }),
+ buildRow({ id: 2, auditLogId: "log-2", attempts: 3 })
+ ])
+ .mockResolvedValueOnce([]);
+ batchStreamLog.mockRejectedValueOnce(new Error(FAILURE_MESSAGE));
+
+ await service.drainStream({ streamId: STREAM_ID, orgId: ORG_ID, provider: PROVIDER });
+
+ expect(auditLogStreamOutboxDAL.commitDeliveryResult).toHaveBeenCalledTimes(1);
+ const call = auditLogStreamOutboxDAL.commitDeliveryResult.mock.calls[0][0] as {
+ successIds: number[];
+ retriable: { groups: { ids: number[]; nextRetryDelayMs: number }[] } | null;
+ exhausted: TFailedStreamRow[];
+ };
+ const groups = call.retriable?.groups ?? [];
+ expect(groups).toHaveLength(2);
+
+ const groupForId = (id: number) => groups.find((g) => g.ids.includes(id));
+ const lowAttemptGroup = groupForId(1);
+ const highAttemptGroup = groupForId(2);
+
+ expect(lowAttemptGroup?.ids).toEqual([1]);
+ expect(highAttemptGroup?.ids).toEqual([2]);
+ // The row that has already failed more times must wait strictly longer.
+ expect(highAttemptGroup!.nextRetryDelayMs).toBeGreaterThan(lowAttemptGroup!.nextRetryDelayMs);
+ });
});
describe("audit-log-stream-outbox-service enqueueForLogs batch fanout", () => {
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
index 00fdc8c0d4..fde2db2862 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
@@ -58,11 +58,11 @@ const PROVIDER_QUEUE_MAP: Record<LogProvider, QueueName> = {
const LAST_ERROR_MAX_LENGTH = 1_000;
-const computeBackoff = (attemptsAfterIncrement: number): Date => {
+const computeBackoffMs = (attemptsAfterIncrement: number): number => {
const exponent = Math.min(attemptsAfterIncrement - 1, 10);
const base = Math.min(BACKOFF_BASE_MS * 2 ** exponent, BACKOFF_MAX_MS);
const jitter = Math.floor(Math.random() * Math.min(base / 2, 30_000));
- return new Date(Date.now() + base + jitter);
+ return base + jitter;
};
const formatErrorMessage = (error: unknown): string => {
@@ -249,10 +249,18 @@ export const auditLogStreamOutboxServiceFactory = ({
}
}
- const retriableInput = retriable.length
+ const retriableByAttempts = new Map<number, number[]>();
+ for (const { row } of retriable) {
+ const ids = retriableByAttempts.get(row.attempts);
+ if (ids) ids.push(row.id);
+ else retriableByAttempts.set(row.attempts, [row.id]);
+ }
+ const retriableInput = retriableByAttempts.size
? {
- rows: retriable,
- nextRetryAt: computeBackoff(Math.max(...retriable.map(({ row }) => row.attempts + 1)))
+ groups: Array.from(retriableByAttempts, ([attempts, ids]) => ({
+ ids,
+ nextRetryDelayMs: computeBackoffMs(attempts + 1)
+ }))
}
: null;