diff --git a/pool/puppeteer.ts b/pool/puppeteer.ts index 7f35a9f..c1b85f9 100644 --- a/pool/puppeteer.ts +++ b/pool/puppeteer.ts @@ -2,7 +2,7 @@ import puppeteer, {Browser, Page, PuppeteerLaunchOptions} from "puppeteer"; import path from "path"; import run from "node:test"; import * as fs from "fs"; -import {sleep} from "../utils"; +import {shuffleArray, sleep} from "../utils"; const runPath = path.join(__dirname, 'run'); @@ -70,7 +70,7 @@ export class BrowserPool { //@ts-ignore get(): [page: Page | undefined, data: T | undefined, done: (data: T) => void, destroy: (newID: string) => void] { - for (const item of this.pool) { + for (const item of shuffleArray(this.pool)) { if (item.ready) { item.ready = false; return [ diff --git a/utils/index.ts b/utils/index.ts index b6b354d..59cacda 100644 --- a/utils/index.ts +++ b/utils/index.ts @@ -57,3 +57,12 @@ export async function sleep(duration: number): Promise { setTimeout(() => resolve(), duration); }) } + +export function shuffleArray(array: T[]): T[] { + const shuffledArray = [...array]; + for (let i = shuffledArray.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]]; + } + return shuffledArray; +}