feat: implement mysql connection pool with deadlock retry and add reverse geocoding worker service
This commit is contained in:
@ -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;
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user