From e306b1f6edf3527e7b93af0dac8de925250d49c1 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Thu, 21 Apr 2022 17:54:26 +0800 Subject: [PATCH] tracker user update use variable instead of properity --- app/Repositories/TrackerRepository.php | 38 +++++++++---------- .../2021_06_08_113437_create_peers_table.php | 6 +-- ..._18_030257_handle_peers_peer_id_unique.php | 2 +- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/app/Repositories/TrackerRepository.php b/app/Repositories/TrackerRepository.php index bdb9f439..a5fcffce 100644 --- a/app/Repositories/TrackerRepository.php +++ b/app/Repositories/TrackerRepository.php @@ -52,15 +52,13 @@ class TrackerRepository extends BaseRepository 6699, // Port used by p2p software, such as WinMX, Napster. ]; - private array $userUpdates = []; - public function announce(Request $request): \Illuminate\Http\Response { do_log("queryString: " . $request->getQueryString()); /** - * Note: In Octane this class will be reused, must reset this property !!! + * Note: In Octane this class will be reused, use variable is better !!! */ - $this->userUpdates = []; + $userUpdates = []; try { $withPeers = false; $queries = $this->checkAnnounceFields($request); @@ -117,16 +115,16 @@ class TrackerRepository extends BaseRepository $this->updateTorrent($torrent, $queries, $isPeerExists); if ($dataTraffic['uploaded_increment_for_user'] > 0) { - $this->userUpdates['uploaded'] = DB::raw('uploaded + ' . $dataTraffic['uploaded_increment_for_user']); + $userUpdates['uploaded'] = DB::raw('uploaded + ' . $dataTraffic['uploaded_increment_for_user']); } if ($dataTraffic['downloaded_increment_for_user'] > 0) { - $this->userUpdates['downloaded'] = DB::raw('downloaded + ' . $dataTraffic['downloaded_increment_for_user']); + $userUpdates['downloaded'] = DB::raw('downloaded + ' . $dataTraffic['downloaded_increment_for_user']); } if ($user->clientselect != $clientAllow->id) { - $this->userUpdates['clientselect'] = $clientAllow->id; + $userUpdates['clientselect'] = $clientAllow->id; } if ($user->showclienterror == 'yes') { - $this->userUpdates['showclienterror'] = 'no'; + $userUpdates['showclienterror'] = 'no'; } } } @@ -134,7 +132,7 @@ class TrackerRepository extends BaseRepository } catch (ClientNotAllowedException $exception) { do_log("[ClientNotAllowedException] " . $exception->getMessage()); if (isset($user) && $user->showclienterror == 'no') { - $this->userUpdates['showclienterror'] = 'yes'; + $userUpdates['showclienterror'] = 'yes'; } $repDict = $this->generateFailedAnnounceResponse($exception->getMessage()); } catch (TrackerException $exception) { @@ -145,7 +143,7 @@ class TrackerRepository extends BaseRepository $repDict = $this->generateFailedAnnounceResponse("system error, report to sysop please, hint: " . nexus()->getRequestId()); } finally { if (isset($user)) { - $this->updateUser($user); + $this->updateUser($user, $userUpdates); } return $this->sendFinalAnnounceResponse($repDict); } @@ -955,25 +953,25 @@ class TrackerRepository extends BaseRepository { do_log("queryString: " . $request->getQueryString()); /** - * Note: In Octane this class will be reused, must reset this property !!! + * Note: In Octane this class will be reused, use variable is better !!! */ - $this->userUpdates = []; + $userUpdates = []; try { $infoHashArr = $this->checkScrapeFields($request); $user = $this->checkUser($request); $clientAllow = $this->checkClient($request); if ($user->clientselect != $clientAllow->id) { - $this->userUpdates['clientselect'] = $clientAllow->id; + $userUpdates['clientselect'] = $clientAllow->id; } if ($user->showclienterror == 'yes') { - $this->userUpdates['showclienterror'] = 'no'; + $userUpdates['showclienterror'] = 'no'; } $repDict = $this->generateScrapeResponse($infoHashArr); } catch (ClientNotAllowedException $exception) { do_log("[ClientNotAllowedException] " . $exception->getMessage()); if (isset($user) && $user->showclienterror == 'no') { - $this->userUpdates['showclienterror'] = 'yes'; + $userUpdates['showclienterror'] = 'yes'; } $repDict = $this->generateFailedAnnounceResponse($exception->getMessage()); } catch (TrackerException $exception) { @@ -984,7 +982,7 @@ class TrackerRepository extends BaseRepository $repDict = $this->generateFailedAnnounceResponse("system error, report to sysop please, hint: " . nexus()->getRequestId()); } finally { if (isset($user)) { - $this->updateUser($user); + $this->updateUser($user, $userUpdates); } return $this->sendFinalAnnounceResponse($repDict); } @@ -1066,13 +1064,13 @@ class TrackerRepository extends BaseRepository DB::insert($sql); } - private function updateUser(User $user) + private function updateUser(User $user, array $update) { - $log = "update: " . json_encode($this->userUpdates); - if (count($this->userUpdates) === 0) { + $log = "update: " . json_encode($update); + if (empty($update)) { $log .= ", no update..."; } else { - $user->fill($this->userUpdates); + $user->fill($update); $log .= ", dirty: " . json_encode($user->getDirty()); $user->save(); $log .= ", query: " . last_query(); diff --git a/database/migrations/2021_06_08_113437_create_peers_table.php b/database/migrations/2021_06_08_113437_create_peers_table.php index 102c320d..dc6c3cd2 100644 --- a/database/migrations/2021_06_08_113437_create_peers_table.php +++ b/database/migrations/2021_06_08_113437_create_peers_table.php @@ -19,7 +19,7 @@ class CreatePeersTable extends Migration Schema::create('peers', function (Blueprint $table) { $table->increments('id'); $table->unsignedMediumInteger('torrent')->default(0); - $table->char('peer_id', 20)->charset('binary'); + $table->char('peer_id', 20)->charset('binary')->index(); $table->string('ip', 64)->default(''); $table->unsignedSmallInteger('port')->default(0); $table->unsignedBigInteger('uploaded')->default(0); @@ -30,13 +30,13 @@ class CreatePeersTable extends Migration $table->dateTime('last_action')->nullable(); $table->dateTime('prev_action')->nullable(); $table->enum('connectable', ['yes', 'no'])->default('yes'); - $table->unsignedMediumInteger('userid')->default(0)->index('userid'); + $table->unsignedMediumInteger('userid')->default(0)->index(); $table->string('agent', 60)->default(''); $table->unsignedInteger('finishedat')->default(0); $table->unsignedBigInteger('downloadoffset')->default(0); $table->unsignedBigInteger('uploadoffset')->default(0); $table->char('passkey', 32)->default(''); - $table->unique(['torrent', 'peer_id']); + $table->index(['torrent', 'peer_id']); }); } diff --git a/database/migrations/2022_04_18_030257_handle_peers_peer_id_unique.php b/database/migrations/2022_04_18_030257_handle_peers_peer_id_unique.php index 036f0ee8..f335d238 100644 --- a/database/migrations/2022_04_18_030257_handle_peers_peer_id_unique.php +++ b/database/migrations/2022_04_18_030257_handle_peers_peer_id_unique.php @@ -28,7 +28,7 @@ return new class extends Migration } Schema::table($tableName, function (Blueprint $table) { - $table->unique(['torrent', 'peer_id']); + $table->index(['torrent', 'peer_id']); $table->index('peer_id'); }); }