Skip to content

Commit dd3a610

Browse files
authored
refactor: extract shard ID generation logic into a separate function (#778)
1 parent 4dc08a9 commit dd3a610

File tree

1 file changed

+17
-8
lines changed
  • packages/open-next/src/core/routing

1 file changed

+17
-8
lines changed

packages/open-next/src/core/routing/queue.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
export function generateShardId(
2+
rawPath: string,
3+
maxConcurrency: number,
4+
prefix: string,
5+
) {
6+
let a = cyrb128(rawPath);
7+
// We use mulberry32 to generate a random int between 0 and MAX_REVALIDATE_CONCURRENCY
8+
let t = (a += 0x6d2b79f5);
9+
t = Math.imul(t ^ (t >>> 15), t | 1);
10+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
11+
const randomFloat = ((t ^ (t >>> 14)) >>> 0) / 4294967296;
12+
// This will generate a random int between 0 and maxConcurrency
13+
const randomInt = Math.floor(randomFloat * maxConcurrency);
14+
return `${prefix}-${randomInt}`;
15+
}
16+
117
// Since we're using a FIFO queue, every messageGroupId is treated sequentially
218
// This could cause a backlog of messages in the queue if there is too much page to
319
// revalidate at once. To avoid this, we generate a random messageGroupId for each
@@ -6,19 +22,12 @@
622
// will always have the same messageGroupId.
723
// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript#answer-47593316
824
export function generateMessageGroupId(rawPath: string) {
9-
let a = cyrb128(rawPath);
10-
// We use mulberry32 to generate a random int between 0 and MAX_REVALIDATE_CONCURRENCY
11-
let t = (a += 0x6d2b79f5);
12-
t = Math.imul(t ^ (t >>> 15), t | 1);
13-
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
14-
const randomFloat = ((t ^ (t >>> 14)) >>> 0) / 4294967296;
1525
// This will generate a random int between 0 and MAX_REVALIDATE_CONCURRENCY
1626
// This means that we could have 1000 revalidate request at the same time
1727
const maxConcurrency = Number.parseInt(
1828
process.env.MAX_REVALIDATE_CONCURRENCY ?? "10",
1929
);
20-
const randomInt = Math.floor(randomFloat * maxConcurrency);
21-
return `revalidate-${randomInt}`;
30+
return generateShardId(rawPath, maxConcurrency, "revalidate");
2231
}
2332

2433
// Used to generate a hash int from a string

0 commit comments

Comments
 (0)