Skip to content

Commit

Permalink
Auto-delete rooms after 10 minutes of inactivity
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb531 committed Jul 2, 2019
1 parent dd64a40 commit 38f4f6f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ io.on('connection', (socket) => {
let room = roomManager.getRoom(roomCode);
if (room) {
console.log(`join room by ${userId}`);
roomManager.markRoomAsActive(room);
let localPlayer = room.connectPlayer({ userId, socket });
let status;
if (localPlayer) {
Expand Down Expand Up @@ -231,6 +232,11 @@ io.on('connection', (socket) => {
console.log('unset player socket');
socket.user.socket = null;
}
// As soon as both players disconnect from the room (making it completely
// empty), mark the room for deletion
if (socket.room && !socket.room.isActive()) {
roomManager.markRoomAsInactive(socket.room);
}
});

});
34 changes: 34 additions & 0 deletions app/server/room-manager.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import moment from 'moment';

import Room from './room.js';

class RoomManager {

constructor() {
this.roomsById = {};
this.inactiveRooms = new Set();
this.pollForAbandonedRooms();
}

getRoom(roomCode) {
Expand Down Expand Up @@ -43,9 +47,39 @@ class RoomManager {
return roomCode;
}

markRoomAsActive(room) {
this.inactiveRooms.delete(room);
room.lastMarkedInactive = null;
console.log(`room ${room.code} is active again`);
}

markRoomAsInactive(room) {
room.lastMarkedInactive = Date.now();
this.inactiveRooms.add(room);
console.log(`room ${room.code} marked as inactive`);
}

// A room is considered "abandoned" if it has been inactive for more than the
// specified period of time
pollForAbandonedRooms() {
setInterval(() => {
this.inactiveRooms.forEach((room) => {
if (room.isAbandoned()) {
// Yes, it is safe to remove elements from a Set while iterating over
// it; see <https://stackoverflow.com/a/28306768/560642>
this.inactiveRooms.delete(room);
delete this.roomsById[room.code];
console.log(`room ${room.code} has been permanently deleted`);
}
});
}, Room.abandonmentCheckInterval.asMilliseconds());
}

}

// The number of characters is a given room code
RoomManager.roomCodeLength = 4;
// How frequently (in minutes) to check for abandoned rooms
Room.abandonmentCheckInterval = moment.duration(30, 'minutes');

export default RoomManager;
23 changes: 23 additions & 0 deletions app/server/room.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import moment from 'moment';

import Player from './player.js';
import Game from './game.js';

Expand All @@ -7,13 +9,17 @@ class Room {
this.code = code;
this.players = players;
this.game = game;
// The date/time the room was last seen completely empty (i.e. both players
// were disconnected)
this.lastMarkedInactive = null;
}

addPlayer({ player, socket }) {
player = new Player(player);
this.players.push(player);
player.socket = socket;
socket.user = player;
socket.room = this;
socket.join(this.code);
return player;
}
Expand All @@ -31,11 +37,28 @@ class Room {
if (player) {
player.socket = socket;
socket.user = player;
socket.room = this;
socket.join(this.code);
}
return player;
}

// Return true if at least one player is currently connected to the room,
// otherwise return false
isActive() {
return this.players.some((player) => player.socket !== null);
}

isAbandoned() {
return moment(this.lastMarkedInactive)
.add(Room.abandonmentThreshold)
.isSameOrBefore(moment());
}

}

// The number of minutes a room can be inactive before it is considered
// abandoned (and thus subject to automatic deletion by the RoomManager)
Room.abandonmentThreshold = moment.duration(10, 'minutes');

export default Room;
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"express": "^4.17.1",
"fastclick": "^1.0.6",
"mithril": "github:MithrilJS/mithril.js#deaf01c8e96446c97f31826b1d04ab4a4e757718",
"moment": "^2.24.0",
"random": "^2.1.1",
"socket.io": "^2.2.0",
"socket.io-client": "^2.2.0",
Expand Down

0 comments on commit 38f4f6f

Please sign in to comment.