Last active
August 5, 2021 21:59
-
-
Save Petah/fd69978af055e86d64f9066dcbb6d2b7 to your computer and use it in GitHub Desktop.
MySQL Poll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$queryConnection = new \mysqli($host, $username, $password, $database); | |
$killConnection = new \mysqli($host, $username, $password, $database); | |
// Execute the query | |
$queryConnection->query($sql, MYSQLI_ASYNC | MYSQLI_USE_RESULT); | |
// Poll for results | |
$start = microtime(true); | |
do { | |
$connections = [$queryConnection]; | |
$error = []; | |
$reject = []; | |
if ($queryConnection->poll($connections, $error, $reject, 0, 500 * 1000)) { | |
break; | |
} | |
$executionTime = microtime(true) - $start; | |
if ($executionTime > $maxExecutionTime / 1000) { | |
$killConnection->query('KILL QUERY ' . $queryConnection->thread_id); | |
throw new \Exception('Query took too long: ' . round($executionTime, 4)); | |
} | |
} while (true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment