Remedy connection type query at every login (#2298)

Fix #2285
This change adds an internal counter for each tcp/web socket connection
that the server makes and queries the stored memory count at login
rather than the previous way that quired the database during each login.
Each login that quired the DB put a significant load on the server as
the user base grew.
This commit is contained in:
woogerboy21 2016-12-07 01:35:35 -05:00 committed by GitHub
parent a6f1f4c01d
commit f86b9e0be7
3 changed files with 19 additions and 4 deletions

View file

@ -38,7 +38,7 @@
#include <QDebug> #include <QDebug>
Server::Server(QObject *parent) Server::Server(QObject *parent)
: QObject(parent), nextLocalGameId(0) : QObject(parent), nextLocalGameId(0), tcpUserCount(0), webSocketUserCount(0)
{ {
qRegisterMetaType<ServerInfo_Ban>("ServerInfo_Ban"); qRegisterMetaType<ServerInfo_Ban>("ServerInfo_Ban");
qRegisterMetaType<ServerInfo_Game>("ServerInfo_Game"); qRegisterMetaType<ServerInfo_Game>("ServerInfo_Game");
@ -202,12 +202,25 @@ Server_AbstractUserInterface *Server::findUser(const QString &userName) const
void Server::addClient(Server_ProtocolHandler *client) void Server::addClient(Server_ProtocolHandler *client)
{ {
if (client->getConnectionType() == "tcp")
tcpUserCount++;
if (client->getConnectionType() == "websocket")
webSocketUserCount++;
QWriteLocker locker(&clientsLock); QWriteLocker locker(&clientsLock);
clients << client; clients << client;
} }
void Server::removeClient(Server_ProtocolHandler *client) void Server::removeClient(Server_ProtocolHandler *client)
{ {
if (client->getConnectionType() == "tcp")
tcpUserCount--;
if (client->getConnectionType() == "websocket")
webSocketUserCount--;
QWriteLocker locker(&clientsLock); QWriteLocker locker(&clientsLock);
clients.removeAt(clients.indexOf(client)); clients.removeAt(clients.indexOf(client));
ServerInfo_User *data = client->getUserInfo(); ServerInfo_User *data = client->getUserInfo();

View file

@ -97,10 +97,12 @@ public:
QList<PlayerReference> getPersistentPlayerReferences(const QString &userName) const; QList<PlayerReference> getPersistentPlayerReferences(const QString &userName) const;
int getUsersCount() const; int getUsersCount() const;
int getGamesCount() const; int getGamesCount() const;
int getTCPUserCount() const { return tcpUserCount; }
int getWebSocketUserCount() const { return webSocketUserCount; }
private: private:
QMultiMap<QString, PlayerReference> persistentPlayers; QMultiMap<QString, PlayerReference> persistentPlayers;
mutable QReadWriteLock persistentPlayersLock; mutable QReadWriteLock persistentPlayersLock;
int nextLocalGameId; int nextLocalGameId, tcpUserCount, webSocketUserCount;
QMutex nextLocalGameIdMutex; QMutex nextLocalGameIdMutex;
protected slots: protected slots:

View file

@ -1259,7 +1259,7 @@ bool TcpServerSocketInterface::initTcpSession()
bool enforceUserLimit = settingsCache->value("security/enable_max_user_limit", false).toBool(); bool enforceUserLimit = settingsCache->value("security/enable_max_user_limit", false).toBool();
if (enforceUserLimit) { if (enforceUserLimit) {
int userLimit = settingsCache->value("security/max_users_tcp", 500).toInt(); int userLimit = settingsCache->value("security/max_users_tcp", 500).toInt();
int playerCount = (databaseInterface->getActiveUserCount(getConnectionType()) + 1); int playerCount = (server->getTCPUserCount() + 1);
if (playerCount > userLimit){ if (playerCount > userLimit){
std::cerr << "Max Tcp Users Limit Reached, please increase the max_users_tcp setting." << std::endl; std::cerr << "Max Tcp Users Limit Reached, please increase the max_users_tcp setting." << std::endl;
logger->logMessage(QString("Max Tcp Users Limit Reached, please increase the max_users_tcp setting."), this); logger->logMessage(QString("Max Tcp Users Limit Reached, please increase the max_users_tcp setting."), this);
@ -1313,7 +1313,7 @@ bool WebsocketServerSocketInterface::initWebsocketSession()
bool enforceUserLimit = settingsCache->value("security/enable_max_user_limit", false).toBool(); bool enforceUserLimit = settingsCache->value("security/enable_max_user_limit", false).toBool();
if (enforceUserLimit) { if (enforceUserLimit) {
int userLimit = settingsCache->value("security/max_users_websocket", 500).toInt(); int userLimit = settingsCache->value("security/max_users_websocket", 500).toInt();
int playerCount = (databaseInterface->getActiveUserCount(getConnectionType()) + 1); int playerCount = (server->getWebSocketUserCount() + 1);
if (playerCount > userLimit){ if (playerCount > userLimit){
std::cerr << "Max Websocket Users Limit Reached, please increase the max_users_websocket setting." << std::endl; std::cerr << "Max Websocket Users Limit Reached, please increase the max_users_websocket setting." << std::endl;
logger->logMessage(QString("Max Websocket Users Limit Reached, please increase the max_users_websocket setting."), this); logger->logMessage(QString("Max Websocket Users Limit Reached, please increase the max_users_websocket setting."), this);