|
| 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 | + |
1 | 17 | // Since we're using a FIFO queue, every messageGroupId is treated sequentially |
2 | 18 | // This could cause a backlog of messages in the queue if there is too much page to |
3 | 19 | // revalidate at once. To avoid this, we generate a random messageGroupId for each |
|
6 | 22 | // will always have the same messageGroupId. |
7 | 23 | // https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript#answer-47593316 |
8 | 24 | 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; |
15 | 25 | // This will generate a random int between 0 and MAX_REVALIDATE_CONCURRENCY |
16 | 26 | // This means that we could have 1000 revalidate request at the same time |
17 | 27 | const maxConcurrency = Number.parseInt( |
18 | 28 | process.env.MAX_REVALIDATE_CONCURRENCY ?? "10", |
19 | 29 | ); |
20 | | - const randomInt = Math.floor(randomFloat * maxConcurrency); |
21 | | - return `revalidate-${randomInt}`; |
| 30 | + return generateShardId(rawPath, maxConcurrency, "revalidate"); |
22 | 31 | } |
23 | 32 |
|
24 | 33 | // Used to generate a hash int from a string |
|
0 commit comments