Skip to content

Commit c2a3346

Browse files
authored
FIX: CA_Storage: Add more safety for slow DB. (#197)
* Add queue on gagData save * Add Info messages for Queue loadings * Add support queue on gag save for CSBans * compile fix * Add warning for Storage_Remove when connection not initialized
1 parent f9fcac1 commit c2a3346

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

cstrike/addons/amxmodx/scripting/CA_Storage_CSBans.sma

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <ChatAdditions>
77
#include <CA_StorageAPI_endpoint>
8+
#include <CA_GAG_API>
89

910
#pragma ctrlchar '\'
1011
#pragma dynamic 131072
@@ -22,7 +23,8 @@ const MAX_REASON_LENGTH = 256;
2223
new g_query[QUERY_LENGTH]
2324

2425
new Handle: g_tuple = Empty_Handle
25-
new Queue: g_queueLoad = Invalid_Queue
26+
new Queue: g_queueLoad = Invalid_Queue,
27+
Queue: g_queueSave = Invalid_Queue
2628

2729
new ca_storage_host[64],
2830
ca_storage_user[128],
@@ -46,6 +48,7 @@ public plugin_init() {
4648
AutoExecConfig(true, "CA_Storage_CSBans", "ChatAdditions")
4749

4850
g_queueLoad = QueueCreate(MAX_AUTHID_LENGTH)
51+
g_queueSave = QueueCreate(gagData_s)
4952
}
5053
public OnConfigsExecuted() {
5154
g_tuple = SQL_MakeDbTuple(ca_storage_host, ca_storage_user, ca_storage_pass, ca_storage_dbname)
@@ -58,6 +61,7 @@ public plugin_end() {
5861
}
5962

6063
QueueDestroy(g_queueLoad)
64+
QueueDestroy(g_queueSave)
6165
}
6266
public plugin_natives() {
6367
RegisterNatives()
@@ -122,9 +126,35 @@ public handle_StorageCreated(failstate, Handle: query, error[], errnum, data[],
122126
g_storageInitialized = true
123127
ExecuteForward(g_fwd_StorageInitialized, g_ret)
124128

129+
// Load prepared data from storage
130+
new queueCounter
125131
for(new i, len = QueueSize(g_queueLoad); i < len; i++) {
126132
new authID[MAX_AUTHID_LENGTH]; QueuePopString(g_queueLoad, authID, charsmax(authID))
127133
Storage_Load(authID)
134+
135+
++queueCounter
136+
}
137+
138+
if(queueCounter) {
139+
CA_Log(logLevel_Info, "Loaded %i queue gags from DB (slow DB connection issue)", queueCounter)
140+
queueCounter = 0
141+
}
142+
143+
// Save prepared data to storage
144+
for(new i, len = QueueSize(g_queueSave); i < len; i++) {
145+
new gagData[gagData_s]; QueuePopArray(g_queueSave, gagData, sizeof(gagData))
146+
147+
Storage_Save(gagData[gd_name], gagData[gd_authID], gagData[gd_IP],
148+
gagData[gd_reason][r_name], gagData[gd_adminName], gagData[gd_adminAuthID],
149+
gagData[gd_adminIP], gagData[gd_expireAt], gagData[gd_reason][r_flags]
150+
)
151+
152+
++queueCounter
153+
}
154+
155+
if(queueCounter) {
156+
CA_Log(logLevel_Info, "Saved %i queue gags to DB (slow DB connection issue)", queueCounter)
157+
queueCounter = 0
128158
}
129159
}
130160

@@ -133,6 +163,27 @@ Storage_Save(const name[], const authID[], const IP[],
133163
const reason[], const adminName[], const adminAuthID[],
134164
const adminIP[], const expireAt, const flags) {
135165

166+
if(!g_storageInitialized) {
167+
new gagData[gagData_s]; {
168+
copy(gagData[gd_name], charsmax(gagData[gd_name]), name)
169+
copy(gagData[gd_authID], charsmax(gagData[gd_authID]), authID)
170+
copy(gagData[gd_IP], charsmax(gagData[gd_IP]), IP)
171+
172+
copy(gagData[gd_adminName], charsmax(gagData[gd_adminName]), adminName)
173+
copy(gagData[gd_adminAuthID], charsmax(gagData[gd_adminAuthID]), adminAuthID)
174+
copy(gagData[gd_adminIP], charsmax(gagData[gd_adminIP]), adminIP)
175+
176+
copy(gagData[gd_reason][r_name], charsmax(gagData[r_name]), reason)
177+
gagData[gd_reason][r_flags] = gag_flags_s: flags
178+
179+
gagData[gd_expireAt] = expireAt
180+
}
181+
182+
QueuePushArray(g_queueSave, gagData)
183+
184+
return
185+
}
186+
136187
new name_safe[MAX_NAME_LENGTH * 2];
137188
SQL_QuoteString(Empty_Handle, name_safe, charsmax(name_safe), name);
138189

@@ -279,6 +330,14 @@ public handle_Loaded(failstate, Handle: query, error[], errnum, data[], size, Fl
279330
}
280331

281332
Storage_Remove(const authID[]) {
333+
if(g_storageInitialized || g_tuple == Empty_Handle) {
334+
CA_Log(logLevel_Warning, "Storage_Remove(): Storage connection not initialized. Query not executed. (g_storageInitialized=%i, g_tuple=%i)",
335+
g_storageInitialized, g_tuple
336+
)
337+
338+
return
339+
}
340+
282341
formatex(g_query, charsmax(g_query), "DELETE FROM %s ", SQL_TBL_GAGS); {
283342
strcat(g_query, fmt("WHERE (authid = '%s')", authID), charsmax(g_query))
284343
}

cstrike/addons/amxmodx/scripting/CA_Storage_GameCMS.sma

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const MAX_REASON_LENGTH = 256;
2323
new g_query[QUERY_LENGTH]
2424

2525
new Handle: g_tuple = Empty_Handle
26-
new Queue: g_queueLoad = Invalid_Queue
26+
new Queue: g_queueLoad = Invalid_Queue,
27+
Queue: g_queueSave = Invalid_Queue
2728

2829
new g_serverID = -1
2930

@@ -49,6 +50,7 @@ public plugin_init() {
4950
AutoExecConfig(true, "CA_Storage_GameCMS", "ChatAdditions")
5051

5152
g_queueLoad = QueueCreate(MAX_AUTHID_LENGTH)
53+
g_queueSave = QueueCreate(gagData_s)
5254
}
5355
public OnConfigsExecuted() {
5456
g_tuple = SQL_MakeDbTuple(ca_storage_host, ca_storage_user, ca_storage_pass, ca_storage_dbname)
@@ -62,6 +64,7 @@ public plugin_end() {
6264
}
6365

6466
QueueDestroy(g_queueLoad)
67+
QueueDestroy(g_queueSave)
6568
}
6669
public plugin_natives() {
6770
RegisterNatives()
@@ -135,6 +138,27 @@ Storage_Save(const name[], const authID[], const IP[],
135138
const reason[], const adminName[], const adminAuthID[],
136139
const adminIP[], const expireAt, const flags) {
137140

141+
if(!g_storageInitialized) {
142+
new gagData[gagData_s]; {
143+
copy(gagData[gd_name], charsmax(gagData[gd_name]), name)
144+
copy(gagData[gd_authID], charsmax(gagData[gd_authID]), authID)
145+
copy(gagData[gd_IP], charsmax(gagData[gd_IP]), IP)
146+
147+
copy(gagData[gd_adminName], charsmax(gagData[gd_adminName]), adminName)
148+
copy(gagData[gd_adminAuthID], charsmax(gagData[gd_adminAuthID]), adminAuthID)
149+
copy(gagData[gd_adminIP], charsmax(gagData[gd_adminIP]), adminIP)
150+
151+
copy(gagData[gd_reason][r_name], charsmax(gagData[r_name]), reason)
152+
gagData[gd_reason][r_flags] = gag_flags_s: flags
153+
154+
gagData[gd_expireAt] = expireAt
155+
}
156+
157+
QueuePushArray(g_queueSave, gagData)
158+
159+
return
160+
}
161+
138162
#pragma unused adminIP, adminAuthID, IP
139163
new name_safe[MAX_NAME_LENGTH * 2];
140164
SQL_QuoteString(Empty_Handle, name_safe, charsmax(name_safe), name);
@@ -282,6 +306,14 @@ public handle_Loaded(failstate, Handle: query, error[], errnum, data[], size, Fl
282306
}
283307

284308
Storage_Remove(const authID[]) {
309+
if(g_storageInitialized || g_tuple == Empty_Handle) {
310+
CA_Log(logLevel_Warning, "Storage_Remove(): Storage connection not initialized. Query not executed. (g_storageInitialized=%i, g_tuple=%i)",
311+
g_storageInitialized, g_tuple
312+
)
313+
314+
return
315+
}
316+
285317
formatex(g_query, charsmax(g_query), "DELETE FROM %s ", SQL_TBL_GAGS); {
286318
strcat(g_query, fmt("WHERE (authid = '%s')", authID), charsmax(g_query))
287319
}
@@ -332,9 +364,35 @@ public handle_GetServerID(failstate, Handle: query, error[], errnum, data[], siz
332364
g_storageInitialized = true
333365
ExecuteForward(g_fwd_StorageInitialized, g_ret)
334366

367+
// Load prepared data from storage
368+
new queueCounter
335369
for(new i, len = QueueSize(g_queueLoad); i < len; i++) {
336370
new authID[MAX_AUTHID_LENGTH]; QueuePopString(g_queueLoad, authID, charsmax(authID))
337371
Storage_Load(authID)
372+
373+
++queueCounter
374+
}
375+
376+
if(queueCounter) {
377+
CA_Log(logLevel_Info, "Loaded %i queue gags from DB (slow DB connection issue)", queueCounter)
378+
queueCounter = 0
379+
}
380+
381+
// Save prepared data to storage
382+
for(new i, len = QueueSize(g_queueSave); i < len; i++) {
383+
new gagData[gagData_s]; QueuePopArray(g_queueSave, gagData, sizeof(gagData))
384+
385+
Storage_Save(gagData[gd_name], gagData[gd_authID], gagData[gd_IP],
386+
gagData[gd_reason][r_name], gagData[gd_adminName], gagData[gd_adminAuthID],
387+
gagData[gd_adminIP], gagData[gd_expireAt], gagData[gd_reason][r_flags]
388+
)
389+
390+
++queueCounter
391+
}
392+
393+
if(queueCounter) {
394+
CA_Log(logLevel_Info, "Saved %i queue gags to DB (slow DB connection issue)", queueCounter)
395+
queueCounter = 0
338396
}
339397
}
340398

0 commit comments

Comments
 (0)