From 6412c56fcd0472d9a5e00d0d44ba98456cef7172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Mon, 29 Jun 2020 22:56:41 +0200 Subject: [PATCH 1/2] Adding finally clause to make sure Prometheus indicators are ok --- back/src/Controller/IoSocketController.ts | 25 +++++++++++++---------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index 2ea25f31..102c03fb 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -295,20 +295,23 @@ export class IoSocketController { leaveRoom(Client : ExSocketInterface){ // leave previous room and world if(Client.roomId){ - Client.to(Client.roomId).emit(SockerIoEvent.USER_LEFT, Client.userId); + try { + Client.to(Client.roomId).emit(SockerIoEvent.USER_LEFT, Client.userId); - //user leave previous world - const world : World|undefined = this.Worlds.get(Client.roomId); - if(world){ - world.leave(Client); - if (world.isEmpty()) { - this.Worlds.delete(Client.roomId); + //user leave previous world + const world: World | undefined = this.Worlds.get(Client.roomId); + if (world) { + world.leave(Client); + if (world.isEmpty()) { + this.Worlds.delete(Client.roomId); + } } + //user leave previous room + Client.leave(Client.roomId); + } finally { + this.nbClientsPerRoomGauge.dec({ host: os.hostname(), room: Client.roomId }); + delete Client.roomId; } - //user leave previous room - Client.leave(Client.roomId); - this.nbClientsPerRoomGauge.dec({ host: os.hostname(), room: Client.roomId }); - delete Client.roomId; } } From 82c57810a2e8c109bd068fe1f82edb271197e3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Mon, 29 Jun 2020 23:01:52 +0200 Subject: [PATCH 2/2] Removing host label from Prometheus because it is redundant --- back/src/Controller/IoSocketController.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index 102c03fb..edd29e7b 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -48,12 +48,12 @@ export class IoSocketController { this.nbClientsGauge = new Gauge({ name: 'workadventure_nb_sockets', help: 'Number of connected sockets', - labelNames: [ 'host' ] + labelNames: [ ] }); this.nbClientsPerRoomGauge = new Gauge({ name: 'workadventure_nb_clients_per_room', help: 'Number of clients per room', - labelNames: [ 'host', 'room' ] + labelNames: [ 'room' ] }); // Authentication with token. it will be decoded and stored in the socket. @@ -139,7 +139,7 @@ export class IoSocketController { // Let's log server load when a user joins const srvSockets = this.Io.sockets.sockets; - this.nbClientsGauge.inc({ host: os.hostname() }); + this.nbClientsGauge.inc(); console.log(new Date().toISOString() + ' A user joined (', Object.keys(srvSockets).length, ' connected users)'); si.currentLoad().then(data => console.log(' Current load: ', data.avgload)); si.currentLoad().then(data => console.log(' CPU: ', data.currentload, '%')); @@ -262,7 +262,7 @@ export class IoSocketController { // Let's log server load when a user leaves const srvSockets = this.Io.sockets.sockets; - this.nbClientsGauge.dec({ host: os.hostname() }); + this.nbClientsGauge.dec(); console.log('A user left (', Object.keys(srvSockets).length, ' connected users)'); si.currentLoad().then(data => console.log('Current load: ', data.avgload)); si.currentLoad().then(data => console.log('CPU: ', data.currentload, '%')); @@ -309,7 +309,7 @@ export class IoSocketController { //user leave previous room Client.leave(Client.roomId); } finally { - this.nbClientsPerRoomGauge.dec({ host: os.hostname(), room: Client.roomId }); + this.nbClientsPerRoomGauge.dec({ room: Client.roomId }); delete Client.roomId; } } @@ -318,7 +318,7 @@ export class IoSocketController { private joinRoom(Client : ExSocketInterface, roomId: string, position: PointInterface): World { //join user in room Client.join(roomId); - this.nbClientsPerRoomGauge.inc({ host: os.hostname(), room: roomId }); + this.nbClientsPerRoomGauge.inc({ room: roomId }); Client.roomId = roomId; Client.position = position;