Skip to content

Commit

Permalink
fix(forefront): get random one in pool
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangsx committed Jun 8, 2023
1 parent 3489d6d commit 773f5c8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pool/puppeteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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 [
Expand Down
9 changes: 9 additions & 0 deletions utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 773f5c8

Please sign in to comment.