Merge pull request #212 from thecodingmachine/mitigateworlderror

Adding finally clause to make sure Prometheus indicators are ok
This commit is contained in:
David Négrier 2020-06-29 23:05:39 +02:00 committed by GitHub
commit 2eaba04f35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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, '%'));
@ -295,27 +295,30 @@ 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({ 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;
}
}
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;