Skip to content

Commit

Permalink
sweep pendingPreRequestsToRemove
Browse files Browse the repository at this point in the history
  • Loading branch information
mschile committed Apr 23, 2024
1 parent f3e9fbe commit bc767fd
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
12 changes: 9 additions & 3 deletions packages/proxy/lib/http/util/prerequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class PreRequests {
pendingPreRequests = new QueueMap<PendingPreRequest>()
pendingRequests = new QueueMap<PendingRequest>()
pendingUrlsWithoutPreRequests = new QueueMap<PendingUrlWithoutPreRequest>()
pendingPreRequestsToRemove: Set<string> = new Set()
pendingPreRequestsToRemove: Map<string, number> = new Map()
sweepIntervalTimer: NodeJS.Timeout
protocolManager?: ProtocolManagerShape

Expand Down Expand Up @@ -153,6 +153,12 @@ export class PreRequests {
return true
})

this.pendingPreRequestsToRemove.forEach((timestamp, requestId) => {
if (timestamp + this.sweepInterval < now) {
this.pendingPreRequestsToRemove.delete(requestId)
}
})

this.pendingUrlsWithoutPreRequests.removeMatching(({ timestamp }) => {
return timestamp + this.sweepInterval >= now
})
Expand Down Expand Up @@ -260,7 +266,7 @@ export class PreRequests {
// the async function to add the pre-request
if (!removed) {
debugVerbose('No pre-request found to remove, adding to pending list: %s', requestId)
this.pendingPreRequestsToRemove.add(requestId)
this.pendingPreRequestsToRemove.set(requestId, Date.now())
} else {
debugVerbose('Removed pre-request %s', requestId)
}
Expand Down Expand Up @@ -360,6 +366,6 @@ export class PreRequests {

this.pendingRequests = new QueueMap<PendingRequest>()
this.pendingUrlsWithoutPreRequests = new QueueMap<PendingUrlWithoutPreRequest>()
this.pendingPreRequestsToRemove = new Set<string>()
this.pendingPreRequestsToRemove = new Map()
}
}
3 changes: 3 additions & 0 deletions packages/proxy/test/unit/http/util/prerequests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ describe('http/util/prerequests', () => {
preRequests = new PreRequests(10, 200)
preRequests.addPending({ requestId: '1234', url: 'foo', method: 'GET', cdpRequestWillBeSentReceivedTimestamp: performance.now() + performance.timeOrigin } as BrowserPreRequest)
preRequests.addPendingUrlWithoutPreRequest('bar')
preRequests.removePendingPreRequest('12345')

expectPendingCounts(0, 1, 1, 1)

// preRequests garbage collects pre-requests that never matched up with an incoming request after around
// 2 * requestTimeout. We verify that it's gone (and therefore not leaking memory) by sending in a request
Expand Down
3 changes: 0 additions & 3 deletions packages/server/lib/automation/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export type AutomationOptions = {
onBrowserPreRequest?: OnBrowserPreRequest
onRequestEvent?: OnRequestEvent
onRemoveBrowserPreRequest?: (requestId: string) => void
onRequestFailed?: (requestId: string) => void
onDownloadLinkClicked?: (downloadUrl: string) => void
onServiceWorkerRegistrationUpdated?: OnServiceWorkerRegistrationUpdated
onServiceWorkerVersionUpdated?: OnServiceWorkerVersionUpdated
Expand All @@ -32,7 +31,6 @@ export class Automation {
public onBrowserPreRequest: OnBrowserPreRequest | undefined
public onRequestEvent: OnRequestEvent | undefined
public onRemoveBrowserPreRequest: ((requestId: string) => void) | undefined
public onRequestFailed: ((requestId: string) => void) | undefined
public onDownloadLinkClicked: ((downloadUrl: string) => void) | undefined
public onServiceWorkerRegistrationUpdated: OnServiceWorkerRegistrationUpdated | undefined
public onServiceWorkerVersionUpdated: OnServiceWorkerVersionUpdated | undefined
Expand All @@ -43,7 +41,6 @@ export class Automation {
this.onBrowserPreRequest = options.onBrowserPreRequest
this.onRequestEvent = options.onRequestEvent
this.onRemoveBrowserPreRequest = options.onRemoveBrowserPreRequest
this.onRequestFailed = options.onRequestFailed
this.onDownloadLinkClicked = options.onDownloadLinkClicked
this.onServiceWorkerRegistrationUpdated = options.onServiceWorkerRegistrationUpdated
this.onServiceWorkerVersionUpdated = options.onServiceWorkerVersionUpdated
Expand Down
2 changes: 1 addition & 1 deletion packages/server/lib/browsers/cdp_automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ export class CdpAutomation implements CDPClient {
}

private onRequestFailed = (params: Protocol.Network.LoadingFailedEvent) => {
this.automation.onRequestFailed?.(params.requestId)
this.automation.onRemoveBrowserPreRequest?.(params.requestId)
}

private onResponseReceived = (params: Protocol.Network.ResponseReceivedEvent) => {
Expand Down
5 changes: 0 additions & 5 deletions packages/server/lib/project-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,6 @@ export class ProjectBase extends EE {
this.server.removeBrowserPreRequest(requestId)
}

const onRequestFailed = (requestId: string) => {
this.server.removeBrowserPreRequest(requestId)
}

const onDownloadLinkClicked = (downloadUrl: string) => {
this.server.addPendingUrlWithoutPreRequest(downloadUrl)
}
Expand Down Expand Up @@ -367,7 +363,6 @@ export class ProjectBase extends EE {
onBrowserPreRequest,
onRequestEvent,
onRemoveBrowserPreRequest,
onRequestFailed,
onDownloadLinkClicked,
onServiceWorkerRegistrationUpdated,
onServiceWorkerVersionUpdated,
Expand Down
5 changes: 2 additions & 3 deletions packages/server/test/unit/browsers/cdp_automation_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ context('lib/browsers/cdp_automation', () => {
onBrowserPreRequest: sinon.stub(),
onRequestEvent: sinon.stub(),
onRemoveBrowserPreRequest: sinon.stub(),
onRequestFailed: sinon.stub(),
onServiceWorkerRegistrationUpdated: sinon.stub(),
onServiceWorkerVersionUpdated: sinon.stub(),
}
Expand Down Expand Up @@ -286,7 +285,7 @@ context('lib/browsers/cdp_automation', () => {
})

describe('.onRequestFailed', function () {
it('triggers onRequestFailed', function () {
it('triggers onRemoveBrowserPreRequest', function () {
const browserRequestFailed = {
requestId: '0',
}
Expand All @@ -295,7 +294,7 @@ context('lib/browsers/cdp_automation', () => {
.withArgs('Network.loadingFailed')
.yield(browserRequestFailed)

expect(this.automation.onRequestFailed).to.have.been.calledWith(browserRequestFailed.requestId)
expect(this.automation.onRemoveBrowserPreRequest).to.have.been.calledWith(browserRequestFailed.requestId)
})
})

Expand Down

4 comments on commit bc767fd

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on bc767fd Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.8.1/linux-x64/mschile/service_worker_uncontrolled-bc767fde47b6e1696b6f8763f15c3e73e264ba0d/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on bc767fd Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.8.1/linux-arm64/mschile/service_worker_uncontrolled-bc767fde47b6e1696b6f8763f15c3e73e264ba0d/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on bc767fd Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.8.1/darwin-arm64/mschile/service_worker_uncontrolled-bc767fde47b6e1696b6f8763f15c3e73e264ba0d/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on bc767fd Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.8.1/darwin-x64/mschile/service_worker_uncontrolled-bc767fde47b6e1696b6f8763f15c3e73e264ba0d/cypress.tgz

Please sign in to comment.