Merge pull request #3702 from mempool/mononaut/websocket-logs

Log websocket statistics
This commit is contained in:
softsimon 2023-05-02 00:55:13 +04:00 committed by GitHub
commit 32c39f7af9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,6 +26,10 @@ class WebsocketHandler {
private wss: WebSocket.Server | undefined;
private extraInitProperties = {};
private numClients = 0;
private numConnected = 0;
private numDisconnected = 0;
constructor() { }
setWebsocketServer(wss: WebSocket.Server) {
@ -42,7 +46,11 @@ class WebsocketHandler {
}
this.wss.on('connection', (client: WebSocket) => {
this.numConnected++;
client.on('error', logger.info);
client.on('close', () => {
this.numDisconnected++;
});
client.on('message', async (message: string) => {
try {
const parsedMessage: WebsocketResponse = JSON.parse(message);
@ -232,6 +240,8 @@ class WebsocketHandler {
throw new Error('WebSocket.Server is not set');
}
this.printLogs();
this.wss.clients.forEach((client) => {
if (client.readyState !== WebSocket.OPEN) {
return;
@ -253,6 +263,8 @@ class WebsocketHandler {
throw new Error('WebSocket.Server is not set');
}
this.printLogs();
if (config.MEMPOOL.ADVANCED_GBT_MEMPOOL) {
await mempoolBlocks.$updateBlockTemplates(newMempool, newTransactions, deletedTransactions.map(tx => tx.txid), true);
} else {
@ -421,6 +433,8 @@ class WebsocketHandler {
throw new Error('WebSocket.Server is not set');
}
this.printLogs();
const _memPool = memPool.getMempool();
if (config.MEMPOOL.AUDIT) {
@ -597,6 +611,17 @@ class WebsocketHandler {
client.send(JSON.stringify(response));
});
}
private printLogs(): void {
if (this.wss) {
const count = this.wss?.clients?.size || 0;
const diff = count - this.numClients;
this.numClients = count;
logger.debug(`${count} websocket clients | ${this.numConnected} connected | ${this.numDisconnected} disconnected | (${diff >= 0 ? '+' : ''}${diff})`);
this.numConnected = 0;
this.numDisconnected = 0;
}
}
}
export default new WebsocketHandler();