From 17940b652d0a078f5a28b089aa0f5362756d438e Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 6 Jan 2012 12:43:18 +0100 Subject: MWL#192: non-blocking client API, after-review fixes. Main change is that non-blocking operation is now an option that must be explicitly enabled with mysql_option(mysql, MYSQL_OPT_NONBLOCK, ...) before any non-blocing operation can be used. Also the CLIENT_REMEMBER_OPTIONS flag is now always enabled and thus effectively ignored (it was not really useful anyway, and this simplifies things when non-blocking mysql_real_connect() fails). --- client/async_example.c | 31 ++++++++++++++++++++----------- client/mysqltest.cc | 3 +++ 2 files changed, 23 insertions(+), 11 deletions(-) (limited to 'client') diff --git a/client/async_example.c b/client/async_example.c index de9d455171c..ccb60950904 100644 --- a/client/async_example.c +++ b/client/async_example.c @@ -1,17 +1,15 @@ /* Copyright 2011 Kristian Nielsen and Monty Program Ab. - Experiments with non-blocking libmysql. + This file is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. - This is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 2 of the License, or - (at your option) any later version. - - This is distributed in the hope that it will be useful, + This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. You should have received a copy of the GNU General Public License along with this. If not, see . @@ -133,6 +131,7 @@ doit(const char *host, const char *user, const char *password) int status; mysql_init(&mysql); + mysql_options(&mysql, MYSQL_OPT_NONBLOCK, 0); mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "myapp"); /* Returns 0 when done, else flag for what to wait for when need to block. */ @@ -177,8 +176,18 @@ doit(const char *host, const char *user, const char *password) fatal(&mysql, "Got error while retrieving rows"); mysql_free_result(res); - /* I suppose this must be non-blocking too. */ - mysql_close(&mysql); + /* + mysql_close() sends a COM_QUIT packet, and so in principle could block + waiting for the socket to accept the data. + In practise, for many applications it will probably be fine to use the + blocking mysql_close(). + */ + status= mysql_close_start(&mysql); + while (status) + { + status= wait_for_mysql(&mysql, status); + status= mysql_close_cont(&mysql, status); + } } int diff --git a/client/mysqltest.cc b/client/mysqltest.cc index 7aeaa48519d..41a75a3a646 100644 --- a/client/mysqltest.cc +++ b/client/mysqltest.cc @@ -5368,6 +5368,7 @@ void do_connect(struct st_command *command) #endif if (!(con_slot->mysql= mysql_init(0))) die("Failed on mysql_init()"); + mysql_options(con_slot->mysql, MYSQL_OPT_NONBLOCK, 0); if (opt_compress || con_compress) mysql_options(con_slot->mysql, MYSQL_OPT_COMPRESS, NullS); mysql_options(con_slot->mysql, MYSQL_OPT_LOCAL_INFILE, 0); @@ -7491,6 +7492,7 @@ int util_query(MYSQL* org_mysql, const char* query){ /* enable local infile, in non-binary builds often disabled by default */ mysql_options(mysql, MYSQL_OPT_LOCAL_INFILE, 0); + mysql_options(mysql, MYSQL_OPT_NONBLOCK, 0); safe_connect(mysql, "util", org_mysql->host, org_mysql->user, org_mysql->passwd, org_mysql->db, org_mysql->port, org_mysql->unix_socket); @@ -8226,6 +8228,7 @@ int main(int argc, char **argv) if (!(con->name = my_strdup("default", MYF(MY_WME)))) die("Out of memory"); + mysql_options(con->mysql, MYSQL_OPT_NONBLOCK, 0); safe_connect(con->mysql, con->name, opt_host, opt_user, opt_pass, opt_db, opt_port, unix_sock); -- cgit v1.2.1