From d884578c60ac316c8bacdb381bc88908bed1ef18 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Sun, 11 Mar 2018 12:11:25 +0200 Subject: [PATCH] Merge throttle check --- app/Constants.php | 2 +- app/Ninja/Mailers/ContactMailer.php | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/Constants.php b/app/Constants.php index 8cc43497c799..0cd7e5277e03 100644 --- a/app/Constants.php +++ b/app/Constants.php @@ -158,7 +158,7 @@ if (! defined('APP_NAME')) { define('MAX_DOCUMENT_SIZE', env('MAX_DOCUMENT_SIZE', 10000)); // KB define('MAX_EMAIL_DOCUMENTS_SIZE', env('MAX_EMAIL_DOCUMENTS_SIZE', 10000)); // Total KB define('MAX_ZIP_DOCUMENTS_SIZE', env('MAX_EMAIL_DOCUMENTS_SIZE', 30000)); // Total KB (uncompressed) - define('MAX_EMAILS_SENT_PER_HOUR', 90); + define('MAX_EMAILS_SENT_PER_DAY', 80); define('DOCUMENT_PREVIEW_SIZE', env('DOCUMENT_PREVIEW_SIZE', 300)); // pixels define('DEFAULT_FONT_SIZE', 9); define('DEFAULT_HEADER_FONT', 1); // Roboto diff --git a/app/Ninja/Mailers/ContactMailer.php b/app/Ninja/Mailers/ContactMailer.php index 50dbb8727919..714943bb54df 100644 --- a/app/Ninja/Mailers/ContactMailer.php +++ b/app/Ninja/Mailers/ContactMailer.php @@ -378,26 +378,26 @@ class ContactMailer extends Mailer $key = $account->company_id; // http://stackoverflow.com/questions/1375501/how-do-i-throttle-my-sites-api-users - $hour = 60 * 60; - $hour_limit = MAX_EMAILS_SENT_PER_HOUR; - $hour_throttle = Cache::get("email_hour_throttle:{$key}", null); + $day = 60 * 60 * 24; + $day_limit = MAX_EMAILS_SENT_PER_DAY; + $day_throttle = Cache::get("email_day_throttle:{$key}", null); $last_api_request = Cache::get("last_email_request:{$key}", 0); $last_api_diff = time() - $last_api_request; - if (is_null($hour_throttle)) { - $new_hour_throttle = 0; + if (is_null($day_throttle)) { + $new_day_throttle = 0; } else { - $new_hour_throttle = $hour_throttle - $last_api_diff; - $new_hour_throttle = $new_hour_throttle < 0 ? 0 : $new_hour_throttle; - $new_hour_throttle += $hour / $hour_limit; - $hour_hits_remaining = floor(($hour - $new_hour_throttle) * $hour_limit / $hour); - $hour_hits_remaining = $hour_hits_remaining >= 0 ? $hour_hits_remaining : 0; + $new_day_throttle = $day_throttle - $last_api_diff; + $new_day_throttle = $new_day_throttle < 0 ? 0 : $new_day_throttle; + $new_day_throttle += $day / $day_limit; + $day_hits_remaining = floor(($day - $new_day_throttle) * $day_limit / $day); + $day_hits_remaining = $day_hits_remaining >= 0 ? $day_hits_remaining : 0; } - Cache::put("email_hour_throttle:{$key}", $new_hour_throttle, 60); + Cache::put("email_day_throttle:{$key}", $new_day_throttle, 60); Cache::put("last_email_request:{$key}", time(), 60); - if ($new_hour_throttle > $hour) { + if ($new_day_throttle > $day) { $errorEmail = env('ERROR_EMAIL'); if ($errorEmail && ! Cache::get("throttle_notified:{$key}")) { Mail::raw('Account Throttle', function ($message) use ($errorEmail, $account) { @@ -406,7 +406,7 @@ class ContactMailer extends Mailer ->subject("Email throttle triggered for account " . $account->id); }); } - Cache::put("throttle_notified:{$key}", true, 60); + Cache::put("throttle_notified:{$key}", true, 60 * 24); return true; }