Originally posted to : http://stackoverflow.com/questions/37909652/mariadb-non-blocking-with-epoll
Edited for context.

I have single threaded server written in C that accepts TCP/UDP connections based on EPOLL and supports plugins for the multitude of protocol layers we need to support. That bit is fine.

Due to the single threaded nature I wanted to implement a database layer that could utilize the same EPOLL architecture rather then separately iterating over all of the open connections.

We use MariaDB and the MariaDB C connector that supports non blocking functions in it's API as described here.

https://mariadb.com/kb/en/mariadb/using-the-non-blocking-library/

But what I'm finding is not what I expected, and what I was expecting is described below.

First I fire the mysql_real_connect_start() and if it returns zero we dispatch the query immediately as this indicates no blocking was required, although this never happens.

Otherwise I fetch the file descriptor that seems to be immediate and register it with EPOLL and bail back to the main EPOLL loop waiting for events.

s = mysql_get_socket(mysql);

if(s > 0)
{
    brt_socket_set_fds(endpoint, s);
    struct epoll_event event;
    event.data.fd = s;
    event.events = EPOLLRDHUP | EPOLLIN | EPOLLET | EPOLLOUT;
    s = epoll_ctl(efd, EPOLL_CTL_ADD, s, &event);
    if (s == -1) {
        syslog(LOG_ERR, "brd_db : epoll error.");
        // handle error.
    }
...

So then some time later I do get the EPOLLOUT indicating the socket has been opened.

And I dutifully call mysql_real_connect_cont() but at this stage it is still returning a non-zero value, indicating I must wait longer?

But then that is the last EPOLL event I get, except for the EPOLLRDHUP when I guess the MariaDB hangs up after 10 seconds.

Can anyone help me understand if this idea is even workable?

Thanks

David