Skip to content

Commit 50b3559

Browse files
authored
move generate message id to own file (#731)
1 parent 5e92af8 commit 50b3559

File tree

3 files changed

+48
-48
lines changed

3 files changed

+48
-48
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import type { InternalEvent, InternalResult } from "types/open-next";
55
import type { CacheValue } from "types/overrides";
66
import { emptyReadableStream, toReadableStream } from "utils/stream";
77

8-
import { debug } from "../../adapters/logger";
9-
import { localizePath } from "./i18n";
10-
import { generateMessageGroupId } from "./util";
8+
import { debug } from "../../adapters/logger.js";
9+
import { localizePath } from "./i18n/index.js";
10+
import { generateMessageGroupId } from "./queue.js";
1111

1212
const CACHE_ONE_YEAR = 60 * 60 * 24 * 365;
1313
const CACHE_ONE_MONTH = 60 * 60 * 24 * 30;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Since we're using a FIFO queue, every messageGroupId is treated sequentially
2+
// This could cause a backlog of messages in the queue if there is too much page to
3+
// revalidate at once. To avoid this, we generate a random messageGroupId for each
4+
// revalidation request.
5+
// We can't just use a random string because we need to ensure that the same rawPath
6+
// will always have the same messageGroupId.
7+
// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript#answer-47593316
8+
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+
// This will generate a random int between 0 and MAX_REVALIDATE_CONCURRENCY
16+
// This means that we could have 1000 revalidate request at the same time
17+
const maxConcurrency = Number.parseInt(
18+
process.env.MAX_REVALIDATE_CONCURRENCY ?? "10",
19+
);
20+
const randomInt = Math.floor(randomFloat * maxConcurrency);
21+
return `revalidate-${randomInt}`;
22+
}
23+
24+
// Used to generate a hash int from a string
25+
function cyrb128(str: string) {
26+
let h1 = 1779033703;
27+
let h2 = 3144134277;
28+
let h3 = 1013904242;
29+
let h4 = 2773480762;
30+
for (let i = 0, k: number; i < str.length; i++) {
31+
k = str.charCodeAt(i);
32+
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
33+
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
34+
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
35+
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
36+
}
37+
h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
38+
h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
39+
h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
40+
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
41+
// biome-ignore lint/style/noCommaOperator:
42+
(h1 ^= h2 ^ h3 ^ h4), (h2 ^= h1), (h3 ^= h1), (h4 ^= h1);
43+
return h1 >>> 0;
44+
}

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

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
import { debug, error } from "../../adapters/logger.js";
1818
import { isBinaryContentType } from "../../utils/binary.js";
1919
import { localizePath } from "./i18n/index.js";
20+
import { generateMessageGroupId } from "./queue.js";
2021

2122
/**
2223
*
@@ -310,51 +311,6 @@ export async function revalidateIfRequired(
310311
}
311312
}
312313

313-
// Since we're using a FIFO queue, every messageGroupId is treated sequentially
314-
// This could cause a backlog of messages in the queue if there is too much page to
315-
// revalidate at once. To avoid this, we generate a random messageGroupId for each
316-
// revalidation request.
317-
// We can't just use a random string because we need to ensure that the same rawPath
318-
// will always have the same messageGroupId.
319-
// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript#answer-47593316
320-
export function generateMessageGroupId(rawPath: string) {
321-
let a = cyrb128(rawPath);
322-
// We use mulberry32 to generate a random int between 0 and MAX_REVALIDATE_CONCURRENCY
323-
let t = (a += 0x6d2b79f5);
324-
t = Math.imul(t ^ (t >>> 15), t | 1);
325-
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
326-
const randomFloat = ((t ^ (t >>> 14)) >>> 0) / 4294967296;
327-
// This will generate a random int between 0 and MAX_REVALIDATE_CONCURRENCY
328-
// This means that we could have 1000 revalidate request at the same time
329-
const maxConcurrency = Number.parseInt(
330-
process.env.MAX_REVALIDATE_CONCURRENCY ?? "10",
331-
);
332-
const randomInt = Math.floor(randomFloat * maxConcurrency);
333-
return `revalidate-${randomInt}`;
334-
}
335-
336-
// Used to generate a hash int from a string
337-
function cyrb128(str: string) {
338-
let h1 = 1779033703;
339-
let h2 = 3144134277;
340-
let h3 = 1013904242;
341-
let h4 = 2773480762;
342-
for (let i = 0, k: number; i < str.length; i++) {
343-
k = str.charCodeAt(i);
344-
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
345-
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
346-
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
347-
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
348-
}
349-
h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
350-
h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
351-
h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
352-
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
353-
// biome-ignore lint/style/noCommaOperator:
354-
(h1 ^= h2 ^ h3 ^ h4), (h2 ^= h1), (h3 ^= h1), (h4 ^= h1);
355-
return h1 >>> 0;
356-
}
357-
358314
/**
359315
*
360316
* @__PURE__

0 commit comments

Comments
 (0)