3 namespace Drupal\Core\EventSubscriber;
5 use Drupal\Core\Database\Database;
6 use Symfony\Component\HttpKernel\KernelEvents;
7 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
8 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11 * System subscriber for controller requests.
13 class ReplicaDatabaseIgnoreSubscriber implements EventSubscriberInterface {
16 * Checks and disables the replica database server if appropriate.
18 * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
19 * The Event to process.
21 public function checkReplicaServer(GetResponseEvent $event) {
22 // Ignore replica database servers for this request.
24 // In Drupal's distributed database structure, new data is written to the
25 // master and then propagated to the replica servers. This means there is a
26 // lag between when data is written to the master and when it is available
27 // on the replica. At these times, we will want to avoid using a replica server
28 // temporarily. For example, if a user posts a new node then we want to
29 // disable the replica server for that user temporarily to allow the replica
30 // server to catch up.
31 // That way, that user will see their changes immediately while for other
32 // users we still get the benefits of having a replica server, just with
33 // slightly stale data. Code that wants to disable the replica server should
34 // use the db_set_ignore_replica() function to set
35 // $_SESSION['ignore_replica_server'] to the timestamp after which the replica
37 if (isset($_SESSION['ignore_replica_server'])) {
38 if ($_SESSION['ignore_replica_server'] >= REQUEST_TIME) {
39 Database::ignoreTarget('default', 'replica');
42 unset($_SESSION['ignore_replica_server']);
50 public static function getSubscribedEvents() {
51 $events[KernelEvents::REQUEST][] = ['checkReplicaServer'];