Skip to content

Commit

Permalink
Fix for read/write timeout for sockets on non Windows platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
9EOR9 committed Feb 23, 2016
1 parent d68b48f commit 38b7870
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
19 changes: 17 additions & 2 deletions plugins/pvio/pvio_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ pvio_socket_connect_sync_or_async(MARIADB_PVIO *pvio,
my_bool pvio_socket_connect(MARIADB_PVIO *pvio, MA_PVIO_CINFO *cinfo)
{
struct st_pvio_socket *csock= NULL;
struct timeval tm;

if (!pvio || !cinfo)
return 1;
Expand Down Expand Up @@ -844,13 +845,27 @@ my_bool pvio_socket_connect(MARIADB_PVIO *pvio, MA_PVIO_CINFO *cinfo)
if (pvio_socket_blocking(pvio, 1, 0) == SOCKET_ERROR)
goto error;
}
#ifdef _WIN32
/* apply timeouts */
if (pvio->timeout[PVIO_WRITE_TIMEOUT] > 0)
{
#ifndef _WIN32
tm.tv_sec= pvio->timeout[PVIO_WRITE_TIMEOUT] / 1000;
tm.tv_usec= pvio->timeout[PVIO_WRITE_TIMEOUT] % 1000;
setsockopt(csock->socket, SOL_SOCKET, SO_SNDTIMEO, (const char *)&tm, sizeof(tm));
#else
setsockopt(csock->socket, SOL_SOCKET, SO_SNDTIMEO, (const char *)&pvio->timeout[PVIO_WRITE_TIMEOUT], sizeof(int));
#endif
}
if (pvio->timeout[PVIO_READ_TIMEOUT] > 0)
setsockopt(csock->socket, SOL_SOCKET, SO_RCVTIMEO, (const char *)&pvio->timeout[PVIO_READ_TIMEOUT], sizeof(int));
{
#ifndef _WIN32
tm.tv_sec= pvio->timeout[PVIO_READ_TIMEOUT] / 1000;
tm.tv_usec= pvio->timeout[PVIO_READ_TIMEOUT] % 1000;
setsockopt(csock->socket, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tm, sizeof(tm));
#else
setsockopt(csock->socket, SOL_SOCKET, SO_RCVTIMEO, (const char *)&pvio->timeout[PVIO_WRITE_TIMEOUT], sizeof(int));
#endif
}
return 0;
error:
if (pvio->data)
Expand Down
19 changes: 19 additions & 0 deletions unittest/libmariadb/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,24 @@ static int test_conc117(MYSQL *mysql)
return OK;
}

static int test_read_timeout(MYSQL *mysql)
{
int timeout= 5, rc;
MYSQL *my= mysql_init(NULL);
mysql_options(my, MYSQL_OPT_READ_TIMEOUT, &timeout);
FAIL_IF(!mysql_real_connect(my, hostname, username, password, schema,
port, socketname, 0), mysql_error(my));

rc= mysql_query(my, "SELECT SLEEP(50)");

FAIL_IF(rc == 0, "error expected");
diag("error: %s", mysql_error(my));

mysql_close(my);

return OK;
}

#ifdef HAVE_REMOTEIO
void *remote_plugin;
static int test_remote1(MYSQL *mysql)
Expand Down Expand Up @@ -1113,6 +1131,7 @@ static int test_zerofill(MYSQL *mysql)
}

struct my_tests_st my_tests[] = {
{"test_read_timeout", test_read_timeout, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"test_zerofill", test_zerofill, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
#ifdef HAVE_REMOTEIO
{"test_remote1", test_remote1, TEST_CONNECTION_NEW, 0, NULL, NULL},
Expand Down

0 comments on commit 38b7870

Please sign in to comment.