Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: memory leak due to adblocker #139

Merged
merged 1 commit into from
Sep 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/adblocker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Page} from 'puppeteer';
import {PuppeteerExtraPluginAdblocker} from 'puppeteer-extra-plugin-adblocker';

export const adBlocker = new PuppeteerExtraPluginAdblocker({
blockTrackers: true
});

export async function disableBlockerInPage(page: Page) {
const blockerObject = await adBlocker.getBlocker();
await blockerObject.disableBlockingInPage(page);
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import puppeteer from 'puppeteer-extra';
import stealthPlugin from 'puppeteer-extra-plugin-stealth';
import adblockerPlugin from 'puppeteer-extra-plugin-adblocker';
import {Config} from './config';
import {Stores} from './store/model';
import {Logger} from './logger';
import {tryLookupAndLoop} from './store';
import {getSleepTime} from './util';
import {adBlocker} from './adblocker';

puppeteer.use(stealthPlugin());
puppeteer.use(adblockerPlugin({blockTrackers: true}));
puppeteer.use(adBlocker);

/**
* Starts the bot.
Expand Down
6 changes: 3 additions & 3 deletions src/store/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import open from 'open';
import {Store} from './model';
import {sendNotification} from '../notification';
import {includesLabels} from './includes-labels';
import {delay, getSleepTime} from '../util';
import {closePage, delay, getSleepTime} from '../util';

/**
* Returns true if the brand should be checked for stock
Expand Down Expand Up @@ -62,7 +62,7 @@ async function lookup(browser: Browser, store: Store) {
response = await page.goto(link.url, {waitUntil: 'networkidle0'});
} catch {
Logger.error(`✖ [${store.name}] ${graphicsCard} skipping; timed out`);
await page.close();
await closePage(page);
continue;
}

Expand Down Expand Up @@ -96,7 +96,7 @@ async function lookup(browser: Browser, store: Store) {
sendNotification(givenUrl, link);
}

await page.close();
await closePage(page);
}
/* eslint-enable no-await-in-loop */
}
Expand Down
7 changes: 7 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {Page} from 'puppeteer';
import {Config} from './config';
import {disableBlockerInPage} from './adblocker';

export function getSleepTime() {
return Config.browser.minSleep + (Math.random() * (Config.browser.maxSleep - Config.browser.minSleep));
Expand All @@ -9,3 +11,8 @@ export async function delay(ms: number) {
setTimeout(resolve, ms);
});
}

export async function closePage(page: Page) {
await disableBlockerInPage(page);
await page.close();
}