From ca21b0b7fe0521aca54db6a29ef33d3547acb4c8 Mon Sep 17 00:00:00 2001 From: xiaomlove <1939737565@qq.com> Date: Sat, 14 Dec 2024 23:46:43 +0800 Subject: [PATCH] improve announce reAnnounce and frequency check --- public/announce.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/public/announce.php b/public/announce.php index bffbf23a..f341bba8 100644 --- a/public/announce.php +++ b/public/announce.php @@ -29,6 +29,9 @@ $authKeyInvalidKey = "authkey_invalid"; $passkeyInvalidKey = "passkey_invalid"; $isReAnnounce = false; +$reAnnounceInterval = 5; +$frequencyInterval = 30; +$isStoppedOrCompleted = !empty($GLOBALS['event']) && in_array($GLOBALS['event'], array('completed', 'stopped')); $userAuthenticateKey = ""; if (!empty($_GET['authkey'])) { $authkey = $_GET['authkey']; @@ -47,11 +50,11 @@ $lockString = http_build_query($lockParams); $lockKey = "isReAnnounce:" . md5($lockString); - if (!$redis->set($lockKey, TIMENOW, ['nx', 'ex' => 20])) { + if (!$redis->set($lockKey, TIMENOW, ['nx', 'ex' => $reAnnounceInterval])) { $isReAnnounce = true; } $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)"; do_log(sprintf("[ANNOUNCE] %s key: %s already exists, value: %s", $msg, $reAnnounceCheckByAuthKey, TIMENOW)); warn($msg, 300); @@ -74,7 +77,7 @@ } $lockString = http_build_query($lockParams); $lockKey = "isReAnnounce:" . md5($lockString); - if (!$redis->set($lockKey, TIMENOW, ['nx', 'ex' => 20])) { + if (!$redis->set($lockKey, TIMENOW, ['nx', 'ex' => $reAnnounceInterval])) { $isReAnnounce = true; } } else { @@ -87,7 +90,7 @@ err($msg); } $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)"; do_log(sprintf("[ANNOUNCE] %s key: %s already exists, value: %s", $msg, $torrentReAnnounceKey, TIMENOW)); warn($msg, 300);