MediaWiki master
LimitBatch.php
Go to the documentation of this file.
1<?php
2
3namespace Wikimedia\WRStats;
4
12 private $limiter;
13
15 private $defaultAmount;
16
18 private $operations = [];
19
25 public function __construct(
26 WRStatsRateLimiter $limiter,
27 $defaultAmount
28 ) {
29 $this->limiter = $limiter;
30 $this->defaultAmount = $defaultAmount;
31 }
32
42 public function localOp( $condName, $components = [], $amount = null ) {
43 if ( !is_array( $components ) ) {
44 $components = [ $components ];
45 }
46 $this->queueOp(
47 $condName,
48 new LocalEntityKey( array_merge( [ $condName ], $components ) ),
49 $amount
50 );
51 return $this;
52 }
53
63 public function globalOp( $condName, $components = [], $amount = null ) {
64 if ( !is_array( $components ) ) {
65 $components = [ $components ];
66 }
67 $this->queueOp(
68 $condName,
69 new GlobalEntityKey( array_merge( [ $condName ], $components ) ),
70 $amount
71 );
72 return $this;
73 }
74
75 private function queueOp( $type, $entity, $amount ) {
76 $amount ??= $this->defaultAmount;
77 if ( isset( $this->operations[$type] ) ) {
78 throw new WRStatsError( 'Cannot queue multiple actions of the same type, ' .
79 'because the result array is indexed by type' );
80 }
81 $this->operations[$type] = new LimitOperation( $type, $entity, $amount );
82 }
83
90 public function peek() {
91 return $this->limiter->peekBatch( $this->operations );
92 }
93
97 public function incr() {
98 $this->limiter->incrBatch( $this->operations );
99 }
100
109 public function tryIncr() {
110 return $this->limiter->tryIncrBatch( $this->operations );
111 }
112}
Entity key with isGlobal=true.
A class representing a batch of increment/peek operations on a WRStatsRateLimiter.
incr()
Execute the batch, unconditionally incrementing all the specified metrics.
localOp( $condName, $components=[], $amount=null)
Construct a local entity key and queue an operation for it.
tryIncr()
Execute the batch, checking each operation against the defined limit.
__construct(WRStatsRateLimiter $limiter, $defaultAmount)
globalOp( $condName, $components=[], $amount=null)
Construct a global entity key and queue an operation for it.
peek()
Execute the batch, checking each operation against the defined limit, but don't actually increment th...
One item in a LimitBatch.
Entity key with isGlobal=false.
Exception class for errors thrown by the WRStats library.
A rate limiter with a WRStats backend.