Unverified Commit 7481b88b authored by Matt Glaman's avatar Matt Glaman Committed by GitHub
Browse files

Issue #3313839 by balintpekker: Add link to Knowledge Base article when flood limit's reached (#33)



* Issue #3313839: Added link to flood control error message

* Issue #3313839: Removed URL and Link objects from logging output

* Issue #3313839: Removed URL and Link objects from subscriber

Co-authored-by: default avatarBalint Pekker <balint.pekker@cheppers.com>
parent c85fb759
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ use Drupal\acquia_search\AcquiaCryptConnector;
use Drupal\acquia_search\Helper\Flood;
use Drupal\acquia_search\PreferredCoreService;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Solarium\Core\Client\Adapter\AdapterHelper;
use Solarium\Core\Client\Response;
use Solarium\Core\Event\Events;
@@ -23,6 +24,8 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 */
class SearchSubscriber extends AbstractPlugin implements EventSubscriberInterface {

  use StringTranslationTrait;

  /**
   * Acquia search endpoint.
   *
@@ -110,11 +113,13 @@ class SearchSubscriber extends AbstractPlugin implements EventSubscriberInterfac
    // Run Flood control checks.
    if (!Flood::isAllowed($request->getHandler())) {
      // If request should be blocked, show an error message.
      $message = 'The Acquia Search flood control mechanism has blocked a Solr query due to API usage limits. You should retry in a few seconds. Contact the site administrator if this message persists.';
      $message = $this->t('<a href=":link">The Acquia Search flood control mechanism has blocked a Solr query due to API usage limits.</a> You should retry in a few seconds. Contact the site administrator if this message persists.', [
        ':link' => Flood::FLOOD_LIMIT_ARTICLE_URL,
      ]);
      \Drupal::messenger()->addError($message);

      // Build a static response which avoids a network request to Solr.
      $response = new Response($message, ['HTTP/1.1 429 Too Many Requests']);
      $response = new Response((string) $message, ['HTTP/1.1 429 Too Many Requests']);
      $event->setResponse($response);
      $event->stopPropagation();
      return;
+7 −2
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ namespace Drupal\acquia_search\Helper;
 */
class Flood {

  const FLOOD_LIMIT_ARTICLE_URL = "https://support-acquia.force.com/s/article/1500008925761-The-Acquia-Search-flood-control-mechanism-has-blocked-a-Solr-query-due-to-API-usage-limits";

  /**
   * List of values by each Solarium request type.
   */
@@ -82,8 +84,11 @@ class Flood {
  public static function logFloodLimit(string $request_type) {
    if (self::isLoggingEnabled()) {
      \Drupal::logger('acquia_search')->warning(
        'Flood protection has blocked request of type @id.',
        ['@id' => $request_type]
        'Flood protection has blocked request of type @id. See more at <a href="@url">The Acquia Search flood control mechanism has blocked a Solr query due to API usage limits</a>',
        [
          '@id' => $request_type,
          '@url' => self::FLOOD_LIMIT_ARTICLE_URL,
        ]
      );
    }
  }