You are here

public function ReplicaKillSwitch::checkReplicaServer in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Database/ReplicaKillSwitch.php \Drupal\Core\Database\ReplicaKillSwitch::checkReplicaServer()

Checks and disables the replica database server if appropriate.

Parameters

\Symfony\Component\HttpKernel\Event\RequestEvent $event: The Event to process.

File

core/lib/Drupal/Core/Database/ReplicaKillSwitch.php, line 79

Class

ReplicaKillSwitch
Provides replica server kill switch to ignore it.

Namespace

Drupal\Core\Database

Code

public function checkReplicaServer(RequestEvent $event) {

  // Ignore replica database servers for this request.
  //
  // In Drupal's distributed database structure, new data is written to the
  // master and then propagated to the replica servers.  This means there is a
  // lag between when data is written to the master and when it is available
  // on the replica. At these times, we will want to avoid using a replica
  // server temporarily. For example, if a user posts a new node then we want
  // to disable the replica server for that user temporarily to allow the
  // replica server to catch up.
  // That way, that user will see their changes immediately while for other
  // users we still get the benefits of having a replica server, just with
  // slightly stale data. Code that wants to disable the replica server should
  // use the 'database.replica_kill_switch' service's trigger() method to set
  // 'ignore_replica_server' session flag to the timestamp after which the
  // replica can be re-enabled.
  if ($this->session
    ->has('ignore_replica_server')) {
    if ($this->session
      ->get('ignore_replica_server') >= $this->time
      ->getRequestTime()) {
      Database::ignoreTarget('default', 'replica');
    }
    else {
      $this->session
        ->remove('ignore_replica_server');
    }
  }
}