Browse Source

Disallow certain commands when in replication mode.

Some of these commands do not actually pose any threat, but their replies can
be misleading since users do not know on which server they are connected to.
We prefer to make things explicit so users can get a new client object out of
a specific connection in the replication pool to issue these commands.
Daniele Alessandri 13 years ago
parent
commit
565690a5b1
1 changed files with 30 additions and 6 deletions
  1. 30 6
      lib/Predis/Network/PredisReplication.php

+ 30 - 6
lib/Predis/Network/PredisReplication.php

@@ -12,6 +12,7 @@
 namespace Predis\Network;
 
 use Predis\Commands\ICommand;
+use Predis\NotSupportedException;
 
 /**
  * Defines the standard virtual connection class that is used
@@ -22,6 +23,7 @@ use Predis\Commands\ICommand;
  */
 class PredisReplication implements IConnectionReplication
 {
+    private $disallowed = array();
     private $readonly = array();
     private $readonlySHA1 = array();
     private $current = null;
@@ -33,6 +35,7 @@ class PredisReplication implements IConnectionReplication
      */
     public function __construct()
     {
+        $this->disallowed = $this->getDisallowedOperations();
         $this->readonly = $this->getReadOnlyOperations();
     }
 
@@ -244,7 +247,11 @@ class PredisReplication implements IConnectionReplication
      */
     protected function isReadOperation(ICommand $command)
     {
-        if (isset($this->readonly[$id = $command->getId()])) {
+        if (isset($this->disallowed[$id = $command->getId()])) {
+            throw new NotSupportedException("The command $id is not allowed in replication mode");
+        }
+
+        if (isset($this->readonly[$id])) {
             if (true === $readonly = $this->readonly[$id]) {
                 return true;
             }
@@ -310,6 +317,28 @@ class PredisReplication implements IConnectionReplication
         }
     }
 
+    /**
+     * Returns the default list of disallowed commands.
+     *
+     * @return array
+     */
+    protected function getDisallowedOperations()
+    {
+        return array(
+            'SHUTDOWN'          => true,
+            'INFO'              => true,
+            'DBSIZE'            => true,
+            'LASTSAVE'          => true,
+            'CONFIG'            => true,
+            'MONITOR'           => true,
+            'SLAVEOF'           => true,
+            'SAVE'              => true,
+            'BGSAVE'            => true,
+            'BGREWRITEAOF'      => true,
+            'SLOWLOG'           => true,
+        );
+    }
+
     /**
      * Returns the default list of commands performing read-only operations.
      *
@@ -360,11 +389,6 @@ class PredisReplication implements IConnectionReplication
             'SELECT'            => true,
             'ECHO'              => true,
             'QUIT'              => true,
-            'INFO'              => true,
-            'DBSIZE'            => true,
-            'MONITOR'           => true,
-            'LASTSAVE'          => true,
-            'SHUTDOWN'          => true,
             'OBJECT'            => true,
             'SORT'              => function(ICommand $command) {
                 $arguments = $command->getArguments();