2022-12-14 21:56:52 +00:00
|
|
|
<?php
|
|
|
|
|
namespace MediaWiki\PoolCounter;
|
|
|
|
|
|
2024-03-01 19:00:23 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
2022-12-14 21:56:52 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.40
|
|
|
|
|
*/
|
|
|
|
|
class PoolCounterFactory {
|
|
|
|
|
private ?PoolCounterConnectionManager $manager = null;
|
|
|
|
|
private ?array $typeConfigs;
|
|
|
|
|
private array $clientConf;
|
2024-03-01 19:00:23 +00:00
|
|
|
private LoggerInterface $logger;
|
2022-12-14 21:56:52 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @internal For use by ServiceWiring
|
|
|
|
|
* @param array|null $typeConfigs See $wgPoolCounterConf
|
|
|
|
|
* @param array $clientConf See $wgPoolCountClientConf
|
2024-03-01 19:00:23 +00:00
|
|
|
* @param LoggerInterface $logger
|
2022-12-14 21:56:52 +00:00
|
|
|
*/
|
2024-03-01 19:00:23 +00:00
|
|
|
public function __construct( ?array $typeConfigs, array $clientConf, LoggerInterface $logger ) {
|
2022-12-14 21:56:52 +00:00
|
|
|
$this->typeConfigs = $typeConfigs;
|
|
|
|
|
$this->clientConf = $clientConf;
|
2024-03-01 19:00:23 +00:00
|
|
|
$this->logger = $logger;
|
2022-12-14 21:56:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getClientManager(): PoolCounterConnectionManager {
|
|
|
|
|
$this->manager ??= new PoolCounterConnectionManager( $this->clientConf );
|
|
|
|
|
return $this->manager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a PoolCounter.
|
|
|
|
|
*
|
|
|
|
|
* @internal This should only be called from PoolCounterWork
|
|
|
|
|
* @param string $type The class of actions to limit concurrency for (task type)
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return PoolCounter
|
|
|
|
|
*/
|
|
|
|
|
public function create( string $type, string $key ): PoolCounter {
|
|
|
|
|
$conf = $this->typeConfigs[$type] ?? null;
|
|
|
|
|
if ( $conf === null ) {
|
|
|
|
|
return new PoolCounterNull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$class = $conf['class'] ?? null;
|
|
|
|
|
if ( $class === 'PoolCounter_Client' ) {
|
|
|
|
|
// Since 1.16: Introduce PoolCounter_Client in PoolCounter extension.
|
|
|
|
|
// Since 1.40: Move to core as symbolic name, discourage use of class name.
|
|
|
|
|
$class = PoolCounterClient::class;
|
|
|
|
|
}
|
|
|
|
|
/** @var PoolCounter $poolCounter */
|
|
|
|
|
$poolCounter = new $class( $conf, $type, $key );
|
2024-03-01 19:00:23 +00:00
|
|
|
$poolCounter->setLogger( $this->logger );
|
2022-12-14 21:56:52 +00:00
|
|
|
|
|
|
|
|
// Support subclass for back-compat with the extension
|
|
|
|
|
if ( $poolCounter instanceof PoolCounterClient ) {
|
|
|
|
|
$poolCounter->setManager( $this->getClientManager() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $poolCounter;
|
|
|
|
|
}
|
|
|
|
|
}
|