From 050eef58b9993345c1cd05aae58e3d6170ade70b Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Sun, 3 Apr 2022 06:10:13 +1100 Subject: [PATCH] fix(execute): revert to promise api Several reports of increased chances to hit a deadlock since v2.2.2, and three reports of a timeout during prepare since v2.2.4. Simpler to revert to the old prepare function and api (with improvements) than to work out the solution. Look into rewriting prepare in the future or including the mariadb connector, which doesn't suffer from the same perf issues. --- fxmanifest.lua | 2 +- package.json | 2 +- src/database/rawExecute.ts | 105 +++++++++++++++---------------------- src/database/rawQuery.ts | 1 + 4 files changed, 45 insertions(+), 65 deletions(-) diff --git a/fxmanifest.lua b/fxmanifest.lua index 8b29f1f..5e839ed 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -3,7 +3,7 @@ game 'common' name 'oxmysql' description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.' -version '2.2.4' +version '2.2.5' url 'https://github.com/overextended/oxmysql' author 'overextended' diff --git a/package.json b/package.json index 5d136c0..6edcd91 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "oxmysql", - "version": "2.2.4", + "version": "2.2.5", "description": "FXServer to MySQL communication via node-mysql2", "repository": "git@github.com:overextended/oxmysql.git", "author": "dunak-debug <19434539+dunak-debug@users.noreply.github.com>", diff --git a/src/database/rawExecute.ts b/src/database/rawExecute.ts index cc0590c..ccda338 100644 --- a/src/database/rawExecute.ts +++ b/src/database/rawExecute.ts @@ -14,80 +14,59 @@ export const rawExecute = async ( ) => { const type = executeType(query); parameters = parseExecute(parameters); + const response = [] as RowDataPacket; let single: boolean; if (!parameters.every(Array.isArray)) parameters = [[...parameters]]; - const length = parameters.length - 1; - await scheduleTick(); - await new Promise((resolve, reject) => { - pool.getConnection((err, connection) => { - if (err) return reject([connection, err]); + const connection = await pool.promise().getConnection(); - const execute = () => { - const placeholders = query.split('?').length - 1; - const response = [] as RowDataPacket; + try { + if (type !== null) await connection.beginTransaction(); + const placeholders = query.split('?').length - 1; - parameters.forEach((values, index) => { - const executionTime = process.hrtime(); - values = parseValues(placeholders, values); + for (let values of parameters as CFXParameters[]) { + const executionTime = process.hrtime(); + values = parseValues(placeholders, values); - connection.execute(query, values, (err, results: RowDataPacket[][]) => { - if (err) return reject([connection, err]); + const [results, _] = (await connection.execute(query, values)) as RowDataPacket[][]; - if (cb) { - if (results.length > 1) { - for (const value of results) { - response.push(parseResponse(type, value)); - } - } else response.push(parseResponse(type, results)); - } - - logQuery(invokingResource, query, process.hrtime(executionTime)[1] / 1e6, values as typeof parameters); - - if (index === length) { - single = response.length === 1; - if (!single && type !== null) connection.commit(); - connection.release(); - - resolve(response); - } - }); - }); - }; - - if (type === null) { - execute(); - } else - connection.beginTransaction((err) => { - if (err) return reject(err); - execute(); - }); - }); - }) - .catch(([connection, err]) => { - if (!single && type !== null) connection.rollback(); - - throw new Error(`${invokingResource} was unable to execute a query! - ${err.message} - ${err.sql}`); - }) - // somebody who cares about types can do this - .then((results: any) => { if (cb) { - if (single) { - if (type === null) { - //@ts-expect-error - if (results[0][0] && Object.keys(results[0][0]).length === 1) cb(Object.values(results[0][0])[0]); - else cb(results[0][0]); - } else { - cb(results[0]); + if (results.length > 1) { + for (const value of results) { + response.push(parseResponse(type, value)); } - } else { - cb(results); - } + } else response.push(parseResponse(type, results)); } - }); + + logQuery(invokingResource, query, process.hrtime(executionTime)[1] / 1e6, values as typeof parameters); + } + + single = response.length === 1; + if (type !== null) connection.commit(); + } catch (err: any) { + if (connection && type !== null) connection.rollback(); + + throw new Error(`${invokingResource} was unable to execute a query! + ${err.message} + ${err.sql}`); + } finally { + connection.release(); + } + + if (cb) { + if (single) { + if (type === null) { + //@ts-expect-error + if (response[0][0] && Object.keys(response[0][0]).length === 1) cb(Object.values(response[0][0])[0]); + else cb(response[0][0]); + } else { + cb(response[0]); + } + } else { + cb(response); + } + } }; diff --git a/src/database/rawQuery.ts b/src/database/rawQuery.ts index 68ff04d..0fdb5cf 100644 --- a/src/database/rawQuery.ts +++ b/src/database/rawQuery.ts @@ -17,6 +17,7 @@ export const rawQuery = async ( [query, parameters, cb] = parseArguments(invokingResource, query, parameters, cb); return await new Promise((resolve, reject) => { + //@ts-expect-error todo: patch type with executionTime pool.query(query, parameters, (err, result, _, executionTime) => { if (err) return reject(err);