@@ -17,6 +17,7 @@ import type {
1717import { debug , error } from "../../adapters/logger.js" ;
1818import { isBinaryContentType } from "../../utils/binary.js" ;
1919import { 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