From ace8dcb5b115cfb4f38cdfd1f8be98a01d3900f4 Mon Sep 17 00:00:00 2001 From: HDVinnie Date: Mon, 14 Nov 2022 20:10:26 -0500 Subject: [PATCH] revert: peer batching --- app/Console/Commands/AutoFlushPeers.php | 2 +- app/Console/Commands/AutoInsertPeers.php | 83 ------------------- app/Console/Kernel.php | 1 - .../Controllers/Staff/FlushController.php | 2 +- app/Http/Controllers/UserController.php | 2 +- app/Http/Livewire/UserActive.php | 1 + app/Jobs/ProcessAnnounce.php | 4 - app/Models/Peer.php | 6 -- .../2022_10_30_064546_update_peers_table.php | 20 ----- 9 files changed, 4 insertions(+), 117 deletions(-) delete mode 100644 app/Console/Commands/AutoInsertPeers.php delete mode 100644 database/migrations/2022_10_30_064546_update_peers_table.php diff --git a/app/Console/Commands/AutoFlushPeers.php b/app/Console/Commands/AutoFlushPeers.php index 469f9b7bd..f841bf7b9 100644 --- a/app/Console/Commands/AutoFlushPeers.php +++ b/app/Console/Commands/AutoFlushPeers.php @@ -45,7 +45,7 @@ class AutoFlushPeers extends Command public function handle(): void { $carbon = new Carbon(); - $peers = Peer::select(['torrent_id', 'user_id', 'peer_id', 'updated_at'])->where('updated_at', '<', $carbon->copy()->subHours(2)->toDateTimeString())->get(); + $peers = Peer::select(['id', 'torrent_id', 'user_id', 'updated_at'])->where('updated_at', '<', $carbon->copy()->subHours(2)->toDateTimeString())->get(); foreach ($peers as $peer) { $history = History::where('torrent_id', '=', $peer->torrent_id)->where('user_id', '=', $peer->user_id)->first(); diff --git a/app/Console/Commands/AutoInsertPeers.php b/app/Console/Commands/AutoInsertPeers.php deleted file mode 100644 index c97769546..000000000 --- a/app/Console/Commands/AutoInsertPeers.php +++ /dev/null @@ -1,83 +0,0 @@ - - * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 - */ - -namespace App\Console\Commands; - -use App\Models\Peer; -use Illuminate\Console\Command; -use Illuminate\Support\Facades\Redis; - -/** - * @see \Tests\Unit\Console\Commands\AutoFlushPeersTest - */ -class AutoInsertPeers extends Command -{ - /** - * MySql can handle a max of 65k placeholders per query, - * and there are 13 fields on each peer that are updated - */ - public const PEERS_PER_CYCLE = 65_000 / 13; - - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'auto:insert_peers'; - - /** - * The console command description. - * - * @var string - */ - protected $description = 'Inserts peers in batches'; - - /** - * Execute the console command. - * - * @throws \Exception - */ - public function handle(): void - { - $key = config('cache.prefix').':peers:batch'; - $peerCount = Redis::connection('cache')->command('LLEN', [$key]); - $cycles = ceil($peerCount / self::PEERS_PER_CYCLE); - - for ($i = 0; $i < $cycles; $i++) { - $peers = Redis::connection('cache')->command('RPOP', [$key, self::PEERS_PER_CYCLE]); - $peers = array_map('unserialize', $peers); - - Peer::upsert( - $peers, - ['peer_id'], - [ - 'peer_id', - 'md5_peer_id', - 'info_hash', - 'ip', - 'port', - 'agent', - 'uploaded', - 'downloaded', - 'seeder', - 'left', - 'torrent_id', - 'user_id', - 'updated_at' - ], - ); - } - - $this->comment('Automated insert peers command complete'); - } -} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 78047c83b..d06b158f1 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -47,7 +47,6 @@ class Kernel extends ConsoleKernel $schedule->command('auto:stats_clients')->daily(); $schedule->command('auto:remove_torrent_buffs')->hourly(); $schedule->command('auto:torrent_balance')->hourly(); - //$schedule->command('auto:insert_peers')->everyMinute(); //$schedule->command('auto:ban_disposable_users')->weekends(); //$schedule->command('backup:clean')->daily(); //$schedule->command('backup:run')->daily(); diff --git a/app/Http/Controllers/Staff/FlushController.php b/app/Http/Controllers/Staff/FlushController.php index 0943e93b9..3190b41d4 100644 --- a/app/Http/Controllers/Staff/FlushController.php +++ b/app/Http/Controllers/Staff/FlushController.php @@ -41,7 +41,7 @@ class FlushController extends Controller public function peers(): \Illuminate\Http\RedirectResponse { $carbon = new Carbon(); - $peers = Peer::select(['torrent_id', 'user_id', 'peer_id', 'updated_at'])->where('updated_at', '<', $carbon->copy()->subHours(2)->toDateTimeString())->get(); + $peers = Peer::select(['id', 'torrent_id', 'user_id', 'updated_at'])->where('updated_at', '<', $carbon->copy()->subHours(2)->toDateTimeString())->get(); foreach ($peers as $peer) { $history = History::where('torrent_id', '=', $peer->torrent_id)->where('user_id', '=', $peer->user_id)->first(); diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 47fbc7b4d..dba1e60de 100755 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -1442,7 +1442,7 @@ class UserController extends Controller $carbon = new Carbon(); // Get Peer List from User - $peers = Peer::select(['torrent_id', 'user_id', 'peer_id', 'updated_at']) + $peers = Peer::select(['id', 'torrent_id', 'user_id', 'updated_at']) ->where('user_id', '=', $user->id) ->where('updated_at', '<', $carbon->copy()->subMinutes(70)->toDateTimeString()) ->get(); diff --git a/app/Http/Livewire/UserActive.php b/app/Http/Livewire/UserActive.php index 9c05ac694..c767c228a 100644 --- a/app/Http/Livewire/UserActive.php +++ b/app/Http/Livewire/UserActive.php @@ -79,6 +79,7 @@ class UserActive extends Component return Peer::query() ->join('torrents', 'peers.torrent_id', '=', 'torrents.id') ->select( + 'peers.id', 'peers.ip', 'peers.port', 'peers.agent', diff --git a/app/Jobs/ProcessAnnounce.php b/app/Jobs/ProcessAnnounce.php index 182e3ceed..0e27d46f7 100644 --- a/app/Jobs/ProcessAnnounce.php +++ b/app/Jobs/ProcessAnnounce.php @@ -24,7 +24,6 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\Middleware\WithoutOverlapping; use Illuminate\Queue\SerializesModels; -use Illuminate\Support\Facades\Redis; class ProcessAnnounce implements ShouldQueue { @@ -151,7 +150,6 @@ class ProcessAnnounce implements ShouldQueue $peer->updateConnectableStateIfNeeded(); $peer->updated_at = \now(); $peer->save(); - //Redis::connection('cache')->command('LPUSH', [config('cache.prefix').':peers:batch', serialize($peer)]); $history->user_id = $this->user->id; $history->torrent_id = $this->torrent->id; @@ -185,7 +183,6 @@ class ProcessAnnounce implements ShouldQueue $peer->updateConnectableStateIfNeeded(); $peer->updated_at = \now(); $peer->save(); - //Redis::connection('cache')->command('LPUSH', [config('cache.prefix').':peers:batch', serialize($peer)]); $history->user_id = $this->user->id; $history->torrent_id = $this->torrent->id; @@ -280,7 +277,6 @@ class ProcessAnnounce implements ShouldQueue $peer->updateConnectableStateIfNeeded(); $peer->updated_at = \now(); $peer->save(); - //Redis::connection('cache')->command('LPUSH', [config('cache.prefix').':peers:batch', serialize($peer)]); $history->user_id = $this->user->id; $history->torrent_id = $this->torrent->id; diff --git a/app/Models/Peer.php b/app/Models/Peer.php index 901715763..519b6543c 100644 --- a/app/Models/Peer.php +++ b/app/Models/Peer.php @@ -21,12 +21,6 @@ class Peer extends Model { use HasFactory; - protected $primaryKey = 'peer_id'; - - public $incrementing = false; - - protected $keyType = 'string'; - /** * Belongs To A User. */ diff --git a/database/migrations/2022_10_30_064546_update_peers_table.php b/database/migrations/2022_10_30_064546_update_peers_table.php deleted file mode 100644 index 97ff1033c..000000000 --- a/database/migrations/2022_10_30_064546_update_peers_table.php +++ /dev/null @@ -1,20 +0,0 @@ -dropColumn('id'); - $table->primary(['torrent_id', 'user_id', 'peer_id']); - }); - } -};