ModernUO/Projects/UOContent/Network/Blocklist/PromotedGuard.cs
Kamron Batman a2fd2441ae
fix: Removes unnecessary dictionary removal guards
Dictionary<K,V>.Remove and HashSet<T>.Remove do not bump the collection
version, so removing during a foreach is safe and the collect-then-remove
queues guarding those loops are dead weight. This drops them, including
via the Keys and Values enumerators.

Deletion of a Mobile is left deferred: removing from World.Mobiles while
enumerating it is fine, but Delete() runs OnDelete/OnAfterDelete, the
OnParentDeleted cascade over the pack, and region and guild callbacks.
Anything in there that constructs a Mobile is an Add into the dictionary
being enumerated, which does invalidate it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-08 11:43:10 -07:00

51 lines
1.8 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2026 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: PromotedGuard.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Collections.Generic;
namespace Server.Network.Bans;
/// <summary>Suppresses re-reporting the same IP within a TTL. Accept-path thread only.</summary>
public sealed class PromotedGuard
{
private readonly Dictionary<UInt128, long> _expiry = [];
public bool TryMark(UInt128 ip, long nowTicks, long ttlMs)
{
if (_expiry.TryGetValue(ip, out var exp) && exp - nowTicks > 0)
{
return false;
}
_expiry[ip] = nowTicks + ttlMs;
return true;
}
public void Sweep(long nowTicks)
{
if (_expiry.Count == 0)
{
return;
}
foreach (var (ip, exp) in _expiry)
{
if (exp - nowTicks <= 0)
{
_expiry.Remove(ip);
}
}
}
}