Skip to content

Commit 3af8c47

Browse files
committed
Add random interval between 45-55 seconds
1 parent a2b466a commit 3af8c47

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

packages/clerk-js/src/core/auth/SessionCookiePoller.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ import { createWorkerTimers } from '@clerk/shared/workerTimers';
33
import { SafeLock } from './safeLock';
44

55
const REFRESH_SESSION_TOKEN_LOCK_KEY = 'clerk.lock.refreshSessionToken';
6-
const INTERVAL_IN_MS = 5 * 1_000;
6+
7+
/**
8+
* Returns an interval in milliseconds with random jitter.
9+
* Uses a base interval of 5 seconds and adds up to 1.5 seconds of random jitter.
10+
* This randomization helps prevent synchronized polling requests across multiple clients.
11+
*/
12+
const getIntervalInMs = () => {
13+
const jitter = Math.random() * 1500;
14+
return 5 * 1_000 + jitter;
15+
};
716

817
export class SessionCookiePoller {
918
private lock = SafeLock(REFRESH_SESSION_TOKEN_LOCK_KEY);
@@ -20,7 +29,7 @@ export class SessionCookiePoller {
2029
const run = async () => {
2130
this.initiated = true;
2231
await this.lock.acquireLockAndRun(cb);
23-
this.timerId = this.workerTimers.setTimeout(run, INTERVAL_IN_MS);
32+
this.timerId = this.workerTimers.setTimeout(run, getIntervalInMs());
2433
};
2534

2635
void run();

0 commit comments

Comments
 (0)