forked from leftypol/leftypol
anti-bot.php: refactor antibot transaction handling
This commit is contained in:
parent
7fed118b1d
commit
efec014bd1
1 changed files with 33 additions and 39 deletions
|
@ -204,7 +204,6 @@ function _create_antibot($pdo, $board, $thread) {
|
||||||
purge_old_antispam();
|
purge_old_antispam();
|
||||||
}
|
}
|
||||||
|
|
||||||
retry_on_deadlock(4, function() use($config, $board, $thread) {
|
|
||||||
// Keep the now invalid timestamps around for a bit to enable users to post if they're still on an old version of
|
// Keep the now invalid timestamps around for a bit to enable users to post if they're still on an old version of
|
||||||
// the HTML page.
|
// the HTML page.
|
||||||
// By virtue of existing, we know that we're making a new version of the page, and the user from now on may just reload.
|
// By virtue of existing, we know that we're making a new version of the page, and the user from now on may just reload.
|
||||||
|
@ -221,12 +220,10 @@ function _create_antibot($pdo, $board, $thread) {
|
||||||
$query->bindValue(':expires', $config['spam']['hidden_inputs_expire']);
|
$query->bindValue(':expires', $config['spam']['hidden_inputs_expire']);
|
||||||
// Throws on error.
|
// Throws on error.
|
||||||
$query->execute();
|
$query->execute();
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
|
||||||
$hash = $antibot->hash();
|
$hash = $antibot->hash();
|
||||||
|
|
||||||
retry_on_deadlock(2, function() use($board, $thread, $hash) {
|
|
||||||
// Insert an antispam with infinite life as the HTML page of a thread might last well beyond the expiry date.
|
// Insert an antispam with infinite life as the HTML page of a thread might last well beyond the expiry date.
|
||||||
$query = prepare('INSERT INTO ``antispam`` VALUES (:board, :thread, :hash, UNIX_TIMESTAMP(), NULL, 0)');
|
$query = prepare('INSERT INTO ``antispam`` VALUES (:board, :thread, :hash, UNIX_TIMESTAMP(), NULL, 0)');
|
||||||
$query->bindValue(':board', $board);
|
$query->bindValue(':board', $board);
|
||||||
|
@ -234,19 +231,16 @@ function _create_antibot($pdo, $board, $thread) {
|
||||||
$query->bindValue(':hash', $hash);
|
$query->bindValue(':hash', $hash);
|
||||||
// Throws on error.
|
// Throws on error.
|
||||||
$query->execute();
|
$query->execute();
|
||||||
});
|
|
||||||
|
$pdo->commit();
|
||||||
} catch (\PDOException $e) {
|
} catch (\PDOException $e) {
|
||||||
|
$pdo->rollBack();
|
||||||
if ($e->errorInfo === null || $e->errorInfo[1] != MYSQL_ER_LOCK_DEADLOCK) {
|
if ($e->errorInfo === null || $e->errorInfo[1] != MYSQL_ER_LOCK_DEADLOCK) {
|
||||||
throw $e;
|
throw $e;
|
||||||
} else {
|
} else {
|
||||||
error_log('Multiple deadlocks on _create_antibot while inserting, skipping');
|
error_log('Deadlock on _create_antibot while inserting, skipping');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (\PDOException $e) {
|
|
||||||
$pdo->rollBack();
|
|
||||||
throw $e;
|
|
||||||
}
|
|
||||||
$pdo->commit();
|
|
||||||
|
|
||||||
return $antibot;
|
return $antibot;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue