Skip to content

Commit

Permalink
feat(logger): add logStorage size limit
Browse files Browse the repository at this point in the history
Prevent excessive memory use when people use ui in production.
  • Loading branch information
thelindat committed Feb 2, 2024
1 parent f05d375 commit 0a8b15a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/config.ts
Expand Up @@ -3,7 +3,11 @@ import { typeCast } from './utils/typeCast';
export const mysql_connection_string = GetConvar('mysql_connection_string', '');
export let mysql_ui = GetConvar('mysql_ui', 'false') === 'true';
export let mysql_slow_query_warning = GetConvarInt('mysql_slow_query_warning', 200);
export let mysql_debug: boolean | string[];
export let mysql_debug: boolean | string[] = false;

// max array size of individual resource query logs
// prevent excessive memory use when people use debug/ui in production
export let mysql_log_size = 0;

export function setDebug() {
mysql_ui = GetConvar('mysql_ui', 'false') === 'true';
Expand All @@ -15,6 +19,8 @@ export function setDebug() {
} catch (e) {
mysql_debug = true;
}

mysql_log_size = mysql_debug ? 10000 : GetConvarInt('mysql_log_size', 100);
}

export const mysql_transaction_isolation_level = (() => {
Expand Down Expand Up @@ -87,9 +93,7 @@ export const connectionOptions = (() => {
try {
options[key] = JSON.parse(value);
} catch (err) {
console.log(
`^3Failed to parse property ${key} in configuration (${err})!^0`
)
console.log(`^3Failed to parse property ${key} in configuration (${err})!^0`);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/logger/index.ts
@@ -1,5 +1,5 @@
import { PoolConnection, RowDataPacket } from 'mysql2/promise';
import { mysql_debug, mysql_slow_query_warning, mysql_ui } from '../config';
import { mysql_debug, mysql_log_size, mysql_slow_query_warning, mysql_ui } from '../config';
import type { CFXCallback, CFXParameters } from '../types';
import { dbVersion } from '../database';

Expand Down Expand Up @@ -109,7 +109,9 @@ export const logQuery = (

if (!mysql_ui) return;

if (logStorage[invokingResource] === undefined) logStorage[invokingResource] = [];
if (!logStorage[invokingResource]) logStorage[invokingResource] = [];
else if (logStorage[invokingResource].length > mysql_log_size) logStorage[invokingResource].splice(0, 1);

logStorage[invokingResource].push({
query,
executionTime,
Expand Down

0 comments on commit 0a8b15a

Please sign in to comment.