38 $this->store = $store;
39 $this->conditions = $conditions;
40 $this->prefix = $prefix;
44 foreach ( $conditions as $name => $condition ) {
47 'timeStep' => $condition->window / $bucketCount,
48 'expiry' => $condition->window
52 $this->specs = $specs;
63 return new LimitBatch( $this, $defaultAmount );
80 $actions = [ new
LimitOperation( $condName, $entityKey, $amount ) ];
82 return $result->getAllResults()[0];
93 $reader =
new WRStatsReader( $this->store, $this->specs, $this->prefix );
94 if ( $this->now !==
null ) {
95 $reader->setCurrentTime( $this->now );
100 foreach ( $operations as $operation ) {
101 $name = $operation->condName;
102 $cond = $this->conditions[$name] ??
null;
103 if ( $cond ===
null ) {
104 throw new WRStatsError(
"Unrecognized metric \"$name\"" );
106 if ( !isset( $rates[$name] ) ) {
107 $range = $reader->latest( $cond->window );
108 $rates[$name] = $reader->getRate( $name, $operation->entityKey, $range );
111 $amounts[$name] += $operation->amount;
115 foreach ( $operations as $i => $operation ) {
116 $name = $operation->condName;
117 $total = $rates[$name]->total();
118 $cond = $this->conditions[$name];
122 $total + $amounts[$name]
142 $actions = [ new
LimitOperation( $condName, $entityKey, $amount ) ];
143 $result = $this->tryIncrBatch( $actions );
144 return $result->getAllResults()[0];
155 $result = $this->peekBatch( $operations );
156 if ( $result->isAllowed() ) {
157 $this->incrBatch( $operations );
175 $actions = [
new LimitOperation( $condName, $entityKey, $amount ) ];
176 $this->incrBatch( $actions );
185 $writer =
new WRStatsWriter( $this->store, $this->specs, $this->prefix );
186 if ( $this->now !==
null ) {
187 $writer->setCurrentTime( $this->now );
189 foreach ( $operations as $operation ) {
191 $operation->condName,
192 $operation->entityKey,