Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove sleep after the last retry attempt #1342

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ jobs:
run: sed -i 's;@upstash/redis@latest;@upstash/redis@${{needs.release.outputs.version}};' ./examples/deno/main.ts

- name: Deploy
run: deno run -A https://deno.land/x/deploy/deployctl.ts deploy --project=upstash-redis ./main.ts
run: deno run -A https://deno.land/x/deploy@1.12.0/deployctl.ts deploy --project=upstash-redis ./main.ts
working-directory: examples/deno
env:
DENO_DEPLOY_TOKEN: ${{ secrets.DENO_DEPLOY_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion examples/deno/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { serve } from "https://deno.land/std@0.177.0/http/server.ts";
import { serve } from "https://deno.land/std@0.224.0/http/server.ts";
import { Redis } from "https://esm.sh/@upstash/redis@latest";

serve(async (_req: Request) => {
Expand Down
23 changes: 23 additions & 0 deletions pkg/http.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, test, expect } from "bun:test";
import { Redis } from "../platforms/nodejs";

describe("http", () => {
test("should terminate after sleeping 5 times", async () => {
// init a cient which will always get errors
const redis = new Redis({
url: undefined,
token: "non-existent",
// set retry explicitly
retry: {
retries: 5,
backoff: (retryCount) => Math.exp(retryCount) * 50,
},
});

// get should take 4.287 seconds and terminate before the timeout.
const throws = () => Promise.race([redis.get("foo"), new Promise((r) => setTimeout(r, 4500))]);

// if the Promise.race doesn't throw, that means the retries took longer than 4.5s
expect(throws).toThrow("fetch() URL is invalid");
});
});
6 changes: 5 additions & 1 deletion pkg/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ export class HttpClient implements Requester {
break;
}
error = error_ as Error;
await new Promise((r) => setTimeout(r, this.retry.backoff(i)));

// Only sleep if this is not the last attempt
if (i < this.retry.attempts) {
await new Promise((r) => setTimeout(r, this.retry.backoff(i)));
}
}
}
if (!res) {
Expand Down
Loading