Skip to content
This repository was archived by the owner on Apr 29, 2025. It is now read-only.

Commit 0a8b15a

Browse files
committed
feat(logger): add logStorage size limit
Prevent excessive memory use when people use ui in production.
1 parent f05d375 commit 0a8b15a

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/config.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { typeCast } from './utils/typeCast';
33
export const mysql_connection_string = GetConvar('mysql_connection_string', '');
44
export let mysql_ui = GetConvar('mysql_ui', 'false') === 'true';
55
export let mysql_slow_query_warning = GetConvarInt('mysql_slow_query_warning', 200);
6-
export let mysql_debug: boolean | string[];
6+
export let mysql_debug: boolean | string[] = false;
7+
8+
// max array size of individual resource query logs
9+
// prevent excessive memory use when people use debug/ui in production
10+
export let mysql_log_size = 0;
711

812
export function setDebug() {
913
mysql_ui = GetConvar('mysql_ui', 'false') === 'true';
@@ -15,6 +19,8 @@ export function setDebug() {
1519
} catch (e) {
1620
mysql_debug = true;
1721
}
22+
23+
mysql_log_size = mysql_debug ? 10000 : GetConvarInt('mysql_log_size', 100);
1824
}
1925

2026
export const mysql_transaction_isolation_level = (() => {
@@ -87,9 +93,7 @@ export const connectionOptions = (() => {
8793
try {
8894
options[key] = JSON.parse(value);
8995
} catch (err) {
90-
console.log(
91-
`^3Failed to parse property ${key} in configuration (${err})!^0`
92-
)
96+
console.log(`^3Failed to parse property ${key} in configuration (${err})!^0`);
9397
}
9498
}
9599
}

src/logger/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PoolConnection, RowDataPacket } from 'mysql2/promise';
2-
import { mysql_debug, mysql_slow_query_warning, mysql_ui } from '../config';
2+
import { mysql_debug, mysql_log_size, mysql_slow_query_warning, mysql_ui } from '../config';
33
import type { CFXCallback, CFXParameters } from '../types';
44
import { dbVersion } from '../database';
55

@@ -109,7 +109,9 @@ export const logQuery = (
109109

110110
if (!mysql_ui) return;
111111

112-
if (logStorage[invokingResource] === undefined) logStorage[invokingResource] = [];
112+
if (!logStorage[invokingResource]) logStorage[invokingResource] = [];
113+
else if (logStorage[invokingResource].length > mysql_log_size) logStorage[invokingResource].splice(0, 1);
114+
113115
logStorage[invokingResource].push({
114116
query,
115117
executionTime,

0 commit comments

Comments
 (0)