Guest User

Untitled

a guest
Dec 23rd, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <mariadb/mysql.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <poll.h>
  5.  
  6. static int wait_for_mysql(MYSQL *mysql, int status) {
  7. struct pollfd pfd;
  8. int timeout, res;
  9.  
  10. pfd.fd = mysql_get_socket(mysql);
  11. pfd.events =
  12. (status & MYSQL_WAIT_READ ? POLLIN : 0) |
  13. (status & MYSQL_WAIT_WRITE ? POLLOUT : 0) |
  14. (status & MYSQL_WAIT_EXCEPT ? POLLPRI : 0);
  15. if (status & MYSQL_WAIT_TIMEOUT)
  16. timeout = 1000*mysql_get_timeout_value(mysql);
  17. else
  18. timeout = -1;
  19. res = poll(&pfd, 1, timeout);
  20. if (res == 0)
  21. return MYSQL_WAIT_TIMEOUT;
  22. else if (res < 0)
  23. return MYSQL_WAIT_TIMEOUT;
  24. else {
  25. int status = 0;
  26. if (pfd.revents & POLLIN) status |= MYSQL_WAIT_READ;
  27. if (pfd.revents & POLLOUT) status |= MYSQL_WAIT_WRITE;
  28. if (pfd.revents & POLLPRI) status |= MYSQL_WAIT_EXCEPT;
  29. return status;
  30. }
  31. }
  32.  
  33. void die(char const *msg)
  34. {
  35. fprintf(stderr, "error in %s\n", msg);
  36. exit(2);
  37. }
  38.  
  39. int main()
  40. {
  41. char const *host = getenv("OCAML_MARIADB_HOST");
  42. char const *user = getenv("OCAML_MARIADB_USER");
  43. char const *pass = getenv("OCAML_MARIADB_PASS");
  44. char const *db = getenv("OCAML_MARIADB_DB");
  45. MYSQL *ret;
  46. int status;
  47. for (int i = 0; i < 500000; ++i) {
  48. MYSQL *mysql = mysql_init(NULL);
  49. mysql_options(mysql, MYSQL_OPT_NONBLOCK, 0);
  50.  
  51. status = mysql_real_connect_start(&ret, mysql, host, user, pass, db, 3306, NULL, 0);
  52. while (status) {
  53. status = wait_for_mysql(mysql, status);
  54. status = mysql_real_connect_cont(&ret, mysql, status);
  55. }
  56. if (!ret) die("mysql_real_connect_*");
  57.  
  58. status = mysql_close_start(mysql);
  59. while (status) {
  60. status = wait_for_mysql(mysql, status);
  61. status = mysql_close_cont(mysql, status);
  62. }
  63. }
  64. }
Add Comment
Please, Sign In to add comment