Make backup throttling configurable

This commit is contained in:
Dane Everitt 2020-12-27 16:41:53 -08:00
parent a7fef8b736
commit 794cf9d9dd
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
3 changed files with 23 additions and 9 deletions

View file

@ -19,16 +19,16 @@ class BackupRepository extends EloquentRepository
* Determines if too many backups have been generated by the server.
*
* @param int $server
* @param int $minutes
* @param int $seconds
* @return \Pterodactyl\Models\Backup[]|\Illuminate\Support\Collection
*/
public function getBackupsGeneratedDuringTimespan(int $server, int $minutes = 10)
public function getBackupsGeneratedDuringTimespan(int $server, int $seconds = 600)
{
return $this->getBuilder()
->withTrashed()
->where('server_id', $server)
->where('is_successful', true)
->where('created_at', '>=', Carbon::now()->subMinutes($minutes)->toDateTimeString())
->where('created_at', '>=', Carbon::now()->subSeconds($seconds)->toDateTimeString())
->get()
->toBase();
}

View file

@ -108,13 +108,17 @@ class InitiateBackupService
*/
public function handle(Server $server, string $name = null, bool $override = false): Backup
{
$previous = $this->repository->getBackupsGeneratedDuringTimespan($server->id, 10);
if ($previous->count() >= 2) {
$limit = config('backups.throttles.limit');
$period = config('backups.throttles.period');
if ($period > 0) {
$previous = $this->repository->getBackupsGeneratedDuringTimespan($server->id, $period);
if ($previous->count() >= $limit) {
throw new TooManyRequestsHttpException(
CarbonImmutable::now()->diffInSeconds($previous->last()->created_at->addMinutes(10)),
'Only two backups may be generated within a 10 minute span of time.'
sprintf('Only %d backups may be generated within a %d second span of time.', $limit, $period)
);
}
}
// Check if the server has reached or exceeded it's backup limit
if (! $server->backup_limit || $server->backups()->where('is_successful', true)->count() >= $server->backup_limit) {

View file

@ -16,6 +16,16 @@ return [
// to 6 hours. To disable this feature, set the value to `0`.
'prune_age' => env('BACKUP_PRUNE_AGE', 360),
// Defines the backup creation throttle limits for users. In this default example, we allow
// a user to create two (successful or pending) backups per 10 minutes. Even if they delete
// a backup it will be included in the throttle count.
//
// Set the period to "0" to disable this throttle. The period is defined in seconds.
'throttles' => [
'limit' => env('BACKUP_THROTTLE_LIMIT', 2),
'period' => env('BACKUP_THROTTLE_PERIOD', 600),
],
'disks' => [
// There is no configuration for the local disk for Wings. That configuration
// is determined by the Daemon configuration, and not the Panel.