Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const dbConf = require("./configs");
const Events = require("./lex/event");
const Logger = require("./logger");
const Mariadb = require("./mariadb");
const MemoryWatch = require("./memory-watch");
const Messenger = require("./messenger");
const Network = require("./network");
const Offline = require("./offline");
Expand Down Expand Up @@ -67,6 +68,7 @@ module.exports = {
getUiInfo,
Logger,
Mariadb,
MemoryWatch,
MessageBus: RedisStore,
Messenger,
Network,
Expand Down
9 changes: 9 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const { existsSync, mkdirSync, readFileSync } = require('fs');
const { resolve, normalize, join, dirname } = require('path');

const Cache = require("./cache");
const MemoryWatch = require("./memory-watch");

const LEVEL = {
ERROR: 0,
Expand Down Expand Up @@ -79,6 +80,13 @@ class logger extends Backbone.Model {
return LEVEL;
}

/**
*
*/
preinitialize() {
MemoryWatch.track(this);
}

/**
*
* @param {...any} args
Expand Down Expand Up @@ -412,6 +420,7 @@ class logger extends Backbone.Model {
timer = null;
}, 11000);
this._stopping = 1;
MemoryWatch.markStopped(this);
};


Expand Down
113 changes: 79 additions & 34 deletions lib/mariadb.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,32 @@ class mariadb_stub extends Logger {
if (!Mariadb) {
Mariadb = await import('mariadb');
}
return new Promise(async (resolve, reject) => {
if (this._connection && this._connection.isValid()) {
return resolve(this._connection);
}
if (this.get(Attr.limit) > 1) {
let pool = Mariadb.createPool(this.configs);
this._pool = pool;
try {
this._connection = await Mariadb.createConnection(this.configs)
resolve(this._connection);
} catch (err) {
console.trace()
this.warn("[FATAL]: not connected due to error ", err);
reject(err)
}
} else {
try {
this._connection = await Mariadb.createConnection(this.configs)
resolve(this._connection);
} catch (err) {
console.trace()
this.warn("[FATAL]: not connected due to error ", err);
reject(err)
}
}
})
if (this._connection && this._connection.isValid()) {
return this._connection;
}
try {
this._connection = await Mariadb.createConnection(this.configs)
return this._connection;
} catch (err) {
console.trace()
this.warn("[FATAL]: not connected due to error ", err);
throw err;
}
}

/**
* Lazily creates the connection pool. Only used when limit > 1;
* instances with limit 1 keep the legacy single-connection behavior.
* @returns
*/
async getPool() {
if (!Mariadb) {
Mariadb = await import('mariadb');
}
if (!this._pool) {
this._pool = Mariadb.createPool({ ...this.configs, minimumIdle: 1 });
}
return this._pool;
}

/**
Expand Down Expand Up @@ -112,14 +112,17 @@ class mariadb_stub extends Logger {
}

if (e.fatal) {
this.warn("FATAL ERROR RAISED -- EXITING", e);
this.warn("FATAL ERROR RAISED -- RESETTING CONNECTION", e);
this._connection = null;
throw e;
}
switch (e.code) {
case 'ER_CMD_CONNECTION_CLOSED':
case 'ER_CONNECTION_TIMEOUT':
this.warn("CONNECTION ERROR -- FORCE RELOAD", e);
process.exit(1);
this.warn("CONNECTION ERROR -- RESETTING CONNECTION", e);
this._connection = null;
this.trigger(ERROR, err);
return;
case 'ER_LOCK_DEADLOCK':
return;
default:
Expand Down Expand Up @@ -235,6 +238,9 @@ class mariadb_stub extends Logger {
sql = args.shift();
}
this.log(sql, args);
if (this.get(Attr.limit) > 1) {
return this._runPooled(sql, args, handler);
}
let res = [];
let c = null;
try {
Expand All @@ -244,19 +250,56 @@ class mariadb_stub extends Logger {
this.warn('Attempt to run after connection close');
return { failed: 1, code: "CONNECTION_ALREADY_CLOSED" }
}
this.warn('Some abnormal errors occurred. Exitng...');
this.warn('Some abnormal errors occurred. Connection dropped');
this.debug({ db: this._dbname, sql, args });
process.exit(1);
this._connection = null;
return { failed: 1, code: "CONNECTION_LOST" }
}
} catch (e) {
this.warn('Failed to get DB connection', e);
process.exit(1);
return { failed: 1, code: "CONNECTION_FAILED" }
}
return c.beginTransaction()
.then(() => {
return c.query(sql, args);
})
.then((rows) => {
if (rows) {
try {
res = rows.get_rows();
} catch (e) {
res = rows;
}
}
c.commit();
if (isFunction(handler)) {
return handler(res);
}
return res;
})
.catch(this._handleError);
}

/**
* Runs a statement on a pooled connection. The pool acquires and
* releases the connection around each statement (autocommit), so a
* long-running call never blocks the other requests of the process.
* @param {*} sql
* @param {*} args
* @param {*} handler
* @returns
*/
async _runPooled(sql, args, handler) {
let pool;
try {
pool = await this.getPool();
} catch (e) {
this.warn('Failed to get DB pool', e);
return { failed: 1, code: "CONNECTION_FAILED" }
}
return pool.query(sql, args)
.then((rows) => {
let res = [];
if (rows) {
try {
res = rows.get_rows();
Expand All @@ -267,7 +310,6 @@ class mariadb_stub extends Logger {
if (isFunction(handler)) {
return handler(res);
}
c.commit();
return res;
})
.catch(this._handleError);
Expand Down Expand Up @@ -351,6 +393,9 @@ class mariadb_stub extends Logger {
* @returns
*/
async await_run(sql, args) {
if (this.get(Attr.limit) > 1) {
return this._runPooled(sql, args);
}
let c = await this.getConnection();
return c.beginTransaction()
.then(() => {
Expand Down Expand Up @@ -380,7 +425,7 @@ class mariadb_stub extends Logger {
this._closed = 1;
try {
this.silly("STOPING DB", this._dbname, this.isValid());
if (!this.isValid()) {
if (!this.isValid() && !this._pool) {
this.stop();
return;
}
Expand Down
Loading