Make backup throttling configurable
This commit is contained in:
parent
a7fef8b736
commit
794cf9d9dd
3 changed files with 23 additions and 9 deletions
|
@ -19,16 +19,16 @@ class BackupRepository extends EloquentRepository
|
||||||
* Determines if too many backups have been generated by the server.
|
* Determines if too many backups have been generated by the server.
|
||||||
*
|
*
|
||||||
* @param int $server
|
* @param int $server
|
||||||
* @param int $minutes
|
* @param int $seconds
|
||||||
* @return \Pterodactyl\Models\Backup[]|\Illuminate\Support\Collection
|
* @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()
|
return $this->getBuilder()
|
||||||
->withTrashed()
|
->withTrashed()
|
||||||
->where('server_id', $server)
|
->where('server_id', $server)
|
||||||
->where('is_successful', true)
|
->where('is_successful', true)
|
||||||
->where('created_at', '>=', Carbon::now()->subMinutes($minutes)->toDateTimeString())
|
->where('created_at', '>=', Carbon::now()->subSeconds($seconds)->toDateTimeString())
|
||||||
->get()
|
->get()
|
||||||
->toBase();
|
->toBase();
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,12 +108,16 @@ class InitiateBackupService
|
||||||
*/
|
*/
|
||||||
public function handle(Server $server, string $name = null, bool $override = false): Backup
|
public function handle(Server $server, string $name = null, bool $override = false): Backup
|
||||||
{
|
{
|
||||||
$previous = $this->repository->getBackupsGeneratedDuringTimespan($server->id, 10);
|
$limit = config('backups.throttles.limit');
|
||||||
if ($previous->count() >= 2) {
|
$period = config('backups.throttles.period');
|
||||||
throw new TooManyRequestsHttpException(
|
if ($period > 0) {
|
||||||
CarbonImmutable::now()->diffInSeconds($previous->last()->created_at->addMinutes(10)),
|
$previous = $this->repository->getBackupsGeneratedDuringTimespan($server->id, $period);
|
||||||
'Only two backups may be generated within a 10 minute span of time.'
|
if ($previous->count() >= $limit) {
|
||||||
);
|
throw new TooManyRequestsHttpException(
|
||||||
|
CarbonImmutable::now()->diffInSeconds($previous->last()->created_at->addMinutes(10)),
|
||||||
|
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
|
// Check if the server has reached or exceeded it's backup limit
|
||||||
|
|
|
@ -16,6 +16,16 @@ return [
|
||||||
// to 6 hours. To disable this feature, set the value to `0`.
|
// to 6 hours. To disable this feature, set the value to `0`.
|
||||||
'prune_age' => env('BACKUP_PRUNE_AGE', 360),
|
'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' => [
|
'disks' => [
|
||||||
// There is no configuration for the local disk for Wings. That configuration
|
// There is no configuration for the local disk for Wings. That configuration
|
||||||
// is determined by the Daemon configuration, and not the Panel.
|
// is determined by the Daemon configuration, and not the Panel.
|
||||||
|
|
Loading…
Reference in a new issue