Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void Ping(string pluginVersion)
string? workshopID = _matchService.GetWorkshopID();

string endpoint =
$"{_environmentService.GetApiUrl()}/game-server-node/ping/{serverId}?map={(string.IsNullOrEmpty(workshopID) ? Server.MapName : workshopID)}&pluginVersion={pluginVersion}&pluginRuntime=counterstrikesharp";
$"{_environmentService.GetApiUrl()}/game-server-node/ping/{serverId}?map={(string.IsNullOrEmpty(workshopID) ? Server.MapName : workshopID)}&pluginVersion={pluginVersion}&pluginRuntime=counterstrikesharp&hibernating={(HibernationUtility.IsHibernating ? "true" : "false")}";

if (_steamRelay)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public static class HibernationUtility
{
private static volatile bool _hibernating;

public static bool IsHibernating => _hibernating;

public static void SetHibernating(bool hibernating)
{
_hibernating = hibernating;
Expand Down
2 changes: 1 addition & 1 deletion apps/swiftly/src/FiveStack.Services/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void Ping(string pluginVersion)
: workshopID;

string endpoint =
$"{_environmentService.GetApiUrl()}/game-server-node/ping/{serverId}?map={mapName}&pluginVersion={pluginVersion}&pluginRuntime=swiftlys2";
$"{_environmentService.GetApiUrl()}/game-server-node/ping/{serverId}?map={mapName}&pluginVersion={pluginVersion}&pluginRuntime=swiftlys2&hibernating={(HibernationUtility.IsHibernating ? "true" : "false")}";

if (_steamRelay)
{
Expand Down
42 changes: 41 additions & 1 deletion apps/swiftly/src/FiveStack.Utilities/HibernationUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ namespace FiveStack.Utilities;
public static class HibernationUtility
{
private const long TickingWindowMs = 250;
private const long HibernatingAfterMs = 5000;

private static ISwiftlyCore _core = null!;
private static EventDelegates.OnTick? _tickHandler;
private static EventDelegates.OnWorldUpdate? _worldUpdateHandler;
private static long _lastTickMs;
private static long _updatingWithoutTicksSinceMs;

public static void Initialize(ISwiftlyCore core)
{
_core = core;
_tickHandler = () => Volatile.Write(ref _lastTickMs, Environment.TickCount64);
_core.Event.OnTick += _tickHandler;

_worldUpdateHandler = OnWorldUpdate;
_core.Event.OnWorldUpdate += _worldUpdateHandler;
}

public static void Shutdown()
Expand All @@ -30,11 +36,45 @@ public static void Shutdown()
_core.Event.OnTick -= _tickHandler;
_tickHandler = null;
}

if (_worldUpdateHandler != null)
{
_core.Event.OnWorldUpdate -= _worldUpdateHandler;
_worldUpdateHandler = null;
}
}

private static bool IsTicking =>
Environment.TickCount64 - Volatile.Read(ref _lastTickMs) < TickingWindowMs;

// A map load stops ticks and world updates together and plugin load starts
// with neither seen, so only world updates that keep arriving with no tick
// between them say the server is hibernating.
private static void OnWorldUpdate()
{
if (IsTicking)
{
Volatile.Write(ref _updatingWithoutTicksSinceMs, 0);
}
else if (Volatile.Read(ref _updatingWithoutTicksSinceMs) == 0)
{
Volatile.Write(ref _updatingWithoutTicksSinceMs, Environment.TickCount64);
}
}

public static bool IsHibernating
{
get
{
long since = Volatile.Read(ref _updatingWithoutTicksSinceMs);

return since != 0 && Environment.TickCount64 - since > HibernatingAfterMs;
}
}

public static void NextTick(Action callback)
{
if (Environment.TickCount64 - Volatile.Read(ref _lastTickMs) < TickingWindowMs)
if (IsTicking)
{
_core.Scheduler.NextTick(callback);
return;
Expand Down
24 changes: 19 additions & 5 deletions apps/utility-sw/src/UtilityPracticePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ public override void Load(bool hotReload)
// One repeating job for the whole plugin, not one per player. These
// deliberately do not get StopOnMapChange: the plugin is not reloaded
// on a map change, so a timer that stopped there would never come back.
_secondTimer = Core.Scheduler.RepeatBySeconds(1, Surviving(OnSecond));
_refillTimer = Core.Scheduler.RepeatBySeconds(0.1f, Surviving(OnFastTick));
_secondTimer = Core.Scheduler.RepeatBySeconds(1, Surviving(1, OnSecond));
_refillTimer = Core.Scheduler.RepeatBySeconds(0.1f, Surviving(0.1f, OnFastTick));

// Only on a hot reload. A cold boot has no engine globals yet -- asking
// for the map here is what stopped the plugin loading at all -- and the
Expand Down Expand Up @@ -302,17 +302,31 @@ public override void Load(bool hotReload)

private bool _tornDown;

// SwiftlyS2 never reschedules a repeating timer whose callback throws, and
// still runs a timer's first pass if it was cancelled before then.
private Action Surviving(Action callback)
// SwiftlyS2 never reschedules a repeating timer whose callback throws,
// still runs a timer's first pass if it was cancelled before then, and
// after a spell without ticks replays every run it missed, one a tick. The
// gap check turns that replay into at most twice the usual rate.
private Action Surviving(float interval, Action callback)
{
long lastRunMs = 0;
long minimumGapMs = (long)(interval * 500);

return () =>
{
if (_tornDown)
{
return;
}

long nowMs = Environment.TickCount64;

if (nowMs - lastRunMs < minimumGapMs)
{
return;
}

lastRunMs = nowMs;

try
{
callback();
Expand Down
Loading