Skip to content

Instantly share code, notes, and snippets.

Created March 1, 2018 13:39
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/c3d9b0d51127ea0bd396432075b2990a to your computer and use it in GitHub Desktop.
Save anonymous/c3d9b0d51127ea0bd396432075b2990a to your computer and use it in GitHub Desktop.
const genericPool = require('generic-pool');
const puppeteer = require('puppeteer');
// puppeteer.launch will return a promise to resolve with a browser instance
const browserPromise = puppeteer.launch();
const factory = {
create: async function() {
// reuse the same browser instance to create new pages
const browser = await browserPromise();
const page = await browser.newPage();
await page.setViewport({ width: 800, height: 420 });
return page;
},
destroy: function(puppeteer) {
puppeteer.close();
},
};
const browserPagePool = genericPool.createPool(factory, {
max: 10,
min: 2,
maxWaitingClients: 50,
});
module.exports = browserPagePool;
@lsbyerley
Copy link

lsbyerley commented May 31, 2018

This const browserPromise = puppeteer.launch(); causes an out of memory allocation error. You need to await the puppeteer.launch() instead

const browserPromise = async () => await puppeteer.launch();

@rahulkumar66
Copy link

This doesn't pool the pages against a single browser. It just keeps on opening new browsers for each request since create() is called for each new request

@solitud
Copy link

solitud commented Nov 8, 2018

This doesn't pool the pages against a single browser. It just keeps on opening new browsers for each request since create() is called for each new request

+1. Code might even crash the server by spawning unlimited browsers over time.

@aintHuman
Copy link

aintHuman commented Oct 30, 2019

This doesn't pool the pages against a single browser. It just keeps on opening new browsers for each request since create() is called for each new request

+1. Code might even crash the server by spawning unlimited browsers over time.

lol

(f=>f(f))(async f=>f(f)&&f(f));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment