Skip to content

Commit

Permalink
stratum: workaround for mysql deadlock error to avoid stratum restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
tpfue committed Mar 14, 2024
1 parent 38a4b94 commit f2693b8
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions stratum/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <mysql/mysqld_error.h>
#include <signal.h>

#ifndef ER_LOCK_DEADLOCK
#define ER_LOCK_DEADLOCK 1213
#endif

void db_reconnect(YAAMP_DB *db)
{
if (g_exiting) {
Expand Down Expand Up @@ -80,6 +84,7 @@ void db_query(YAAMP_DB *db, const char *format, ...)
int len = vsprintf(buffer, format, arglist);
va_end(arglist);

int max_retries = 5;
while(!g_exiting)
{
int res = mysql_query(&db->mysql, buffer);
Expand All @@ -88,10 +93,16 @@ void db_query(YAAMP_DB *db, const char *format, ...)

stratumlog("SQL ERROR: %d, %s\n", res, mysql_error(&db->mysql));
if(res == ER_DUP_ENTRY) break; // rarely seen on new user creation
if(res != CR_SERVER_GONE_ERROR && res != CR_SERVER_LOST) exit(1);
if(res != CR_SERVER_GONE_ERROR && res != CR_SERVER_LOST && res != ER_LOCK_DEADLOCK) exit(1);

usleep(100*YAAMP_MS);
db_reconnect(db);

max_retries--;
if (!max_retries) exit(1);

if ((res == CR_SERVER_GONE_ERROR) || (res == CR_SERVER_LOST)) {
db_reconnect(db);
}
}

free(buffer);
Expand Down

0 comments on commit f2693b8

Please sign in to comment.