Skip to content

Commit

Permalink
Limit number of server-initiated instant failovers followed by client
Browse files Browse the repository at this point in the history
Set the limit to 8 for now. This will prevent the client from getting stuck
in redirect loops.

All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer Amazon Web
Services, Inc.
  • Loading branch information
dlenski committed Apr 25, 2024
1 parent c93c566 commit 041b095
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion libmariadb/mariadb_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
#define MA_RPL_VERSION_HACK "5.5.5-"

#define CHARSET_NAME_LEN 64
#define INSTANT_FAILOVER_LIMIT 8

#undef max_allowed_packet
#undef net_buffer_length
Expand Down Expand Up @@ -1589,7 +1590,7 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
my_bool is_multi= 0;
char *host_copy= NULL;
struct st_host *host_list= NULL;
int connect_attempts= 0;
int connect_attempts= 0, instant_failovers=0;

if (!mysql->methods)
mysql->methods= &MARIADB_DEFAULT_METHODS;
Expand Down Expand Up @@ -2002,6 +2003,13 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
/* Client has disabled instant failover. Fall through and treat this
* as a "normal" error. */
}
else if (instant_failovers >= INSTANT_FAILOVER_LIMIT)
{
/* Too many instant failovers */
my_set_error(mysql, ER_INSTANT_FAILOVER, SQLSTATE_UNKNOWN,
"Too many instant failovers (>= %d)",
INSTANT_FAILOVER_LIMIT);
}
else
{
char *p= mysql->net.last_error; /* Should look like '|message|host[:port]' */
Expand All @@ -2020,6 +2028,7 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
port= 0;
}
fprintf(stderr, "Got instant failover to '%s' (port %d)\n", host, port);
++instant_failovers;
goto tcp_redirect;
}
}
Expand Down

0 comments on commit 041b095

Please sign in to comment.