aboutsummaryrefslogtreecommitdiffstats
path: root/includes/poolcounter/PoolCounterFactory.php
blob: 80a1f3916b40c2a16822e88abbabb4c72be0a601 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace MediaWiki\PoolCounter;

use Psr\Log\LoggerInterface;
use Wikimedia\Telemetry\TracerInterface;

/**
 * @since 1.40
 */
class PoolCounterFactory {
	private ?PoolCounterConnectionManager $manager = null;
	private ?array $typeConfigs;
	private array $clientConf;
	private LoggerInterface $logger;
	private TracerInterface $tracer;

	/**
	 * @internal For use by ServiceWiring
	 * @param array|null $typeConfigs See $wgPoolCounterConf
	 * @param array $clientConf See $wgPoolCountClientConf
	 * @param LoggerInterface $logger
	 */
	public function __construct( ?array $typeConfigs, array $clientConf,
		LoggerInterface $logger, TracerInterface $tracer ) {
		$this->typeConfigs = $typeConfigs;
		$this->clientConf = $clientConf;
		$this->logger = $logger;
		$this->tracer = $tracer;
	}

	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 );
		$poolCounter->setLogger( $this->logger );
		$poolCounter->setTracer( $this->tracer );

		// Support subclass for back-compat with the extension
		if ( $poolCounter instanceof PoolCounterClient ) {
			$poolCounter->setManager( $this->getClientManager() );
		}

		return $poolCounter;
	}
}