MediaWiki REL1_40
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
26 public function __construct(
27 WRStatsRateLimiter $limiter,
28 $defaultAmount
29 ) {
30 $this->limiter = $limiter;
31 $this->defaultAmount = $defaultAmount;
32 }
33
43 public function localOp( $condName, $components = [], $amount = null ) {
44 if ( !is_array( $components ) ) {
45 $components = [ $components ];
46 }
47 $this->queueOp(
48 $condName,
49 new LocalEntityKey( array_merge( [ $condName ], $components ) ),
50 $amount
51 );
52 return $this;
53 }
54
64 public function globalOp( $condName, $components = [], $amount = null ) {
65 if ( !is_array( $components ) ) {
66 $components = [ $components ];
67 }
68 $this->queueOp(
69 $condName,
70 new GlobalEntityKey( array_merge( [ $condName ], $components ) ),
71 $amount
72 );
73 return $this;
74 }
75
76 private function queueOp( $type, $entity, $amount ) {
77 $amount ??= $this->defaultAmount;
78 if ( isset( $this->operations[$type] ) ) {
79 throw new WRStatsError( __METHOD__ .
80 ': cannot queue multiple actions of the same type, ' .
81 'since the result array is indexed by type' );
82 }
83 $this->operations[$type] = new LimitOperation( $type, $entity, $amount );
84 }
85
92 public function peek() {
93 return $this->limiter->peekBatch( $this->operations );
94 }
95
99 public function incr() {
100 $this->limiter->incrBatch( $this->operations );
101 }
102
111 public function tryIncr() {
112 return $this->limiter->tryIncrBatch( $this->operations );
113 }
114}
Entity key with global=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...
Class representing one item in a limit batch.
Entity key with global=false.
Exception class for errors thrown by the WRStats library.
A rate limiter with a WRStats backend.