Rate-limit AI updates (#2120)
* Rate-limit AI updates Stop the public servers from getting hammered too hard. * Add logger for the AI limit as well. * Also support AI maxupdates <= 0 Juuusssttt in case Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using Content.Server.GameObjects.Components.Movement;
|
using Content.Server.GameObjects.Components.Movement;
|
||||||
using Content.Shared.GameObjects.Components.Movement;
|
using Content.Shared.GameObjects.Components.Movement;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
@@ -9,9 +10,11 @@ using Robust.Server.Interfaces.Console;
|
|||||||
using Robust.Server.Interfaces.Player;
|
using Robust.Server.Interfaces.Player;
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
using Robust.Shared.GameObjects.Systems;
|
using Robust.Shared.GameObjects.Systems;
|
||||||
|
using Robust.Shared.Interfaces.Configuration;
|
||||||
using Robust.Shared.Interfaces.GameObjects;
|
using Robust.Shared.Interfaces.GameObjects;
|
||||||
using Robust.Shared.Interfaces.Reflection;
|
using Robust.Shared.Interfaces.Reflection;
|
||||||
using Robust.Shared.IoC;
|
using Robust.Shared.IoC;
|
||||||
|
using Robust.Shared.Log;
|
||||||
using Robust.Shared.Utility;
|
using Robust.Shared.Utility;
|
||||||
|
|
||||||
namespace Content.Server.GameObjects.EntitySystems.AI
|
namespace Content.Server.GameObjects.EntitySystems.AI
|
||||||
@@ -19,6 +22,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI
|
|||||||
[UsedImplicitly]
|
[UsedImplicitly]
|
||||||
internal class AiSystem : EntitySystem
|
internal class AiSystem : EntitySystem
|
||||||
{
|
{
|
||||||
|
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
|
||||||
[Dependency] private readonly IDynamicTypeFactory _typeFactory = default!;
|
[Dependency] private readonly IDynamicTypeFactory _typeFactory = default!;
|
||||||
[Dependency] private readonly IReflectionManager _reflectionManager = default!;
|
[Dependency] private readonly IReflectionManager _reflectionManager = default!;
|
||||||
|
|
||||||
@@ -38,6 +42,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI
|
|||||||
public override void Initialize()
|
public override void Initialize()
|
||||||
{
|
{
|
||||||
base.Initialize();
|
base.Initialize();
|
||||||
|
_configurationManager.RegisterCVar("ai.maxupdates", 64);
|
||||||
SubscribeLocalEvent<SleepAiMessage>(HandleAiSleep);
|
SubscribeLocalEvent<SleepAiMessage>(HandleAiSleep);
|
||||||
|
|
||||||
var processors = _reflectionManager.GetAllChildren<AiLogicProcessor>();
|
var processors = _reflectionManager.GetAllChildren<AiLogicProcessor>();
|
||||||
@@ -53,24 +58,44 @@ namespace Content.Server.GameObjects.EntitySystems.AI
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void Update(float frameTime)
|
public override void Update(float frameTime)
|
||||||
{
|
{
|
||||||
|
var cvarMaxUpdates = _configurationManager.GetCVar<int>("ai.maxupdates");
|
||||||
|
if (cvarMaxUpdates <= 0)
|
||||||
|
return;
|
||||||
|
|
||||||
foreach (var message in _queuedSleepMessages)
|
foreach (var message in _queuedSleepMessages)
|
||||||
{
|
{
|
||||||
switch (message.Sleep)
|
switch (message.Sleep)
|
||||||
{
|
{
|
||||||
case true:
|
case true:
|
||||||
|
if (_awakeAi.Count == cvarMaxUpdates && _awakeAi.Contains(message.Processor))
|
||||||
|
{
|
||||||
|
Logger.Warning($"Under AI limit again: {_awakeAi.Count - 1} / {cvarMaxUpdates}");
|
||||||
|
}
|
||||||
_awakeAi.Remove(message.Processor);
|
_awakeAi.Remove(message.Processor);
|
||||||
break;
|
break;
|
||||||
case false:
|
case false:
|
||||||
_awakeAi.Add(message.Processor);
|
_awakeAi.Add(message.Processor);
|
||||||
|
|
||||||
|
if (_awakeAi.Count > cvarMaxUpdates)
|
||||||
|
{
|
||||||
|
Logger.Warning($"AI limit exceeded: {_awakeAi.Count} / {cvarMaxUpdates}");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_queuedSleepMessages.Clear();
|
_queuedSleepMessages.Clear();
|
||||||
var toRemove = new List<AiLogicProcessor>();
|
var toRemove = new List<AiLogicProcessor>();
|
||||||
|
var maxUpdates = Math.Min(_awakeAi.Count, cvarMaxUpdates);
|
||||||
|
var count = 0;
|
||||||
|
|
||||||
foreach (var processor in _awakeAi)
|
foreach (var processor in _awakeAi)
|
||||||
{
|
{
|
||||||
|
if (count >= maxUpdates)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (processor.SelfEntity.Deleted)
|
if (processor.SelfEntity.Deleted)
|
||||||
{
|
{
|
||||||
toRemove.Add(processor);
|
toRemove.Add(processor);
|
||||||
@@ -78,6 +103,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI
|
|||||||
}
|
}
|
||||||
|
|
||||||
processor.Update(frameTime);
|
processor.Update(frameTime);
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var processor in toRemove)
|
foreach (var processor in toRemove)
|
||||||
|
|||||||
Reference in New Issue
Block a user