improve announce reAnnounce and frequency check

This commit is contained in:
xiaomlove
2024-12-14 23:46:43 +08:00
parent 0f4125fbc8
commit ca21b0b7fe
+7 -4
View File
@@ -29,6 +29,9 @@ $torrentNotExistsKey = "torrent_not_exists";
$authKeyInvalidKey = "authkey_invalid"; $authKeyInvalidKey = "authkey_invalid";
$passkeyInvalidKey = "passkey_invalid"; $passkeyInvalidKey = "passkey_invalid";
$isReAnnounce = false; $isReAnnounce = false;
$reAnnounceInterval = 5;
$frequencyInterval = 30;
$isStoppedOrCompleted = !empty($GLOBALS['event']) && in_array($GLOBALS['event'], array('completed', 'stopped'));
$userAuthenticateKey = ""; $userAuthenticateKey = "";
if (!empty($_GET['authkey'])) { if (!empty($_GET['authkey'])) {
$authkey = $_GET['authkey']; $authkey = $_GET['authkey'];
@@ -47,11 +50,11 @@ if (!empty($_GET['authkey'])) {
$lockString = http_build_query($lockParams); $lockString = http_build_query($lockParams);
$lockKey = "isReAnnounce:" . md5($lockString); $lockKey = "isReAnnounce:" . md5($lockString);
if (!$redis->set($lockKey, TIMENOW, ['nx', 'ex' => 20])) { if (!$redis->set($lockKey, TIMENOW, ['nx', 'ex' => $reAnnounceInterval])) {
$isReAnnounce = true; $isReAnnounce = true;
} }
$reAnnounceCheckByAuthKey = "reAnnounceCheckByAuthKey:$subAuthkey"; $reAnnounceCheckByAuthKey = "reAnnounceCheckByAuthKey:$subAuthkey";
if (!$isReAnnounce && !$redis->set($reAnnounceCheckByAuthKey, TIMENOW, ['nx', 'ex' => 60])) { if (!$isStoppedOrCompleted && !$isReAnnounce && !$redis->set($reAnnounceCheckByAuthKey, TIMENOW, ['nx', 'ex' => $frequencyInterval])) {
$msg = "Request too frequent(a)"; $msg = "Request too frequent(a)";
do_log(sprintf("[ANNOUNCE] %s key: %s already exists, value: %s", $msg, $reAnnounceCheckByAuthKey, TIMENOW)); do_log(sprintf("[ANNOUNCE] %s key: %s already exists, value: %s", $msg, $reAnnounceCheckByAuthKey, TIMENOW));
warn($msg, 300); warn($msg, 300);
@@ -74,7 +77,7 @@ if (!empty($_GET['authkey'])) {
} }
$lockString = http_build_query($lockParams); $lockString = http_build_query($lockParams);
$lockKey = "isReAnnounce:" . md5($lockString); $lockKey = "isReAnnounce:" . md5($lockString);
if (!$redis->set($lockKey, TIMENOW, ['nx', 'ex' => 20])) { if (!$redis->set($lockKey, TIMENOW, ['nx', 'ex' => $reAnnounceInterval])) {
$isReAnnounce = true; $isReAnnounce = true;
} }
} else { } else {
@@ -87,7 +90,7 @@ if ($redis->get("$torrentNotExistsKey:$info_hash")) {
err($msg); err($msg);
} }
$torrentReAnnounceKey = sprintf('reAnnounceCheckByInfoHash:%s:%s', $userAuthenticateKey, $info_hash); $torrentReAnnounceKey = sprintf('reAnnounceCheckByInfoHash:%s:%s', $userAuthenticateKey, $info_hash);
if (!$isReAnnounce && !$redis->set($torrentReAnnounceKey, TIMENOW, ['nx', 'ex' => 60])) { if (!$isStoppedOrCompleted && !$isReAnnounce && !$redis->set($torrentReAnnounceKey, TIMENOW, ['nx', 'ex' => $frequencyInterval])) {
$msg = "Request too frequent(h)"; $msg = "Request too frequent(h)";
do_log(sprintf("[ANNOUNCE] %s key: %s already exists, value: %s", $msg, $torrentReAnnounceKey, TIMENOW)); do_log(sprintf("[ANNOUNCE] %s key: %s already exists, value: %s", $msg, $torrentReAnnounceKey, TIMENOW));
warn($msg, 300); warn($msg, 300);