/**
* @param array $conf
- * @param string $type
+ * @param string $type The class of actions to limit concurrency for (task type)
* @param string $key
*/
protected function __construct( $conf, $type, $key ) {
}
if ( $this->slots ) {
- $key = $this->hashKeyIntoSlots( $key, $this->slots );
+ $key = $this->hashKeyIntoSlots( $type, $key, $this->slots );
}
+
$this->key = $key;
$this->isMightWaitKey = !preg_match( '/^nowait:/', $this->key );
}
/**
* Create a Pool counter. This should only be called from the PoolWorks.
*
- * @param string $type
+ * @param string $type The class of actions to limit concurrency for (task type)
* @param string $key
*
* @return PoolCounter
}
/**
- * Given a key (any string) and the number of lots, returns a slot number (an integer from
- * the [0..($slots-1)] range). This is used for a global limit on the number of instances of
- * a given type that can acquire a lock. The hashing is deterministic so that
+ * Given a key (any string) and the number of lots, returns a slot key (a prefix with a suffix
+ * integer from the [0..($slots-1)] range). This is used for a global limit on the number of
+ * instances of a given type that can acquire a lock. The hashing is deterministic so that
* PoolCounter::$workers is always an upper limit of how many instances with the same key
* can acquire a lock.
*
+ * @param string $type The class of actions to limit concurrency for (task type)
* @param string $key PoolCounter instance key (any string)
* @param int $slots The number of slots (max allowed value is 65536)
- * @return int
+ * @return string Slot key with the type and slot number
*/
- protected function hashKeyIntoSlots( $key, $slots ) {
- return hexdec( substr( sha1( $key ), 0, 4 ) ) % $slots;
+ protected function hashKeyIntoSlots( $type, $key, $slots ) {
+ return $type . ':' . ( hexdec( substr( sha1( $key ), 0, 4 ) ) % $slots );
}
}
protected $cacheable = false; // does this override getCachedWork() ?
/**
- * @param string $type The type of PoolCounter to use
+ * @param string $type The class of actions to limit concurrency for (task type)
* @param string $key Key that identifies the queue this work is placed on
*/
public function __construct( $type, $key ) {
* If a 'doCachedWork' callback is provided, then execute() may wait for any prior
* process in the pool to finish and reuse its cached result.
*
- * @param string $type
+ * @param string $type The class of actions to limit concurrency for
* @param string $key
* @param array $callbacks Map of callbacks
* @throws MWException
$keysWithTwoSlots = $keysWithFiveSlots = [];
foreach ( range( 1, 100 ) as $i ) {
- $keysWithTwoSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'key ' . $i, 2 );
- $keysWithFiveSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'key ' . $i, 5 );
+ $keysWithTwoSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 2 );
+ $keysWithFiveSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 5 );
}
- $this->assertArrayEquals( range( 0, 1 ), array_unique( $keysWithTwoSlots ) );
- $this->assertArrayEquals( range( 0, 4 ), array_unique( $keysWithFiveSlots ) );
+ $twoSlotKeys = [];
+ for ( $i = 0; $i <= 1; $i++ ) {
+ $twoSlotKeys[] = "test:$i";
+ }
+ $fiveSlotKeys = [];
+ for ( $i = 0; $i <= 4; $i++ ) {
+ $fiveSlotKeys[] = "test:$i";
+ }
+
+ $this->assertArrayEquals( $twoSlotKeys, array_unique( $keysWithTwoSlots ) );
+ $this->assertArrayEquals( $fiveSlotKeys, array_unique( $keysWithFiveSlots ) );
// make sure it is deterministic
$this->assertEquals(
- $hashKeyIntoSlots->invoke( $poolCounter, 'asdfgh', 1000 ),
- $hashKeyIntoSlots->invoke( $poolCounter, 'asdfgh', 1000 )
+ $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 ),
+ $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 )
);
}
}