Skip to content

Commit 017e7f5

Browse files
committed
Add retry429 helper function
The idea is that this provides a generalized way to retry a 429 error, with exponential backoff.
1 parent eb09541 commit 017e7f5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/gcp/retry429.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as clc from "colorette";
2+
import * as utils from "../utils";
3+
import { withHttpBackoff } from "./retryWithBackoff";
4+
5+
/**
6+
* Convenience wrapper for Cloud Functions deploy operations.
7+
* Retries **only** on HTTP 429 and logs a clear message.
8+
*/
9+
export function with429Backoff<T>(
10+
op: "create" | "update" | "delete" | "generateUploadUrl",
11+
resourceName: string,
12+
thunk: () => Promise<T>,
13+
) {
14+
return withHttpBackoff(thunk, {
15+
statuses: [429],
16+
onRetry: ({ attempt, maxAttempts }) => {
17+
utils.logLabeledWarning(
18+
"functions",
19+
`${clc.bold(clc.yellow("429 (Quota Exceeded)"))} on ${op} ${resourceName}; retrying (attempt ${attempt}${maxAttempts ? `/${maxAttempts}` : ""})…`,
20+
);
21+
},
22+
});
23+
}

0 commit comments

Comments
 (0)