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

@ -108,12 +108,16 @@ 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) {
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.'
);
$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)),
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