From 9b46d59045956378b3aeb275bb87c1933e0b3e6c Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 21 Mar 2021 12:29:18 -0700 Subject: [PATCH] Cache resource lookup results for 20 seconds for each server --- .../Servers/ResourceUtilizationController.php | 22 +++++++++++++++---- .../components/dashboard/ServerRow.tsx | 2 +- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php b/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php index 4cf0d249b..72901c8bd 100644 --- a/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php +++ b/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php @@ -2,7 +2,9 @@ namespace Pterodactyl\Http\Controllers\Api\Client\Servers; +use Carbon\Carbon; use Pterodactyl\Models\Server; +use Illuminate\Cache\Repository; use Pterodactyl\Transformers\Api\Client\StatsTransformer; use Pterodactyl\Repositories\Wings\DaemonServerRepository; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; @@ -13,26 +15,38 @@ class ResourceUtilizationController extends ClientApiController /** * @var \Pterodactyl\Repositories\Wings\DaemonServerRepository */ - private $repository; + private DaemonServerRepository $repository; + + /** + * @var \Illuminate\Cache\Repository + */ + private Repository $cache; /** * ResourceUtilizationController constructor. */ - public function __construct(DaemonServerRepository $repository) + public function __construct(Repository $cache, DaemonServerRepository $repository) { parent::__construct(); + $this->cache = $cache; $this->repository = $repository; } /** - * Return the current resource utilization for a server. + * Return the current resource utilization for a server. This value is cached for up to + * 20 seconds at a time to ensure that repeated requests to this endpoint do not cause + * a flood of unnecessary API calls. * * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException */ public function __invoke(GetServerRequest $request, Server $server): array { - $stats = $this->repository->setServer($server)->getDetails(); + $stats = $this->cache + ->tags(['resources']) + ->remember($server->uuid, Carbon::now()->addSeconds(20), function () use ($server) { + return $this->repository->setServer($server)->getDetails(); + }); return $this->fractal->item($stats) ->transformWith($this->getTransformer(StatsTransformer::class)) diff --git a/resources/scripts/components/dashboard/ServerRow.tsx b/resources/scripts/components/dashboard/ServerRow.tsx index 40eebefac..2b33dbab7 100644 --- a/resources/scripts/components/dashboard/ServerRow.tsx +++ b/resources/scripts/components/dashboard/ServerRow.tsx @@ -59,7 +59,7 @@ export default ({ server, className }: { server: Server; className?: string }) = getStats().then(() => { // @ts-ignore - interval.current = setInterval(() => getStats(), 20000); + interval.current = setInterval(() => getStats(), 30000); }); return () => {