Skip to content

Commit

Permalink
chore(chrome): fix connection overfetching
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Jul 23, 2023
1 parent 35d3872 commit 173ba21
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 40 deletions.
68 changes: 34 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@a11ywatch/pagemind",
"version": "0.9.35",
"version": "0.9.36",
"description": "Get metrics on your web page with inclusion",
"main": "./server.js",
"author": "Jeff Mendez",
Expand All @@ -26,14 +26,14 @@
"dependencies": {
"@a11ywatch/protos": "^0.4.7",
"@a11ywatch/website-source-builder": "^0.1.8",
"@grpc/grpc-js": "1.8.16",
"@grpc/proto-loader": "0.7.7",
"@grpc/grpc-js": "1.8.18",
"@grpc/proto-loader": "0.7.8",
"fastq": "1.15.0",
"get-page-speed": "1.0.1",
"kayle": "^0.4.69",
"kayle": "^0.4.75",
"lighthouse": "9.6.8",
"pb-util": "1.0.3",
"playwright": "^1.35.1"
"playwright": "^1.36.1"
},
"devDependencies": {
"@swc/core": "^1.3.19",
Expand Down
23 changes: 22 additions & 1 deletion src/config/chrome.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import dns from "dns";
import { EventEmitter } from "node:events";
import { fetchUrl } from "../core/lib/utils/fetch";
import { getLoadBalancerDefaults } from "../core/lib/utils/connection/load-balancer";

class LoadEmitter extends EventEmitter {}

const loadEmitter = new LoadEmitter();

// chrome load balancer endpoint
const chromeLb = process.env.CHROME_LB;

let CONNECTION_FETCHING = false;

// the chrome hostname dns to connect to with lighthouse sockets
let chromeHost = process.env.CHROME_HOST;
// the chrome socket connection to connect to
Expand Down Expand Up @@ -112,6 +119,15 @@ const getWsEndPoint = async (
retry?: boolean,
bindHost?: boolean
): Promise<[string, string]> => {
if (CONNECTION_FETCHING) {
return new Promise((resolve) => {
loadEmitter.once("event", () => {
resolve([chromeHost, wsChromeEndpointurl]);
});
});
}
CONNECTION_FETCHING = true;

// return the load balancer instance of chrome
if (chromeLb) {
return new Promise(async (resolve) => {
Expand All @@ -124,7 +140,8 @@ const getWsEndPoint = async (
wsChromeEndpointurl = clb[1];
}
}

CONNECTION_FETCHING = false;
loadEmitter.emit("event");
resolve(clb);
});
}
Expand All @@ -136,9 +153,13 @@ const getWsEndPoint = async (
if (retry && !wsChromeEndpointurl) {
setTimeout(async () => {
await getWs();
CONNECTION_FETCHING = false;
loadEmitter.emit("event");
resolve([chromeHost, wsChromeEndpointurl]);
}, 33);
} else {
CONNECTION_FETCHING = false;
loadEmitter.emit("event");
resolve([chromeHost, wsChromeEndpointurl]);
}
});
Expand Down

0 comments on commit 173ba21

Please sign in to comment.