Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mariadb/mysql.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <poll.h>
- static int wait_for_mysql(MYSQL *mysql, int status) {
- struct pollfd pfd;
- int timeout, res;
- pfd.fd = mysql_get_socket(mysql);
- pfd.events =
- (status & MYSQL_WAIT_READ ? POLLIN : 0) |
- (status & MYSQL_WAIT_WRITE ? POLLOUT : 0) |
- (status & MYSQL_WAIT_EXCEPT ? POLLPRI : 0);
- if (status & MYSQL_WAIT_TIMEOUT)
- timeout = 1000*mysql_get_timeout_value(mysql);
- else
- timeout = -1;
- res = poll(&pfd, 1, timeout);
- if (res == 0)
- return MYSQL_WAIT_TIMEOUT;
- else if (res < 0)
- return MYSQL_WAIT_TIMEOUT;
- else {
- int status = 0;
- if (pfd.revents & POLLIN) status |= MYSQL_WAIT_READ;
- if (pfd.revents & POLLOUT) status |= MYSQL_WAIT_WRITE;
- if (pfd.revents & POLLPRI) status |= MYSQL_WAIT_EXCEPT;
- return status;
- }
- }
- void die(char const *msg)
- {
- fprintf(stderr, "error in %s\n", msg);
- exit(2);
- }
- int main()
- {
- char const *host = getenv("OCAML_MARIADB_HOST");
- char const *user = getenv("OCAML_MARIADB_USER");
- char const *pass = getenv("OCAML_MARIADB_PASS");
- char const *db = getenv("OCAML_MARIADB_DB");
- MYSQL *ret;
- int status;
- for (int i = 0; i < 500000; ++i) {
- MYSQL *mysql = mysql_init(NULL);
- mysql_options(mysql, MYSQL_OPT_NONBLOCK, 0);
- status = mysql_real_connect_start(&ret, mysql, host, user, pass, db, 3306, NULL, 0);
- while (status) {
- status = wait_for_mysql(mysql, status);
- status = mysql_real_connect_cont(&ret, mysql, status);
- }
- if (!ret) die("mysql_real_connect_*");
- status = mysql_close_start(mysql);
- while (status) {
- status = wait_for_mysql(mysql, status);
- status = mysql_close_cont(mysql, status);
- }
- }
- }
Add Comment
Please, Sign In to add comment