feat: implement mysql connection pool with deadlock retry and add reverse geocoding worker service

This commit is contained in:
pringgosutono
2026-04-27 11:46:03 +07:00
parent 0deac1f6da
commit a11e0ea832
3 changed files with 938 additions and 1 deletions

View File

@ -43,4 +43,33 @@ pool.getConnection((err, conn) => {
// console.log('Connection %d released', connection.threadId);
// });
// --- The Deadlock Optimizer ---
// This function wraps your queries and retries them if a deadlock occurs
const queryWithRetry = async (sql, params, retries = 3) => {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
return await pool.execute(sql, params);
} catch (err) {
const isDeadlock = err.code === 'ER_LOCK_DEADLOCK' || err.errno === 1213;
if (isDeadlock && attempt < retries) {
console.warn(`Deadlock detected. Retry attempt ${attempt}...`);
// Wait slightly longer each time (Exponential Backoff)
await new Promise(res => setTimeout(res, attempt * 100));
continue;
}
throw err; // If not a deadlock or out of retries, throw
}
}
};
module.exports = {
pool,
query: queryWithRetry
};
module.exports = pool;