Skip to content

Commit

Permalink
fix: 修正可能存在的内存泄漏。
Browse files Browse the repository at this point in the history
  • Loading branch information
sj817 committed Dec 9, 2024
1 parent 6ea5782 commit b701d41
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
29 changes: 13 additions & 16 deletions src/puppeteer/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,19 @@ export class Render {
this.process = this.browser.process()

/** 监听浏览器关闭事件 移除浏览器实例 */
this.browser.on('disconnected', () => {
this.browser.on('disconnected', async () => {
console.error(`[浏览器][${this.id}] 已关闭或崩溃`)

/** 传递一个浏览器崩溃事件出去 用于在浏览器池子中移除掉当前浏览器 */
common.emit('browserCrash', this.id)
/** 尝试关闭 */
this.browser?.close && this.browser.close().catch(() => { })
if (this.browser) {
await this.browser?.close().catch(() => { })
}
/** 如果pid存在 再使用node自带的kill杀一次 */
this.process?.pid && process.kill(this.process.pid)
if (this.process?.pid) {
process.kill(this.process.pid)
}
})
}

Expand All @@ -133,10 +137,11 @@ export class Render {
* @returns 截图结果
*/
async render<T extends screenshot> (echo: string, data: T): Promise<RenderResult<T>> {
let page: Page | undefined
try {
this.list.set(echo, true)
/** 创建页面 */
const page = await this.page(data)
page = await this.page(data)

const options = {
path: data.path,
Expand All @@ -157,7 +162,6 @@ export class Render {
options.captureBeyondViewport = true
const uint8Array = await page.screenshot(options)
await this.setViewport(page, data?.setViewport?.width, data?.setViewport?.height, data?.setViewport?.deviceScaleFactor)
this.screenshot(page)
return uint8Array as RenderResult<T>
}

Expand All @@ -176,8 +180,6 @@ export class Render {
if (!data.multiPage) {
/** 截图 */
const uint8Array = await page.screenshot(options)

this.screenshot(page)
return uint8Array as RenderResult<T>
}

Expand Down Expand Up @@ -212,6 +214,10 @@ export class Render {
} finally {
/** 从队列中去除 */
this.list.delete(echo)
if (page) {
common.emit('screenshot', this.id)
await page?.close().catch(() => { })
}
}
}

Expand Down Expand Up @@ -294,15 +300,6 @@ export class Render {
}
}

/**
* 生成图片次数+1 并关闭页面
* @param page 页面实例
*/
async screenshot (page: Page) {
common.emit('screenshot', this.id)
await page.close().catch(() => { })
}

/**
* 设置视窗大小
* @param page 页面实例
Expand Down
9 changes: 6 additions & 3 deletions src/puppeteer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ export class Puppeteer {
}

/** 监听浏览器关闭事件 移除浏览器实例 */
common.on('browserCrash', (id) => {
common.on('browserCrash', async (id) => {
const index = this.list.findIndex(item => item.id === id)
if (index !== -1) this.list.splice(index, 1)
/** 新开浏览器 */
if (index !== -1) {
const browser = this.list[index]
this.list.splice(index, 1)
await browser?.browser?.close().catch(() => { })
}
this.launch()
})

Expand Down

0 comments on commit b701d41

Please sign in to comment.