From ff325ad687d0f5c44ea57a25517ac63cf6fed524 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Thu, 1 Jun 2023 01:41:14 +0800 Subject: [PATCH] announce buy torrent add cache and lock --- app/Models/User.php | 4 +- app/Repositories/BonusRepository.php | 1 + ...0_change_bonus_log_table_value_decimal.php | 35 ++++++++++++++ include/constants.php | 2 +- public/announce.php | 46 +++++++++++++++---- 5 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 database/migrations/2023_06_01_013150_change_bonus_log_table_value_decimal.php diff --git a/app/Models/User.php b/app/Models/User.php index 572928f8..2df762c8 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -188,7 +188,7 @@ class User extends Authenticatable implements FilamentUser, HasName * @var array */ protected $hidden = [ - 'secret', 'passhash', + 'secret', 'passhash', 'passkey' ]; /** @@ -219,7 +219,7 @@ class User extends Authenticatable implements FilamentUser, HasName ]; public static $commonFields = [ - 'id', 'username', 'email', 'class', 'status', 'added', 'avatar', + 'id', 'username', 'email', 'class', 'status', 'added', 'avatar', 'passkey', 'uploaded', 'downloaded', 'seedbonus', 'seedtime', 'leechtime', 'invited_by', 'enabled', 'seed_points', 'last_access', 'invites', 'lang', 'attendance_card', 'privacy', 'noad', 'downloadpos', 'donoruntil', 'donor', diff --git a/app/Repositories/BonusRepository.php b/app/Repositories/BonusRepository.php index dcb729ff..2d73285e 100644 --- a/app/Repositories/BonusRepository.php +++ b/app/Repositories/BonusRepository.php @@ -349,6 +349,7 @@ class BonusRepository extends BaseRepository ]; BonusLogs::query()->insert($bonusLog); do_log("bonusLog: " . nexus_json_encode($bonusLog)); + clear_user_cache($user->id, $user->passkey); }); } diff --git a/database/migrations/2023_06_01_013150_change_bonus_log_table_value_decimal.php b/database/migrations/2023_06_01_013150_change_bonus_log_table_value_decimal.php new file mode 100644 index 00000000..cad57547 --- /dev/null +++ b/database/migrations/2023_06_01_013150_change_bonus_log_table_value_decimal.php @@ -0,0 +1,35 @@ +decimal("seedbonus", 20, 1)->change(); + }); + Schema::table('bonus_logs', function (Blueprint $table) { + $table->decimal("old_total_value", 20, 1)->change(); + $table->decimal("value", 20, 1)->change(); + $table->decimal("new_total_value", 20, 1)->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +}; diff --git a/include/constants.php b/include/constants.php index dc53c8e5..9f0d96fe 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ set($subAuthkey, TIMENOW, ['nx', 'ex' => 60])) { $msg = "Request too frequent(a)"; do_log("[ANNOUNCE] $msg"); @@ -186,6 +186,13 @@ if (!$torrent) { $redis->set("$torrentNotExistsKey:$info_hash", TIMENOW, ['ex' => 24*3600]); err("torrent not registered with this tracker"); } +$torrentid = $torrent["id"]; +if (isset($authKeyTid) && $authKeyTid != $torrentid) { + $redis->set("$authKeyInvalidKey:$authkey", TIMENOW, ['ex' => 3600*24]); + $msg = "Invalid authkey: $authkey 2"; + do_log("[ANNOUNCE] $msg"); + err($msg); +} if ($torrent['banned'] == 'yes') { if (!user_can('seebanned', false, $az['id'])) { err("torrent banned"); @@ -196,6 +203,12 @@ if ($torrent['approval_status'] != \App\Models\Torrent::APPROVAL_STATUS_ALLOW && err("torrent review not approved"); } } +if (!$redis->set(sprintf('%s:%s', $userid, $info_hash), TIMENOW, ['nx', 'ex' => 60])) { + $msg = "Request too frequent(h)"; + do_log("[ANNOUNCE] $msg"); + err($msg); +} + if ( $seeder == 'no' && isset($az['seedbonus']) @@ -204,18 +217,35 @@ if ( && $torrent['owner'] != $userid && get_setting("torrent.paid_torrent_enabled") == "yes" ) { - $hasBuy = \App\Models\TorrentBuyLog::query()->where('uid', $userid)->where('torrent_id', $torrent['id'])->exists(); + $hasBuyCacheKey = sprintf("user_has_buy_torrent:%s:%s", $userid, $torrentid); + $hasBuyCacheTime = 86400*10; + $hasBuy = \Nexus\Database\NexusDB::remember($hasBuyCacheKey, $hasBuyCacheTime, function () use($userid, $torrentid) { + $exists = \App\Models\TorrentBuyLog::query()->where('uid', $userid)->where('torrent_id', $torrentid)->exists(); + return intval($exists); + }); if (!$hasBuy) { - if ($az['seedbonus'] < $torrent['price']) { - err("Not enough bonus to buy this paid torrent"); + $lock = new \Nexus\Database\NexusLock("buying_torrent:$userid", 10); + if (!$lock->get()) { + $msg = "buying torrent, wait!"; + do_log("[ANNOUNCE] user: $userid, torrent: $torrentid, $msg", 'error'); + err($msg); } $bonusRep = new \App\Repositories\BonusRepository(); - $bonusRep->consumeToBuyTorrent($az['id'], $torrent['id'], 'Web'); + try { + $bonusRep->consumeToBuyTorrent($az['id'], $torrent['id'], 'Web'); + $lock->release(); + $redis->set($hasBuyCacheKey, 1, $hasBuyCacheTime); + } catch (\Exception $exception) { + $lock->release(); + $msg = $exception->getMessage(); + do_log("[ANNOUNCE] user: $userid, torrent: $torrentid, $msg " . $exception->getTraceAsString(), 'error'); + err($msg); + } } } // select peers info from peers table for this torrent -$torrentid = $torrent["id"]; + $numpeers = $torrent["seeders"]+$torrent["leechers"]; $promotionInfo = apply_filter('torrent_promotion', $torrent);