Skip to content

Commit

Permalink
Add retries for certain errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jacwright committed Mar 22, 2023
1 parent d8aa0a1 commit dad78ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "durable-apis",
"version": "0.0.9",
"version": "0.0.10",
"description": "Functional-style Cloudflare Durable Objects with direct API calls from Cloudflare Workers and TypeScript support.",
"main": "dist/durable-apis.js",
"types": "dist/durable-apis.d.ts",
Expand Down
19 changes: 17 additions & 2 deletions src/durable-apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {

export type EmptyObj = {[key: string]: any};
const URL = 'https://durable/';
const maxRetries = 10;

export type Object = Record<string, any>;
export type DurableInitConstructor<Env, T> = {new (state: DurableObjectState, env: Env): T};
Expand Down Expand Up @@ -137,8 +138,14 @@ function extendNamespace(namespace: DurableObjectNamespace) {
return namespace;
}

async function stubFetch(obj: DurableObjectStub, prop: string, content: any) {
return obj.fetch(createRequest(prop, content)).then(transformResponse)
async function stubFetch(obj: DurableObjectStub, prop: string, content: any, retries = 0) {
return obj.fetch(createRequest(prop, content)).then(transformResponse).catch(err => {
if (!shouldRetry(err, retries)) return Promise.reject(err);
// Retry up to 11 times over 30 seconds with exponential backoff. 20ms, 40ms, etc
return new Promise(resolve => setTimeout(resolve, Math.pow(2, retries) * 10)).then(() => {
return stubFetch(obj, prop, content, retries + 1);
});
});
}

function createRequest(prop: string, content: any) {
Expand Down Expand Up @@ -169,3 +176,11 @@ async function transformResponse(response: Response) {
} catch (err) {}
return response;
}

function shouldRetry(err: any, retries: number) {
if (retries > maxRetries) return false;
err = err + '';
if (err.includes('Network connection lost.')) return true;
if (err.includes('Cannot resolve Durable Object due to transient issue on remote node.')) return true;
return false;
}

0 comments on commit dad78ba

Please sign in to comment.