Server shutdown only if no errors
Summary: The socket will close as the endpoint is terminating the connection. Status code 1000 and 1001 are used for normal closures. Either the connection is no longer needed or the endpoint is going away i.e. browser navigating away from the current page. WS RFC: https://www.rfc-editor.org/rfc/rfc6455 Reviewed By: LukeDefeo Differential Revision: D48896696 fbshipit-source-id: 22070ae34b7f35d35589db06108feb718e73e866
This commit is contained in:
committed by
Facebook GitHub Bot
parent
43c549ee74
commit
4e7a6a70cc
@@ -227,11 +227,8 @@ export function attachSocketServer(
|
|||||||
safe(() => onClientMessage(data));
|
safe(() => onClientMessage(data));
|
||||||
});
|
});
|
||||||
|
|
||||||
async function onClientClose(error: Error | undefined = undefined) {
|
async function onClientClose(closeOnIdle: boolean) {
|
||||||
console.log(`Client disconnected ${clientAddress}`);
|
console.log(`Client disconnected ${clientAddress}`);
|
||||||
if (error) {
|
|
||||||
console.error('Client disconnected with error', error);
|
|
||||||
}
|
|
||||||
|
|
||||||
numberOfConnectedClients--;
|
numberOfConnectedClients--;
|
||||||
|
|
||||||
@@ -244,24 +241,43 @@ export function attachSocketServer(
|
|||||||
clearTimeout(disconnectTimeout);
|
clearTimeout(disconnectTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if (closeOnIdle) {
|
||||||
* If, after 60 seconds, there are no more connected clients, we exit the process.
|
/**
|
||||||
*/
|
* If, after 60 seconds, there are no more connected clients, we exit the process.
|
||||||
disconnectTimeout = setTimeout(() => {
|
*/
|
||||||
if (numberOfConnectedClients === 0 && isProduction()) {
|
disconnectTimeout = setTimeout(() => {
|
||||||
console.info('Shutdown as no clients are currently connected');
|
if (numberOfConnectedClients === 0 && isProduction()) {
|
||||||
process.exit(0);
|
console.info('Shutdown as no clients are currently connected');
|
||||||
}
|
process.exit(0);
|
||||||
}, 60 * 1000);
|
}
|
||||||
|
}, 60 * 1000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
client.on('close', () => {
|
client.on('close', (code, _reason) => {
|
||||||
safe(() => onClientClose());
|
/**
|
||||||
|
* The socket will close as the endpoint is terminating
|
||||||
|
* the connection. Status code 1000 and 1001 are used for normal
|
||||||
|
* closures. Either the connection is no longer needed or the
|
||||||
|
* endpoint is going away i.e. browser navigating away from the
|
||||||
|
* current page.
|
||||||
|
* WS RFC: https://www.rfc-editor.org/rfc/rfc6455
|
||||||
|
*/
|
||||||
|
const closeOnIdle = code === 1000 || code === 1001;
|
||||||
|
safe(() => onClientClose(closeOnIdle));
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('error', (error) => {
|
client.on('error', (error) => {
|
||||||
safe(() => onClientClose(error));
|
safe(() => {
|
||||||
|
/**
|
||||||
|
* The socket will close due to an error. In this case,
|
||||||
|
* do not close on idle as there's a high probability the
|
||||||
|
* client will attempt to connect again.
|
||||||
|
*/
|
||||||
|
onClientClose(false);
|
||||||
|
console.error('Client disconnected with error', error);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user