diff --git a/modules/cloudflarepurger/src/Plugin/Purge/DiagnosticCheck/ApiRateLimitCheck.php b/modules/cloudflarepurger/src/Plugin/Purge/DiagnosticCheck/ApiRateLimitCheck.php index e5db7d3..152ad0f 100644 --- a/modules/cloudflarepurger/src/Plugin/Purge/DiagnosticCheck/ApiRateLimitCheck.php +++ b/modules/cloudflarepurger/src/Plugin/Purge/DiagnosticCheck/ApiRateLimitCheck.php @@ -84,6 +84,9 @@ class ApiRateLimitCheck extends DiagnosticCheckBase implements DiagnosticCheckIn return self::SEVERITY_ERROR; } + // Reset the 5 minute API rate count if 5 minutes has elapsed. + $this->state->resetApiRateCount(); + // Current number of purges today. $rate_count = $this->state->getApiRateCount(); $this->value = $rate_count; diff --git a/src/CloudFlareStateInterface.php b/src/CloudFlareStateInterface.php index 8bc8b62..bd71d6e 100644 --- a/src/CloudFlareStateInterface.php +++ b/src/CloudFlareStateInterface.php @@ -30,6 +30,11 @@ interface CloudFlareStateInterface { */ public function incrementApiRateCount(); + /** + * Reset the 5 minute API rate count if 5 minutes has elapsed. + */ + public function resetApiRateCount(); + /** * Increment the count of tag purges done today. * diff --git a/src/State.php b/src/State.php index de7614b..cac6a0f 100644 --- a/src/State.php +++ b/src/State.php @@ -18,7 +18,7 @@ class State implements CloudFlareStateInterface { /** * Tracks rate limits associated with CloudFlare Api. * - * @var \Drupal\cloudflare\CloudFlareStateInterface + * @var \Drupal\Core\State\StateInterface */ protected $state; @@ -97,6 +97,23 @@ class State implements CloudFlareStateInterface { } } + /** + * {@inheritdoc} + */ + public function resetApiRateCount() { + $last_recorded_timestamp = $this->state->get(self::API_RATE_COUNT_START); + $last_recorded_timestamp = is_null($last_recorded_timestamp) ? new \DateTime('2001-01-01') : $last_recorded_timestamp; + + $now = $this->timestamper->now(); + $diff = $now->getTimestamp() - $last_recorded_timestamp->getTimestamp(); + $minutes_passed = $diff / 60; + + if ($minutes_passed >= 5) { + $this->state->set(self::API_RATE_COUNT, 0); + $this->state->set(self::API_RATE_COUNT_START, $now); + } + } + /** * {@inheritdoc} */