aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Kronqvist <joelkronqvist@proton.me>2023-06-24 06:00:33 +0000
committerJoel Kronqvist <joelkronqvist@proton.me>2023-06-24 06:00:33 +0000
commit53dfdded8181e6bf8987795813e128717ef9860c (patch)
treeafad718d81925665ebd6192b8f8b967e08a8d927
parentf51bce27eb3dfac0ed2590d43e179d160e0b99f5 (diff)
downloadLYLLRuoka-53dfdded8181e6bf8987795813e128717ef9860c.tar.gz
LYLLRuoka-53dfdded8181e6bf8987795813e128717ef9860c.zip
Made database.js wrapper use connection pool
Using a connection pool instead of a single @$*%^_!# connection increased the throughput tremendously and fixed the problem with the server crashing due to server disconnects, reducing downtime. Why was the latter issue even unfixed?
-rw-r--r--database.js25
1 files changed, 17 insertions, 8 deletions
diff --git a/database.js b/database.js
index 30396fe..d8e2d92 100644
--- a/database.js
+++ b/database.js
@@ -2,15 +2,21 @@ const mysql = require("mysql2");
class Database
{
- constructor(credentials)
+ constructor(credentials, connectionLimit=8)
{
- this.connection = mysql.createConnection(credentials);
+ this.pool = mysql.createPool({
+ connectionLimit: connectionLimit,
+ host: credentials.host,
+ user: credentials.user,
+ password: credentials.password,
+ database: credentials.database
+ });
}
query(query, values)
{
return new Promise((resolve, reject) =>
{
- this.connection.query(query, values, (err, res, fields) =>
+ this.pool.query(query, values, (err, res, fields) =>
{
if (err) reject(err);
resolve(res);
@@ -21,10 +27,13 @@ class Database
{
return new Promise((resolve, reject) =>
{
- this.connection.execute(query, values, (err, res, fields) =>
- {
+ this.pool.getConnection((err, connection) => {
if (err) reject(err);
- resolve(res);
+ connection.execute(query, values, (err, res, fields) => {
+ connection.release();
+ if (err) reject(err);
+ resolve(res);
+ })
});
});
@@ -33,7 +42,7 @@ class Database
{
return new Promise((resolve, reject) =>
{
- this.connection.query(query, (err, res, fields) =>
+ this.pool.query(query, (err, res, fields) =>
{
if (err)
reject(err)
@@ -45,7 +54,7 @@ class Database
{
return new Promise((resolve, reject) =>
{
- this.connection.end(err =>
+ this.pool.end(err =>
{
if (err) reject(err);
resolve();