Bug #8938 libmysql should loop through all hosts returned by getaddrinfo
Submitted: 4 Mar 2005 2:42
Reporter: Andrew Donkin Email Updates:
Status: Open Impact on me:
Category:MySQL Server: C API (client library) Severity:S4 (Feature request)
Version: OS:Any
Assigned to: CPU Architecture:Any
Triage: D5 (Feature request)

[4 Mar 2005 2:42] Andrew Donkin
I like OpenLDAP's behaviour of looping through all the hosts returned by getaddrinfo(), attempting to connect to each until a responsive server is found.  This would make a fault-tolerant MySQL client trivial to write.


1: if the connection timeout is applied per server, the client may block for a lot longer than expected

2: Aside from communications problems, what errors should cause the client to try the next server?  Access denied, unknown database, ... ?

How to repeat:
Feature request.

Suggested fix:
Without concern for errors or portability:

struct addrinfo *addr, *addrlist;
struct addrinfo hints = {0, PF_UNSPEC, SOCK_STREAM,
                         0, 0, 0, 0, 0};

int gai_result = getaddrinfo(host, 0, &hints, &addrlist);
if (gai_result) {
  Unknown host.
for (addr = addrlist; addr; addr = addr->ai_next) {
#define IPNAMELEN 20
  char ipnamebuff[IPNAMELEN];
  const char *ipname = inet_ntop(((struct sockaddr_in *)addr->ai_addr)->sin_family,
				 (void *)&((struct sockaddr_in *)addr->ai_addr)->sin_addr,
				 ipnamebuff, IPNAMELEN-1);

  mysql_real_connect(mysql, ipname, user, pass, database, 0, 0, flags);