Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1428138
LimitBatch.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
LimitBatch.php
View Options
<?php
namespace
Wikimedia\WRStats
;
/**
* A class representing a batch of increment/peek operations on a WRStatsRateLimiter
*
* @since 1.39
*/
class
LimitBatch
{
/** @var WRStatsRateLimiter */
private
$limiter
;
/** @var int */
private
$defaultAmount
;
/** @var LimitOperation[] */
private
$operations
=
[];
/**
* @internal Use WRStatsFactory::createRateLimiter()->createBatch() instead
* @param WRStatsRateLimiter $limiter
* @param int $defaultAmount
*/
public
function
__construct
(
WRStatsRateLimiter
$limiter
,
$defaultAmount
)
{
$this
->
limiter
=
$limiter
;
$this
->
defaultAmount
=
$defaultAmount
;
}
/**
* Construct a local entity key and queue an operation for it.
*
* @param string $condName The condition name to be incremented/tested,
* which must match one of the ones passed to createRateLimiter()
* @param mixed $components Entity key component or array of components
* @param int|null $amount The amount to increment by, or null to use the default
* @return $this
*/
public
function
localOp
(
$condName
,
$components
=
[],
$amount
=
null
)
{
if
(
!
is_array
(
$components
)
)
{
$components
=
[
$components
];
}
$this
->
queueOp
(
$condName
,
new
LocalEntityKey
(
array_merge
(
[
$condName
],
$components
)
),
$amount
);
return
$this
;
}
/**
* Construct a global entity key and queue an operation for it.
*
* @param string $condName The condition name to be incremented/tested,
* which must match one of the ones passed to createRateLimiter()
* @param mixed $components Entity key components
* @param int|null $amount The amount, or null to use the default
* @return $this
*/
public
function
globalOp
(
$condName
,
$components
=
[],
$amount
=
null
)
{
if
(
!
is_array
(
$components
)
)
{
$components
=
[
$components
];
}
$this
->
queueOp
(
$condName
,
new
GlobalEntityKey
(
array_merge
(
[
$condName
],
$components
)
),
$amount
);
return
$this
;
}
private
function
queueOp
(
string
$type
,
?
EntityKey
$entity
,
?
int
$amount
)
{
$amount
??=
$this
->
defaultAmount
;
if
(
isset
(
$this
->
operations
[
$type
]
)
)
{
throw
new
WRStatsError
(
'Cannot queue multiple actions of the same type, '
.
'because the result array is indexed by type'
);
}
$this
->
operations
[
$type
]
=
new
LimitOperation
(
$type
,
$entity
,
$amount
);
}
/**
* Execute the batch, checking each operation against the defined limit,
* but don't actually increment the metrics.
*
* @return LimitBatchResult
*/
public
function
peek
()
{
return
$this
->
limiter
->
peekBatch
(
$this
->
operations
);
}
/**
* Execute the batch, unconditionally incrementing all the specified metrics.
*/
public
function
incr
()
{
$this
->
limiter
->
incrBatch
(
$this
->
operations
);
}
/**
* Execute the batch, checking each operation against the defined limit.
* If all operations are allowed, all metrics will be incremented. If some
* of the operations exceed the limit, none of the metrics will be
* incremented.
*
* @return LimitBatchResult
*/
public
function
tryIncr
()
{
return
$this
->
limiter
->
tryIncrBatch
(
$this
->
operations
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 15:42 (14 h, 44 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
9d/b7/a06734688888595188b46144022a
Default Alt Text
LimitBatch.php (3 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment