You are here

class ApiRateLimitCheck in CloudFlare 8

Checks that the site is within CloudFlare's API rate limits.

CloudFlare currently has a rate limit of 1200 Api calls every 5 minutes.

Plugin annotation


@PurgeDiagnosticCheck(
  id = "cloudflare_api_rate_limit_check",
  title = @Translation("CloudFlare - Api Rate limit check."),
  description = @Translation("Checks that the site is not violating CloudFlare's overall Api rate limit."),
  dependent_queue_plugins = {},
  dependent_purger_plugins = {"cloudflare"}
)

Hierarchy

Expanded class hierarchy of ApiRateLimitCheck

See also

https://api.cloudflare.com/#requests

1 file declares its use of ApiRateLimitCheck
ApiRateLimitCheckTest.php in modules/cloudflarepurger/tests/src/Unit/ApiRateLimitCheckTest.php

File

modules/cloudflarepurger/src/Plugin/Purge/DiagnosticCheck/ApiRateLimitCheck.php, line 27

Namespace

Drupal\cloudflarepurger\Plugin\Purge\DiagnosticCheck
View source
class ApiRateLimitCheck extends DiagnosticCheckBase implements DiagnosticCheckInterface {

  /**
   * Tracks rate limits associated with CloudFlare Api.
   *
   * @var \Drupal\cloudflare\CloudFlareStateInterface
   */
  protected $state;

  /**
   * Checks that the Composer dependencies for CloudFlare are met.
   *
   * @var \Drupal\cloudflare\CloudFlareComposerDependenciesCheckInterface
   */
  protected $cloudFlareComposerDependenciesCheck;

  /**
   * Constructs a ApiRateLimitCheck diagnostic check object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\cloudflare\CloudFlareStateInterface $state
   *   Tracks rate limits associated with CloudFlare Api.
   * @param \Drupal\cloudflare\CloudFlareComposerDependenciesCheckInterface $check_interface
   *   Checks that the composer dependencies for CloudFlare are met.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, CloudFlareStateInterface $state, CloudFlareComposerDependenciesCheckInterface $check_interface) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->state = $state;
    $this->cloudFlareComposerDependenciesCheck = $check_interface;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('cloudflare.state'), $container
      ->get('cloudflare.composer_dependency_check'));
  }

  /**
   * {@inheritdoc}
   */
  public function run() {
    if (!$this->cloudFlareComposerDependenciesCheck
      ->check()) {
      $this->recommendation = $this
        ->t("Composer dependencies unmet.  Unable to assess API rate limits.");
      return SELF::SEVERITY_ERROR;
    }

    // Current number of purges today.
    $rate_count = $this->state
      ->getApiRateCount();
    $this->value = $rate_count;

    // Warn at 75% of capacity.
    $daily_warning_level = 0.75 * CloudFlareAPI::API_RATE_LIMIT;
    $message_variables = [
      ':rate_limit' => CloudFlareAPI::API_RATE_LIMIT,
      ':$rate_count' => $rate_count,
    ];
    if ($rate_count >= CloudFlareAPI::API_RATE_LIMIT) {
      $this->recommendation = $this
        ->t('Exceeded Api limit of :$rate_count/:rate_limit limit purges/day.', $message_variables);
      return SELF::SEVERITY_ERROR;
    }
    elseif ($rate_count >= $daily_warning_level) {
      $this->recommendation = $this
        ->t('Approaching Api limit of :$rate_count/:rate_limit limit purges/day.', $message_variables);
      return SELF::SEVERITY_WARNING;
    }
    elseif ($rate_count < $daily_warning_level) {
      $this->recommendation = $this
        ->t('Site is safely below the rate limit of :rate_limit every 5 minutes.', $message_variables);
      return SELF::SEVERITY_OK;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ApiRateLimitCheck::$cloudFlareComposerDependenciesCheck protected property Checks that the Composer dependencies for CloudFlare are met.
ApiRateLimitCheck::$state protected property Tracks rate limits associated with CloudFlare Api.
ApiRateLimitCheck::create public static function Creates an instance of the plugin. Overrides DiagnosticCheckBase::create
ApiRateLimitCheck::run public function Perform the check and determine the severity level. Overrides DiagnosticCheckInterface::run
ApiRateLimitCheck::__construct public function Constructs a ApiRateLimitCheck diagnostic check object. Overrides PluginBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DiagnosticCheckBase::$description private property The description of the check as described in the plugin's metadata.
DiagnosticCheckBase::$recommendation protected property A recommendation matching the severity level, may contain NULL.
DiagnosticCheckBase::$severity private property The severity of the outcome of this check.
DiagnosticCheckBase::$title private property The title of the check as described in the plugin's metadata.
DiagnosticCheckBase::$value protected property Optional check outcome / value (e.g. version numbers), may contain NULL.
DiagnosticCheckBase::getDescription public function Gets the description of the check. Overrides DiagnosticCheckInterface::getDescription
DiagnosticCheckBase::getRecommendation public function Get a recommendation matching the severity level, may return NULL. Overrides DiagnosticCheckInterface::getRecommendation
DiagnosticCheckBase::getRequirementsArray public function Generates a individual Drupal-like requirements array. Overrides DiagnosticCheckInterface::getRequirementsArray
DiagnosticCheckBase::getRequirementsSeverity public function Get the severity level, expressed as a status_report severity. Overrides DiagnosticCheckInterface::getRequirementsSeverity
DiagnosticCheckBase::getSeverity public function Get the severity level. Overrides DiagnosticCheckInterface::getSeverity
DiagnosticCheckBase::getSeverityString public function Get the severity level as unprefixed string. Overrides DiagnosticCheckInterface::getSeverityString
DiagnosticCheckBase::getTitle public function Gets the title of the check. Overrides DiagnosticCheckInterface::getTitle
DiagnosticCheckBase::getValue public function Get an optional value for the check output, may return NULL. Overrides DiagnosticCheckInterface::getValue
DiagnosticCheckBase::runCheck protected function Late runtime helper to assure that ::run() got called (and only once).
DiagnosticCheckInterface::SEVERITY_ERROR constant BLOCKING severity -- Error condition; purge.purgers service cannot operate.
DiagnosticCheckInterface::SEVERITY_INFO constant Non-blocking severity -- Informational message only.
DiagnosticCheckInterface::SEVERITY_OK constant Non-blocking severity -- check successfully passed.
DiagnosticCheckInterface::SEVERITY_WARNING constant Non-blocking severity -- Warning condition; proceed but flag warning.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.