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 a lock-up of the stats-getter #12

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions src/pushStats/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,52 @@ const SERVER_PORT = parseInt(/** @type {string} */ (process.env.SERVER_PORT), 10

/**
* Check whether there's a server nearby
* @returns {Promise<[string, number]>}
* @returns {Promise<[string, number] | undefined>}
*/
async function checkLocalhostServer() {
console.log('Checking for local server');
const hosts = [
'localhost',
'host.docker.internal',
'172.17.0.1',
];
/** @type {Promise<[string, number]>[]} */
/** @type {Promise<[string, number] | undefined>[]} */
const promises = [];
for (const host of hosts) {
const p = new Promise((resolve, reject) => {
const sock = new net.Socket();
sock.setTimeout(2500);
console.log(`[${host}:${SERVER_PORT}] attempting connection`);
function kill() {
sock.removeAllListeners();
sock.end();
sock.destroy();
sock.unref();
}
sock.setTimeout(200);
sock
.on('connect', () => {
sock.destroy();
.once('connect', () => {
kill();
resolve([host, SERVER_PORT]);
})
.on('error', () => {
sock.destroy();
reject();
.once('error', () => {
kill();
reject(new Error('Error connecting to server'));
})
.on('timeout', () => {
sock.destroy();
reject();
.once('timeout', () => {
kill();
reject(new Error('Timeout connecting to server'));
})
.connect(SERVER_PORT, host);
});
promises.push(p);
}
return Promise.any(promises);
promises.push(new Promise((resolve) => { setTimeout(() => resolve(undefined), 1000); }));
const server = await Promise.any(promises);
if (server) {
console.log(`Found server at ${server[0]}:${server[1]}`);
} else {
console.log('No server found');
}
return server;
}

/**
Expand Down Expand Up @@ -78,6 +91,9 @@ export default async function loadUsers() {
if (!user.host) {
try {
if (user.type === 'private') {
if (!localServer) {
throw new Error('no local server available, and host unspecified');
}
[user.host, user.port] = localServer;
} else {
[user.host, user.port] = getHostInfoFromType(user.type);
Expand All @@ -101,5 +117,6 @@ export default async function loadUsers() {
}
validUsers.push(user);
}
console.log(`Loaded ${validUsers.length} users (out of ${users.length})`);
return validUsers;
}
Loading