From 5171e5f94bd6a7f85dee3298fabd7320a3ffb8a2 Mon Sep 17 00:00:00 2001 From: "yanlong.wang" Date: Mon, 2 Sep 2024 14:30:23 +0800 Subject: [PATCH] fix: req cap issues --- backend/functions/src/services/puppeteer.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/backend/functions/src/services/puppeteer.ts b/backend/functions/src/services/puppeteer.ts index a7ae25e..75229c1 100644 --- a/backend/functions/src/services/puppeteer.ts +++ b/backend/functions/src/services/puppeteer.ts @@ -1,7 +1,7 @@ import os from 'os'; import fs from 'fs'; import { container, singleton } from 'tsyringe'; -import { AsyncService, Defer, marshalErrorLike, AssertionFailureError, delay, maxConcurrency, Deferred } from 'civkit'; +import { AsyncService, Defer, marshalErrorLike, AssertionFailureError, delay, maxConcurrency, Deferred, perNextTick } from 'civkit'; import { Logger } from '../shared/services/logger'; import type { Browser, CookieParam, GoToOptions, Page } from 'puppeteer'; @@ -310,11 +310,14 @@ export class PuppeteerControl extends AsyncService { this.logger.warn(`Browser killed`); } + @perNextTick() reqCapRoutine() { const now = Date.now(); const numToPass = Math.round((now - this.lastReqSentAt) / 1000 * this.rpsCap); this.requestDeferredQueue.splice(0, numToPass).forEach((x) => x.resolve(true)); - this.lastReqSentAt = now; + if (numToPass) { + this.lastReqSentAt = now; + } if (!this.requestDeferredQueue.length) { if (this.__reqCapInterval) { clearInterval(this.__reqCapInterval); @@ -403,10 +406,12 @@ export class PuppeteerControl extends AsyncService { return req.abort('blockedbyclient', 1000); } - const d = Defer(); - this.requestDeferredQueue.push(d); - process.nextTick(() => this.reqCapRoutine()); - await d.promise; + if (requestUrl.startsWith('http')) { + const d = Defer(); + this.requestDeferredQueue.push(d); + this.reqCapRoutine(); + await d.promise; + } if (req.isInterceptResolutionHandled()) { return;