announce buy torrent add cache and lock

This commit is contained in:
xiaomlove
2023-06-01 01:41:14 +08:00
parent 29cbe19c21
commit ff325ad687
5 changed files with 77 additions and 11 deletions
+2 -2
View File
@@ -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',
+1
View File
@@ -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);
});
}
@@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->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()
{
//
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
<?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.4');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-05-30');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-06-01');
defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
+38 -8
View File
@@ -11,9 +11,9 @@ if (!empty($_GET['authkey'])) {
if (count($parts) != 3) {
err("authkey format error");
}
$tid = $parts[0];
$uid = $parts[1];
$subAuthkey = sprintf("%s|%s", $tid, $uid);
$authKeyTid = $parts[0];
$authKeyUid = $parts[1];
$subAuthkey = sprintf("%s|%s", $authKeyTid, $authKeyUid);
if (!$redis->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);