Skip to content

Commit 088fdac

Browse files
committed
Fixed vio for non-blocking API calls
1 parent b448d03 commit 088fdac

File tree

13 files changed

+365
-145
lines changed

13 files changed

+365
-145
lines changed

include/violite.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ size_t vio_write(Vio* vio, const gptr buf, size_t size);
8787
* Whenever the socket is set to blocking mode or not.
8888
*/
8989
int vio_blocking( Vio* vio,
90-
my_bool onoff);
90+
my_bool onoff,
91+
my_bool *prevmode);
9192
my_bool vio_is_blocking( Vio* vio);
9293
/*
9394
* setsockopt TCP_NODELAY at IPPROTO_TCP level, when possible.
@@ -135,6 +136,7 @@ void vio_in_addr(Vio *vio, struct in_addr *in);
135136

136137
/* Return 1 if there is data to be read */
137138
my_bool vio_poll_read(Vio *vio,uint timeout);
139+
int vio_wait_or_timeout(Vio *vio, my_bool is_read, int timeout);
138140

139141

140142
struct st_vio
@@ -146,7 +148,8 @@ struct st_vio
146148
struct sockaddr_in local; /* Local internet address */
147149
struct sockaddr_in remote; /* Remote internet address */
148150
struct mysql_async_context *async_context; /* For non-blocking API */
149-
151+
int write_timeout;
152+
int read_timeout;
150153
enum enum_vio_type type; /* Type of connection */
151154
char desc[30]; /* String description */
152155
#ifdef HAVE_OPENSSL

libmariadb/libmariadb.c

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ static int connect2(my_socket s, const struct sockaddr *name, size_t namelen,
211211
poll_fd.fd= s;
212212

213213
/* connection timeout in milliseconds */
214-
res= poll(&poll_fd, 1, (timeout > -1) ? timeout * 1000 : timeout);
214+
res= poll(&poll_fd, 1, timeout);
215215

216216
switch(res)
217217
{
@@ -248,6 +248,24 @@ static int connect2(my_socket s, const struct sockaddr *name, size_t namelen,
248248
return (0); /* ok */
249249
}
250250

251+
static int
252+
connect_sync_or_async(MYSQL *mysql, NET *net, my_socket fd,
253+
const struct sockaddr *name, uint namelen)
254+
{
255+
int vio_timeout= (mysql->options.connect_timeout >= 0) ?
256+
mysql->options.connect_timeout * 1000 : -1;
257+
258+
if (mysql->options.extension && mysql->options.extension->async_context &&
259+
mysql->options.extension->async_context->active)
260+
{
261+
my_bool old_mode;
262+
vio_blocking(net->vio, FALSE, &old_mode);
263+
return my_connect_async(mysql->options.extension->async_context, fd,
264+
name, namelen, vio_timeout);
265+
}
266+
267+
return connect2(fd, name, namelen, vio_timeout);
268+
}
251269
/*
252270
** Create a named pipe connection
253271
*/
@@ -291,7 +309,7 @@ HANDLE create_named_pipe(NET *net, uint connect_timeout, char **arg_host,
291309
return INVALID_HANDLE_VALUE;
292310
}
293311
/* wait for for an other instance */
294-
if (! WaitNamedPipe(szPipeName, connect_timeout*1000) )
312+
if (! WaitNamedPipe(szPipeName, connect_timeout) )
295313
{
296314
net->last_errno=CR_NAMEDPIPEWAIT_ERROR;
297315
sprintf(net->last_error,ER(net->last_errno),host, unix_socket,
@@ -936,7 +954,7 @@ static void mysql_read_default_options(struct st_mysql_options *options,
936954
options->named_pipe=1; /* Force named pipe */
937955
break;
938956
case OPT_connect_timeout:
939-
case OPT_timeout:
957+
case OPT_timeout:
940958
if (opt_arg)
941959
options->connect_timeout=atoi(opt_arg);
942960
break;
@@ -1577,10 +1595,9 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
15771595
bzero((char*) &UNIXaddr,sizeof(UNIXaddr));
15781596
UNIXaddr.sun_family = AF_UNIX;
15791597
strmov(UNIXaddr.sun_path, unix_socket);
1580-
if (connect2(sock,(struct sockaddr *) &UNIXaddr, sizeof(UNIXaddr),
1581-
mysql->options.connect_timeout) <0)
1598+
if (connect_sync_or_async(mysql, net, sock,
1599+
(struct sockaddr *) &UNIXaddr, sizeof(UNIXaddr)))
15821600
{
1583-
printf("err\n");
15841601
DBUG_PRINT("error",("Got error %d on connect to local server",socket_errno));
15851602
my_set_error(mysql, CR_CONNECTION_ERROR, SQLSTATE_UNKNOWN, ER(CR_CONNECTION_ERROR),
15861603
unix_socket, socket_errno);
@@ -1675,17 +1692,20 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
16751692
freeaddrinfo(res);
16761693
goto error;
16771694
}
1678-
if (!(rc= connect2(sock, save_res->ai_addr, save_res->ai_addrlen,
1679-
mysql->options.connect_timeout)))
1695+
rc= connect_sync_or_async(mysql, net, sock,
1696+
save_res->ai_addr, save_res->ai_addrlen);
1697+
if (!rc)
16801698
{
1681-
if (socket_block(sock, 1) == SOCKET_ERROR)
1699+
if (mysql->options.extension && mysql->options.extension->async_context &&
1700+
mysql->options.extension->async_context->active)
1701+
break;
1702+
else if (socket_block(sock, 1) == SOCKET_ERROR)
16821703
{
1683-
closesocket(sock);
1684-
continue;
1704+
closesocket(sock);
1705+
continue;
16851706
}
16861707
break; /* success! */
16871708
}
1688-
16891709
vio_delete(mysql->net.vio);
16901710
mysql->net.vio= NULL;
16911711
}
@@ -1708,6 +1728,13 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
17081728
}
17091729
}
17101730

1731+
/* set timeouts */
1732+
net->vio->read_timeout= mysql->options.read_timeout;
1733+
net->vio->write_timeout= mysql->options.write_timeout;
1734+
1735+
if (mysql->options.extension && mysql->options.extension->async_context)
1736+
net->vio->async_context= mysql->options.extension->async_context;
1737+
17111738
if (!net->vio || my_net_init(net, net->vio))
17121739
{
17131740
vio_delete(net->vio);
@@ -1720,16 +1747,16 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
17201747

17211748

17221749
/* set read timeout */
1723-
if (mysql->options.read_timeout)
1750+
if (mysql->options.read_timeout >= 0)
17241751
vio_read_timeout(net->vio, mysql->options.read_timeout);
17251752

17261753
/* set write timeout */
1727-
if (mysql->options.write_timeout)
1754+
if (mysql->options.write_timeout >= 0)
17281755
vio_write_timeout(net->vio, mysql->options.read_timeout);
17291756
/* Get version info */
17301757
mysql->protocol_version= PROTOCOL_VERSION; /* Assume this */
1731-
if (mysql->options.connect_timeout &&
1732-
vio_poll_read(net->vio, mysql->options.connect_timeout))
1758+
if (mysql->options.connect_timeout >= 0 &&
1759+
vio_wait_or_timeout(net->vio, FALSE, mysql->options.connect_timeout * 1000) < 1)
17331760
{
17341761
my_set_error(mysql, CR_SERVER_LOST, SQLSTATE_UNKNOWN,
17351762
ER(CR_SERVER_LOST_EXTENDED),

libmariadb/libmariadb_exports.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ EXPORTS
109109
mysql_ping
110110
mysql_ping_cont
111111
mysql_ping_start
112-
mysql_plugin_options
113112
mysql_ps_fetch_functions DATA
114113
mysql_query
115114
mysql_query_cont

libmariadb/ma_secure.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <ma_secure.h>
2626
#include <errmsg.h>
2727
#include <violite.h>
28+
#include <mysql_async.h>
29+
#include <my_context.h>
2830

2931
static my_bool my_ssl_initialized= FALSE;
3032
static SSL_CTX *SSL_context= NULL;
@@ -395,7 +397,7 @@ int my_ssl_connect(SSL *ssl)
395397

396398
/* Set socket to blocking if not already set */
397399
if (!(blocking= vio_is_blocking(mysql->net.vio)))
398-
vio_blocking(mysql->net.vio, TRUE);
400+
vio_blocking(mysql->net.vio, TRUE, 0);
399401

400402
SSL_clear(ssl);
401403
SSL_SESSION_set_timeout(SSL_get_session(ssl),
@@ -407,7 +409,7 @@ int my_ssl_connect(SSL *ssl)
407409
my_SSL_error(mysql);
408410
/* restore blocking mode */
409411
if (!blocking)
410-
vio_blocking(mysql->net.vio, FALSE);
412+
vio_blocking(mysql->net.vio, FALSE, 0);
411413
DBUG_RETURN(1);
412414
}
413415

@@ -489,8 +491,11 @@ size_t my_ssl_write(Vio *vio, const uchar* buf, size_t size)
489491
{
490492
size_t written;
491493
DBUG_ENTER("my_ssl_write");
492-
493-
written= SSL_write((SSL*) vio->ssl, buf, size);
494+
if (vio->async_context && vio->async_context->active)
495+
written= my_ssl_write_async(vio->async_context, (SSL *)vio->ssl, buf,
496+
size);
497+
else
498+
written= SSL_write((SSL*) vio->ssl, buf, size);
494499
DBUG_RETURN(written);
495500
}
496501

@@ -511,7 +516,10 @@ size_t my_ssl_read(Vio *vio, uchar* buf, size_t size)
511516
size_t read;
512517
DBUG_ENTER("my_ssl_read");
513518

514-
read= SSL_read((SSL*) vio->ssl, buf, size);
519+
if (vio->async_context && vio->async_context->active)
520+
read= my_ssl_read_async(vio->async_context, (SSL *)vio->ssl, buf, size);
521+
else
522+
read= SSL_read((SSL*) vio->ssl, buf, size);
515523
DBUG_RETURN(read);
516524
}
517525

libmariadb/mysql_async.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@
4141
*/
4242
#define WIN_SET_NONBLOCKING(mysql) { \
4343
my_bool old_mode; \
44-
if ((mysql)->net.vio) vio_blocking((mysql)->net.vio, FALSE); \
44+
if ((mysql)->net.vio) vio_blocking((mysql)->net.vio, FALSE, &old_mode); \
4545
}
4646
#else
4747
#define WIN_SET_NONBLOCKING(mysql)
4848
#endif
4949

50+
extern void mysql_close_slow_part(MYSQL *mysql);
51+
5052

5153
void
5254
my_context_install_suspend_resume_hook(struct mysql_async_context *b,

libmariadb/net.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ int my_net_init(NET *net, Vio* vio)
132132
net->fd = vio_fd(vio); /* For perl DBI/DBD */
133133
#if defined(MYSQL_SERVER) && !defined(__WIN32) && !defined(__EMX__) && !defined(OS2)
134134
if (!(test_flags & TEST_BLOCKING))
135-
vio_blocking(vio, FALSE);
135+
vio_blocking(vio, FALSE, 0);
136136
#endif
137137
vio_fastsend(vio);
138138
}
@@ -441,7 +441,7 @@ net_real_write(NET *net,const char *packet,size_t len)
441441
{ /* Always true for client */
442442
if (!vio_is_blocking(net->vio))
443443
{
444-
while (vio_blocking(net->vio, TRUE) < 0)
444+
while (vio_blocking(net->vio, TRUE, 0) < 0)
445445
{
446446
if (vio_should_retry(net->vio) && retry_count++ < RETRY_COUNT)
447447
continue;
@@ -497,7 +497,7 @@ net_real_write(NET *net,const char *packet,size_t len)
497497
if (thr_alarm_in_use(&alarmed))
498498
{
499499
thr_end_alarm(&alarmed);
500-
vio_blocking(net->vio, net_blocking);
500+
vio_blocking(net->vio, net_blocking, 0);
501501
}
502502
net->reading_or_writing=0;
503503
DBUG_RETURN(((int) (pos != end)));
@@ -522,7 +522,7 @@ static void my_net_skip_rest(NET *net, ulong remain, thr_alarm_t *alarmed,
522522
if (!thr_alarm_in_use(alarmed))
523523
{
524524
if (thr_alarm(alarmed,net->timeout,alarm_buff) ||
525-
(!vio_is_blocking(net->vio) && vio_blocking(net->vio,TRUE) < 0))
525+
(!vio_is_blocking(net->vio) && vio_blocking(net->vio,TRUE, 0) < 0))
526526
return; /* Can't setup, abort */
527527
}
528528
while (remain > 0)
@@ -592,7 +592,7 @@ my_real_read(NET *net, size_t *complen)
592592
{
593593
if (!vio_is_blocking(net->vio))
594594
{
595-
while (vio_blocking(net->vio,TRUE) < 0)
595+
while (vio_blocking(net->vio,TRUE, 0) < 0)
596596
{
597597
if (vio_should_retry(net->vio) &&
598598
retry_count++ < RETRY_COUNT)
@@ -702,7 +702,7 @@ my_real_read(NET *net, size_t *complen)
702702
if (thr_alarm_in_use(&alarmed))
703703
{
704704
thr_end_alarm(&alarmed);
705-
vio_blocking(net->vio, net_blocking);
705+
vio_blocking(net->vio, net_blocking, 0);
706706
}
707707
net->reading_or_writing=0;
708708
return(len);

libmariadb/strtoll.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include <my_global.h>
2121
#include <m_string.h>
22-
#if !defined(HAVE_STRTOLL) && defined(HAVE_LONG_LONG)
22+
#if !defined(_WIN32) && !defined(HAVE_STRTOLL) && defined(HAVE_LONG_LONG)
2323
#define USE_LONGLONG
2424

2525
#define strtoll glob_strtoll /* Fix for True64 */

libmariadb/strtoull.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include <my_global.h>
2121
#include <m_string.h>
22-
#if !defined(HAVE_STRTOULL) && defined(HAVE_LONG_LONG)
22+
#if !defined(_WIN32) && !defined(HAVE_STRTOULL) && defined(HAVE_LONG_LONG)
2323
#define USE_UNSIGNED
2424
#define USE_LONGLONG
2525
#include "strto.c"

0 commit comments

Comments
 (0)