Promise? Pool? Promise Pool? Limited Execution? All-in-one
API
eveything is in TypeScript, by the way
- PLimited - classic promise based "connection pool"
; const pool =
- limited - concurrent execution controller.
const taskRunner = ; // would return N promises you can await for// but execute no more than 10 tasks in concurrentlyPromiseall data; taskRunner;
Handling exceptions
If, by any reason, worker become broken, for example puppetter might broke due to js exceptions on the page, you might dispose it
const worker = await pool;//...//something went wrongawait worker; // dispose the old worker, and create a new one//...worker; // orworker; // drop worker at once, no need to "free" it
Examples
Simple puppeteer, tab per worker
- Uses
onInit
to createbrowser
- creates a
page
on construction. - destroys it on destruction
- Once all resources are freed - destroys the acquired browser.
;; const createBrowser = puppeteer; return limit: 10 ttl: 60000 // keep alive for a minute // per Pool handlers { // pool initilization - create a browser return ; } { // pool idle state(no active workers) - destroy the browser browser; } // worker contruction handlers construct: async { // create a new page return await browser; } destruct: async { page; } ;
Puppeteer, process per worker
Puppeteer is not 100% concurrent across "tabs", so sometimes it's better to create separated processes.
- creates a
browser
and apage
on construction. - destroys both on destruction
- returns only
page
for a customer
;; const createBrowser = puppeteer; return limit: 10 constructionLimit: 1 // allow only one puppeteer to "spin up" in a single point in time ttl: 60000 // keep alive for a moment construct: async { // create a browser const browser = await ; // create a new page const page = await browser; return page browser ; } destruct: async { await browser; } // client needs only the "page" page onAcquire: async { // initialization for every round await page await page; } ;
Mysql connection pool
Easy, and reliable
const mysql = ;const PLimited = ; const pool = limit: 10 ttl: 60 * 10 { return mysql } { conn; };
Licence
MIT