This commit is contained in:
黄心宇 2023-09-27 17:26:10 +08:00
parent 77348e7df7
commit 5fd3c3158e
1 changed files with 30 additions and 3 deletions

33
ssr.js
View File

@ -1,8 +1,13 @@
import puppeteer from 'puppeteer';
// import redisClient from './redis/index.js';
import { getBrowser, scheduledTask } from './puppeteerTool.js'
// import { getBrowser, scheduledTask } from './puppeteerTool.js'
scheduledTask()
const MAX_WSE = 4; //启动几个浏览器
let WSE_LIST = []; //存储browserWSEndpoint列表
// scheduledTask()
init()
async function ssr(url) {
// const REDIS_KEY = `ssr:${url}`;
@ -16,7 +21,10 @@ async function ssr(url) {
const start = Date.now();
const browser = getBrowser()
let tmp = Math.floor(Math.random()* MAX_WSE);
let browserWSEndpoint = WSE_LIST[tmp];
const browser = await puppeteer.connect({browserWSEndpoint});
const page = await browser.newPage();
try {
// networkidle0 waits for the network to be idle (no requests for 500ms).
@ -38,5 +46,24 @@ async function ssr(url) {
return {html, ttRenderMs};
}
function init(){
(async () => {
for(var i=0;i<MAX_WSE;i++){
const browser = await puppeteer.launch({headless:'new',
args: [
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-setuid-sandbox',
'--no-first-run',
'--no-sandbox',
'--no-zygote',
'--single-process'
]});
let browserWSEndpoint = await browser.wsEndpoint();
WSE_LIST[i] = browserWSEndpoint;
}
console.log(WSE_LIST);
})();
}
export {ssr as default};