Skip to content

Commit

Permalink
Use Math.pow for retry delay calculation (#625)
Browse files Browse the repository at this point in the history
  • Loading branch information
HermanBilous authored Feb 14, 2025
1 parent 37728ee commit b4a656d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-mice-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-ws': patch
---

Use Math.pow for retry delay calculation
7 changes: 2 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,11 @@ export function createClient<
connectionAckWaitTimeout = 0,
retryAttempts = 5,
retryWait = async function randomisedExponentialBackoff(retries) {
let retryDelay = 1000; // start with 1s delay
for (let i = 0; i < retries; i++) {
retryDelay *= 2;
}
const retryDelaySeconds = Math.pow(2, retries);
await new Promise((resolve) =>
setTimeout(
resolve,
retryDelay +
retryDelaySeconds * 1000 +
// add random timeout from 300ms to 3s
Math.floor(Math.random() * (3000 - 300) + 300),
),
Expand Down

0 comments on commit b4a656d

Please sign in to comment.