MediaWiki  REL1_24
PoolCounter.php
Go to the documentation of this file.
00001 <?php
00042 abstract class PoolCounter {
00043     /* Return codes */
00044     const LOCKED = 1; /* Lock acquired */
00045     const RELEASED = 2; /* Lock released */
00046     const DONE = 3; /* Another worker did the work for you */
00047 
00048     const ERROR = -1; /* Indeterminate error */
00049     const NOT_LOCKED = -2; /* Called release() with no lock held */
00050     const QUEUE_FULL = -3; /* There are already maxqueue workers on this lock */
00051     const TIMEOUT = -4; /* Timeout exceeded */
00052     const LOCK_HELD = -5; /* Cannot acquire another lock while you have one lock held */
00053 
00055     protected $key;
00057     protected $workers;
00064     protected $slots = 0;
00066     protected $maxqueue;
00068     protected $timeout;
00069 
00075     protected function __construct( $conf, $type, $key ) {
00076         $this->workers = $conf['workers'];
00077         $this->maxqueue = $conf['maxqueue'];
00078         $this->timeout = $conf['timeout'];
00079         if ( isset( $conf['slots'] ) ) {
00080             $this->slots = $conf['slots'];
00081         }
00082 
00083         if ( $this->slots ) {
00084             $key = $this->hashKeyIntoSlots( $key, $this->slots );
00085         }
00086         $this->key = $key;
00087     }
00088 
00097     public static function factory( $type, $key ) {
00098         global $wgPoolCounterConf;
00099         if ( !isset( $wgPoolCounterConf[$type] ) ) {
00100             return new PoolCounter_Stub;
00101         }
00102         $conf = $wgPoolCounterConf[$type];
00103         $class = $conf['class'];
00104 
00105         return new $class( $conf, $type, $key );
00106     }
00107 
00111     public function getKey() {
00112         return $this->key;
00113     }
00114 
00120     abstract public function acquireForMe();
00121 
00128     abstract public function acquireForAnyone();
00129 
00137     abstract public function release();
00138 
00149     protected function hashKeyIntoSlots( $key, $slots ) {
00150         return hexdec( substr( sha1( $key ), 0, 4 ) ) % $slots;
00151     }
00152 }
00153 
00154 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
00155 class PoolCounter_Stub extends PoolCounter {
00156     // @codingStandardsIgnoreEnd
00157 
00158     public function __construct() {
00159         /* No parameters needed */
00160     }
00161 
00162     public function acquireForMe() {
00163         return Status::newGood( PoolCounter::LOCKED );
00164     }
00165 
00166     public function acquireForAnyone() {
00167         return Status::newGood( PoolCounter::LOCKED );
00168     }
00169 
00170     public function release() {
00171         return Status::newGood( PoolCounter::RELEASED );
00172     }
00173 }