39 $this->store = $store;
40 $this->conditions = $conditions;
41 $this->prefix = $prefix;
45 foreach ( $conditions as $name => $condition ) {
48 'timeStep' => $condition->window / $bucketCount,
49 'expiry' => $condition->window
53 $this->specs = $specs;
64 return new LimitBatch( $this, $defaultAmount );
81 $actions = [ new
LimitOperation( $condName, $entityKey, $amount ) ];
83 return $result->getAllResults()[0];
94 $reader =
new WRStatsReader( $this->store, $this->specs, $this->prefix );
95 if ( $this->now !==
null ) {
96 $reader->setCurrentTime( $this->now );
101 foreach ( $operations as $operation ) {
102 $name = $operation->condName;
103 $cond = $this->conditions[$name] ??
null;
104 if ( $cond ===
null ) {
106 ": unrecognized metric \"$name\"" );
108 if ( !isset( $rates[$name] ) ) {
109 $range = $reader->latest( $cond->window );
110 $rates[$name] = $reader->getRate( $name, $operation->entityKey, $range );
113 $amounts[$name] += $operation->amount;
117 foreach ( $operations as $i => $operation ) {
118 $name = $operation->condName;
119 $total = $rates[$name]->total();
120 $cond = $this->conditions[$name];
124 $total + $amounts[$name]
144 $actions = [ new
LimitOperation( $condName, $entityKey, $amount ) ];
145 $result = $this->tryIncrBatch( $actions );
146 return $result->getAllResults()[0];
157 $result = $this->peekBatch( $operations );
158 if ( $result->isAllowed() ) {
159 $this->incrBatch( $operations );
177 $actions = [
new LimitOperation( $condName, $entityKey, $amount ) ];
178 $this->incrBatch( $actions );
187 $writer =
new WRStatsWriter( $this->store, $this->specs, $this->prefix );
188 if ( $this->now !==
null ) {
189 $writer->setCurrentTime( $this->now );
191 foreach ( $operations as $operation ) {
193 $operation->condName,
194 $operation->entityKey,