Skip to content

Commit

Permalink
improve announce reAnnounce and frequency check
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Dec 14, 2024
1 parent 0f4125f commit ca21b0b
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions public/announce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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);
Expand All @@ -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 {
Expand All @@ -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);
Expand Down

0 comments on commit ca21b0b

Please sign in to comment.