Skip to content

Commit 794cf9d

Browse files
committed
Make backup throttling configurable
1 parent a7fef8b commit 794cf9d

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

app/Repositories/Eloquent/BackupRepository.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ public function model()
1919
* Determines if too many backups have been generated by the server.
2020
*
2121
* @param int $server
22-
* @param int $minutes
22+
* @param int $seconds
2323
* @return \Pterodactyl\Models\Backup[]|\Illuminate\Support\Collection
2424
*/
25-
public function getBackupsGeneratedDuringTimespan(int $server, int $minutes = 10)
25+
public function getBackupsGeneratedDuringTimespan(int $server, int $seconds = 600)
2626
{
2727
return $this->getBuilder()
2828
->withTrashed()
2929
->where('server_id', $server)
3030
->where('is_successful', true)
31-
->where('created_at', '>=', Carbon::now()->subMinutes($minutes)->toDateTimeString())
31+
->where('created_at', '>=', Carbon::now()->subSeconds($seconds)->toDateTimeString())
3232
->get()
3333
->toBase();
3434
}

app/Services/Backups/InitiateBackupService.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,16 @@ public function setIgnoredFiles(?array $ignored)
108108
*/
109109
public function handle(Server $server, string $name = null, bool $override = false): Backup
110110
{
111-
$previous = $this->repository->getBackupsGeneratedDuringTimespan($server->id, 10);
112-
if ($previous->count() >= 2) {
113-
throw new TooManyRequestsHttpException(
114-
CarbonImmutable::now()->diffInSeconds($previous->last()->created_at->addMinutes(10)),
115-
'Only two backups may be generated within a 10 minute span of time.'
116-
);
111+
$limit = config('backups.throttles.limit');
112+
$period = config('backups.throttles.period');
113+
if ($period > 0) {
114+
$previous = $this->repository->getBackupsGeneratedDuringTimespan($server->id, $period);
115+
if ($previous->count() >= $limit) {
116+
throw new TooManyRequestsHttpException(
117+
CarbonImmutable::now()->diffInSeconds($previous->last()->created_at->addMinutes(10)),
118+
sprintf('Only %d backups may be generated within a %d second span of time.', $limit, $period)
119+
);
120+
}
117121
}
118122

119123
// Check if the server has reached or exceeded it's backup limit

config/backups.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
// to 6 hours. To disable this feature, set the value to `0`.
1717
'prune_age' => env('BACKUP_PRUNE_AGE', 360),
1818

19+
// Defines the backup creation throttle limits for users. In this default example, we allow
20+
// a user to create two (successful or pending) backups per 10 minutes. Even if they delete
21+
// a backup it will be included in the throttle count.
22+
//
23+
// Set the period to "0" to disable this throttle. The period is defined in seconds.
24+
'throttles' => [
25+
'limit' => env('BACKUP_THROTTLE_LIMIT', 2),
26+
'period' => env('BACKUP_THROTTLE_PERIOD', 600),
27+
],
28+
1929
'disks' => [
2030
// There is no configuration for the local disk for Wings. That configuration
2131
// is determined by the Daemon configuration, and not the Panel.

0 commit comments

Comments
 (0)