fix(forefront): get random one in pool
このコミットが含まれているのは:
コミット
773f5c8393
|
@ -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<T> {
|
|||
|
||||
//@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 [
|
||||
|
|
|
@ -57,3 +57,12 @@ export async function sleep(duration: number): Promise<void> {
|
|||
setTimeout(() => resolve(), duration);
|
||||
})
|
||||
}
|
||||
|
||||
export function shuffleArray<T>(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;
|
||||
}
|
||||
|
|
読み込み中…
新しいイシューから参照