Clarify code and docs for automatic throttling

For the docs part: make it clear how things work there. For the code
part, these are mostly style changes: shorter variable names, no
unnecessary parameters, make the method private, use clearer variable
names.

Change-Id: Ic3bc6e36506973b19a9b1bcecbc1a5080faed2ec
This commit is contained in:
Daimona Eaytoy 2018-11-22 15:52:14 +01:00
parent 8cdd899c16
commit 1f2b7474ed
2 changed files with 22 additions and 26 deletions

View file

@ -264,7 +264,7 @@
"default": 0.05
},
"_merge_strategy": "array_plus",
"description": "Disable filters if they match more than X edits, constituting more than Y% of the last Z edits, if they have been changed in the last S seconds."
"description": "Disable a filter if it matches more than X actions, constituting more than Y% (e.g. 0.05 = 5%) of the last actions, and the filter has been modified in the last S seconds. X is AbuseFilterEmergencyDisableCount, Y is AbuseFilterEmergencyDisableThreshold and S is AbuseFilterEmergencyDisableAge."
},
"AbuseFilterEmergencyDisableCount": {
"value": {

View file

@ -1444,9 +1444,7 @@ class AbuseFilter {
$vars->setVar( 'global_log_ids', $global_log_ids );
$vars->setVar( 'local_log_ids', $local_log_ids );
// Check for emergency disabling.
$total = $stash->get( self::filterUsedKey( $group ) );
self::checkEmergencyDisable( $group, $logged_local_filters, $total );
self::checkEmergencyDisable( $group, $logged_local_filters );
}
/**
@ -2011,25 +2009,24 @@ class AbuseFilter {
}
/**
* Determine whether a filter must be throttled, i.e. its potentially dangerous
* actions must be disabled.
*
* @param string $group The filter's group (as defined in $wgAbuseFilterValidGroups)
* @param string[] $filters
* @param int $total
* @param string[] $filters The filters to check
*/
public static function checkEmergencyDisable( $group, $filters, $total ) {
private static function checkEmergencyDisable( $group, $filters ) {
global $wgAbuseFilterEmergencyDisableThreshold, $wgAbuseFilterEmergencyDisableCount,
$wgAbuseFilterEmergencyDisableAge;
$stash = ObjectCache::getMainStashInstance();
foreach ( $filters as $filter ) {
// Determine emergency disable values for this action
$emergencyDisableThreshold =
self::getEmergencyValue( $wgAbuseFilterEmergencyDisableThreshold, $group );
$filterEmergencyDisableCount =
self::getEmergencyValue( $wgAbuseFilterEmergencyDisableCount, $group );
$emergencyDisableAge =
self::getEmergencyValue( $wgAbuseFilterEmergencyDisableAge, $group );
$totalActions = $stash->get( self::filterUsedKey( $group ) );
foreach ( $filters as $filter ) {
$threshold = self::getEmergencyValue( $wgAbuseFilterEmergencyDisableThreshold, $group );
$hitCountLimit = self::getEmergencyValue( $wgAbuseFilterEmergencyDisableCount, $group );
$maxAge = self::getEmergencyValue( $wgAbuseFilterEmergencyDisableAge, $group );
// Increment counter
$matchCount = $stash->get( self::filterMatchesKey( $filter ) );
// Handle missing keys...
@ -2040,23 +2037,22 @@ class AbuseFilter {
}
$matchCount++;
// Figure out if the filter is subject to being deleted.
$filter_age = wfTimestamp( TS_UNIX, self::getFilter( $filter )->af_timestamp );
$throttle_exempt_time = $filter_age + $emergencyDisableAge;
// Figure out if the filter is subject to being throttled.
$filterAge = wfTimestamp( TS_UNIX, self::getFilter( $filter )->af_timestamp );
$exemptTime = $filterAge + $maxAge;
if ( $total && $throttle_exempt_time > time()
&& $matchCount > $filterEmergencyDisableCount
&& ( $matchCount / $total ) > $emergencyDisableThreshold
if ( $totalActions && $exemptTime > time() && $matchCount > $hitCountLimit &&
( $matchCount / $totalActions ) > $threshold
) {
// More than $wgAbuseFilterEmergencyDisableCount matches,
// constituting more than $emergencyDisableThreshold
// (a fraction) of last few edits. Disable it.
// More than $wgAbuseFilterEmergencyDisableCount matches, constituting more than
// $threshold (a fraction) of last few edits. Disable it.
DeferredUpdates::addUpdate(
new AutoCommitUpdate(
wfGetDB( DB_MASTER ),
__METHOD__,
function ( IDatabase $dbw, $fname ) use ( $filter ) {
$dbw->update( 'abuse_filter',
$dbw->update(
'abuse_filter',
[ 'af_throttled' => 1 ],
[ 'af_id' => $filter ],
$fname