refactor: peer connectable

- this was a major performance issue and was implemented wrong. More refactoring to come.
This commit is contained in:
HDVinnie
2021-08-06 00:04:54 -04:00
parent 550fcd6648
commit dce237ad07
3 changed files with 19 additions and 25 deletions
+1 -1
View File
@@ -81,7 +81,7 @@ class UserController extends Controller
$requested = TorrentRequest::where('user_id', '=', $user->id)->count();
$filled = TorrentRequest::where('filled_by', '=', $user->id)->whereNotNull('approved_by')->count();
$peers = Peer::with(['user'])->where('user_id', '=', $user->id)->latest('seeder')->get();
$peers = Peer::where('user_id', '=', $user->id)->get();
return \view('user.profile', [
'route' => 'profile',
+10 -7
View File
@@ -118,20 +118,23 @@ class Peer extends Model
/**
* Updates Connectable State If Needed.
*
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws \Exception
* @var resource
*/
public function updateConnectableStateIfNeeded()
public function updateConnectableStateIfNeeded(): void
{
$redis = new RedisClient();
if (! $redis->get('peers:connectable:'.$this->id)) {
if (! \cache()->has('peers:connectable:'.$this->ip.'-'.$this->port.'-'.$this->agent)) {
$con = @fsockopen($this->ip, $this->port, $_, $_, 1);
$this->connectable = is_resource($con);
$redis->setex('peers:connectable:'.$this->id, config('announce.connectable_check_interval'), $this->connectable ? 'yes' : 'no');
$this->connectable = \is_resource($con);
\cache()->put('peers:connectable:'.$this->ip.'-'.$this->port.'-'.$this->agent, $this->connectable, now()->addDay());
if (is_resource($con)) {
fclose($con);
if (\is_resource($con)) {
\fclose($con);
}
self::where('ip', '=', $this->ip)->where('port', '=', $this->port)->where('agent', '=', $this->agent)->update(['connectable' => $this->connectable]);
}
}
}
+8 -17
View File
@@ -554,29 +554,20 @@
@php $peer_array = []; @endphp
@foreach ($peers as $p)
@if (!in_array([$p->ip, $p->port], $peer_array))
@php $count = App\Models\Peer::with(['user'])->where('user_id', '=', $user->id)->latest('seeder')->where('ip', '=', $p->ip)->where('port', '=', $p->port)->count(); @endphp
@php $count = App\Models\Peer::where('user_id', '=', $user->id)->where('ip', '=', $p->ip)->where('port', '=', $p->port)->count(); @endphp
<tr>
<td>
<span class="badge-extra text-purple text-bold">{{ $p->agent }}</span>
</td>
@if (auth()->user()->group->is_modo || auth()->user()->id == $p->user_id)
<td><span class="badge-extra text-bold">{{ $p->ip }}</span></td>
<td><span class="badge-extra text-bold">{{ $p->port }}</span></td>
@else
<td> ---</td>
<td> ---</td>
@endif
<td><span class="badge-extra text-bold">{{ $p->ip }}</span></td>
<td><span class="badge-extra text-bold">{{ $p->port }}</span></td>
<td>{{ $p->created_at ? $p->created_at->diffForHumans() : 'N/A' }}</td>
<td>{{ $p->updated_at ? $p->updated_at->diffForHumans() : 'N/A' }}</td>
@if (auth()->user()->group->is_modo || auth()->user()->id == $p->user_id)
<td>
<a href="{{ route('user_active_by_client', ['username' => $user->username, 'ip' => $p->ip, 'port' => $p->port]) }}" itemprop="url" class="l-breadcrumb-item-link">
<span itemprop="title" class="l-breadcrumb-item-link-title">{{ $count }}</span>
</a>
</td>
@else
<td> ---</td>
@endif
<td>
<a href="{{ route('user_active_by_client', ['username' => $user->username, 'ip' => $p->ip, 'port' => $p->port]) }}" itemprop="url" class="l-breadcrumb-item-link">
<span itemprop="title" class="l-breadcrumb-item-link-title">{{ $count }}</span>
</a>
</td>
<td>@choice('user.client-connectable-state', $p->connectable)</td>
</tr>
@php array_push($peer_array, [$p->ip, $p->port]); @endphp