fix: Fixes pooled timer detach (#1115)
- [X] Fixes timers not returning on detach
This commit is contained in:
parent
9c374861d9
commit
a4168dc219
7 changed files with 804 additions and 816 deletions
|
|
@ -20,211 +20,207 @@ using System.Collections.Generic;
|
|||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server
|
||||
namespace Server;
|
||||
|
||||
public partial class Timer
|
||||
{
|
||||
public partial class Timer
|
||||
private static string FormatDelegate(Delegate callback) =>
|
||||
callback == null ? "null" : $"{callback.Method.DeclaringType?.FullName ?? ""}.{callback.Method.Name}";
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static DelayCallTimer DelayCall(Action callback) => DelayCall(TimeSpan.Zero, TimeSpan.Zero, 1, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static DelayCallTimer DelayCall(TimeSpan delay, Action callback) => DelayCall(delay, TimeSpan.Zero, 1, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static DelayCallTimer DelayCall(TimeSpan delay, TimeSpan interval, Action callback) =>
|
||||
DelayCall(delay, interval, 0, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static DelayCallTimer DelayCall(TimeSpan interval, int count, Action callback) =>
|
||||
DelayCall(TimeSpan.Zero, interval, count, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static DelayCallTimer DelayCall(TimeSpan delay, TimeSpan interval, int count, Action callback)
|
||||
{
|
||||
private static string FormatDelegate(Delegate callback) =>
|
||||
callback == null ? "null" : $"{callback.Method.DeclaringType?.FullName ?? ""}.{callback.Method.Name}";
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static DelayCallTimer DelayCall(Action callback) => DelayCall(TimeSpan.Zero, TimeSpan.Zero, 1, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static DelayCallTimer DelayCall(TimeSpan delay, Action callback) => DelayCall(delay, TimeSpan.Zero, 1, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static DelayCallTimer DelayCall(TimeSpan delay, TimeSpan interval, Action callback) =>
|
||||
DelayCall(delay, interval, 0, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static DelayCallTimer DelayCall(TimeSpan interval, int count, Action callback) =>
|
||||
DelayCall(TimeSpan.Zero, interval, count, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static DelayCallTimer DelayCall(TimeSpan delay, TimeSpan interval, int count, Action callback)
|
||||
{
|
||||
DelayCallTimer t = new DelayCallTimer(delay, interval, count, callback);
|
||||
t.Start();
|
||||
DelayCallTimer t = new DelayCallTimer(delay, interval, count, callback);
|
||||
t.Start();
|
||||
#if DEBUG_TIMERS
|
||||
t._allowFinalization = true;
|
||||
t._allowFinalization = true;
|
||||
#endif
|
||||
|
||||
return t;
|
||||
return t;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(Action callback) => StartTimer(TimeSpan.Zero, TimeSpan.Zero, 1, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan delay, Action callback) => StartTimer(delay, TimeSpan.Zero, 1, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan delay, TimeSpan interval, Action callback) =>
|
||||
StartTimer(delay, interval, 0, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan interval, int count, Action callback) =>
|
||||
StartTimer(TimeSpan.Zero, interval, count, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan delay, TimeSpan interval, int count, Action callback)
|
||||
{
|
||||
DelayCallTimer t = DelayCallTimer.GetTimer(delay, interval, count, callback);
|
||||
t._returnOnDetach = true;
|
||||
t.Start();
|
||||
|
||||
#if DEBUG_TIMERS
|
||||
DelayCallTimer._stackTraces[t.GetHashCode()] = $"{callback.Method.Name}\n{new StackTrace()}";
|
||||
#endif
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(Action callback, out TimerExecutionToken token) =>
|
||||
StartTimer(TimeSpan.Zero, TimeSpan.Zero, 1, callback, out token);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan delay, Action callback, out TimerExecutionToken token) =>
|
||||
StartTimer(delay, TimeSpan.Zero, 1, callback, out token);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan delay, TimeSpan interval, Action callback, out TimerExecutionToken token) =>
|
||||
StartTimer(delay, interval, 0, callback, out token);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan interval, int count, Action callback, out TimerExecutionToken token) =>
|
||||
StartTimer(TimeSpan.Zero, interval, count, callback, out token);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan delay, TimeSpan interval, int count, Action callback, out TimerExecutionToken token)
|
||||
{
|
||||
DelayCallTimer t = DelayCallTimer.GetTimer(delay, interval, count, callback);
|
||||
t.Start();
|
||||
|
||||
#if DEBUG_TIMERS
|
||||
DelayCallTimer._stackTraces[t.GetHashCode()] = $"{callback.Method.Name}\n{new StackTrace()}";
|
||||
#endif
|
||||
token = new TimerExecutionToken(t);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static DelayCallTimer Pause(TimeSpan ms) => new(ms);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static DelayCallTimer Pause(int ms) => Pause(TimeSpan.FromMilliseconds(ms));
|
||||
|
||||
public sealed class DelayCallTimer : Timer, INotifyCompletion
|
||||
{
|
||||
internal bool _returnOnDetach;
|
||||
#if DEBUG_TIMERS
|
||||
internal bool _allowFinalization;
|
||||
#endif
|
||||
private Action _continuation;
|
||||
private bool _complete;
|
||||
|
||||
internal DelayCallTimer(TimeSpan delay, TimeSpan interval, int count, Action callback) : base(
|
||||
delay,
|
||||
interval,
|
||||
count
|
||||
) => _continuation = callback;
|
||||
|
||||
internal DelayCallTimer(TimeSpan delay) : base(delay)
|
||||
{
|
||||
#if DEBUG_TIMERS
|
||||
_allowFinalization = true;
|
||||
#endif
|
||||
Start();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(Action callback) => StartTimer(TimeSpan.Zero, TimeSpan.Zero, 1, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan delay, Action callback) => StartTimer(delay, TimeSpan.Zero, 1, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan delay, TimeSpan interval, Action callback) =>
|
||||
StartTimer(delay, interval, 0, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan interval, int count, Action callback) =>
|
||||
StartTimer(TimeSpan.Zero, interval, count, callback);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan delay, TimeSpan interval, int count, Action callback)
|
||||
protected override void OnTick()
|
||||
{
|
||||
DelayCallTimer t = DelayCallTimer.GetTimer(delay, interval, count, callback);
|
||||
t._returnOnDetach = true;
|
||||
t.Start();
|
||||
|
||||
#if DEBUG_TIMERS
|
||||
DelayCallTimer._stackTraces[t.GetHashCode()] = new StackTrace().ToString();
|
||||
#endif
|
||||
_complete = true;
|
||||
_continuation?.Invoke();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(Action callback, out TimerExecutionToken token) =>
|
||||
StartTimer(TimeSpan.Zero, TimeSpan.Zero, 1, callback, out token);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan delay, Action callback, out TimerExecutionToken token) =>
|
||||
StartTimer(delay, TimeSpan.Zero, 1, callback, out token);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan delay, TimeSpan interval, Action callback, out TimerExecutionToken token) =>
|
||||
StartTimer(delay, interval, 0, callback, out token);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan interval, int count, Action callback, out TimerExecutionToken token) =>
|
||||
StartTimer(TimeSpan.Zero, interval, count, callback, out token);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void StartTimer(TimeSpan delay, TimeSpan interval, int count, Action callback, out TimerExecutionToken token)
|
||||
internal override void OnDetach()
|
||||
{
|
||||
DelayCallTimer t = DelayCallTimer.GetTimer(delay, interval, count, callback);
|
||||
t.Start();
|
||||
|
||||
#if DEBUG_TIMERS
|
||||
DelayCallTimer._stackTraces[t.GetHashCode()] = new StackTrace().ToString();
|
||||
#endif
|
||||
token = new TimerExecutionToken(t);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static DelayCallTimer Pause(TimeSpan ms) => new(ms);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static DelayCallTimer Pause(int ms) => Pause(TimeSpan.FromMilliseconds(ms));
|
||||
|
||||
public sealed class DelayCallTimer : Timer, INotifyCompletion
|
||||
{
|
||||
internal bool _returnOnDetach;
|
||||
#if DEBUG_TIMERS
|
||||
internal bool _allowFinalization;
|
||||
#endif
|
||||
private Action _continuation;
|
||||
private bool _complete;
|
||||
|
||||
internal DelayCallTimer(TimeSpan delay, TimeSpan interval, int count, Action callback) : base(
|
||||
delay,
|
||||
interval,
|
||||
count
|
||||
) =>
|
||||
_continuation = callback;
|
||||
|
||||
internal DelayCallTimer(TimeSpan delay) : base(delay)
|
||||
if (Running)
|
||||
{
|
||||
#if DEBUG_TIMERS
|
||||
_allowFinalization = true;
|
||||
#endif
|
||||
Start();
|
||||
logger.Error("Timer is returned while still running!\n{StackTrace}", new StackTrace());
|
||||
return;
|
||||
}
|
||||
|
||||
protected override void OnTick()
|
||||
if (_returnOnDetach)
|
||||
{
|
||||
_complete = true;
|
||||
_continuation?.Invoke();
|
||||
}
|
||||
|
||||
internal override void OnDetach()
|
||||
{
|
||||
if (Running)
|
||||
#if DEBUG_TIMERS
|
||||
_stackTraces.Remove(GetHashCode());
|
||||
#endif
|
||||
|
||||
if (_poolCount >= _poolCapacity)
|
||||
{
|
||||
logger.Error("Timer is returned while still running!\n{StackTrace}", new StackTrace());
|
||||
#if DEBUG_TIMERS
|
||||
logger.Warning("DelayCallTimer pool reached maximum of {Capacity} timers", _poolCapacity);
|
||||
_allowFinalization = true;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
if (_returnOnDetach)
|
||||
{
|
||||
// Increment the version so if this is called from OnTick() and another timer is started, we don't have a problem
|
||||
// Version++;
|
||||
|
||||
#if DEBUG_TIMERS
|
||||
_stackTraces.Remove(GetHashCode());
|
||||
#endif
|
||||
|
||||
if (_poolCount >= _poolCapacity)
|
||||
{
|
||||
#if DEBUG_TIMERS
|
||||
logger.Warning("DelayCallTimer pool reached maximum of {Capacity} timers", _poolCapacity);
|
||||
_allowFinalization = true;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
_continuation = null;
|
||||
_returnOnDetach = false;
|
||||
ReturnToPool(1, this, this);
|
||||
}
|
||||
_continuation = null;
|
||||
_returnOnDetach = false;
|
||||
ReturnToPool(1, this, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static DelayCallTimer GetTimer(TimeSpan delay, TimeSpan interval, int count, Action callback)
|
||||
public static DelayCallTimer GetTimer(TimeSpan delay, TimeSpan interval, int count, Action callback)
|
||||
{
|
||||
var timer = GetFromPool();
|
||||
if (timer != null)
|
||||
{
|
||||
var timer = GetFromPool();
|
||||
if (timer != null)
|
||||
{
|
||||
#if DEBUG_TIMERS
|
||||
logger.Information("Getting from pool: ({Count} / {Capacity})", _poolCount, _poolCapacity);
|
||||
logger.Information("Getting from pool: ({Count} / {Capacity})", _poolCount, _poolCapacity);
|
||||
#endif
|
||||
|
||||
timer.Init(delay, interval, count);
|
||||
timer._continuation = callback;
|
||||
timer._returnOnDetach = false;
|
||||
timer.Init(delay, interval, count);
|
||||
timer._continuation = callback;
|
||||
timer._returnOnDetach = false;
|
||||
#if DEBUG_TIMERS
|
||||
timer._allowFinalization = false;
|
||||
timer._allowFinalization = false;
|
||||
#endif
|
||||
|
||||
return timer;
|
||||
}
|
||||
|
||||
_timerPoolDepletionAmount++;
|
||||
|
||||
#if DEBUG_TIMERS
|
||||
logger.Warning("Timer pool depleted and timer was allocated.\n{StackTrace}", new StackTrace());
|
||||
#endif
|
||||
return new DelayCallTimer(delay, interval, count, callback);
|
||||
return timer;
|
||||
}
|
||||
|
||||
public override string ToString() => $"DelayCallTimer[{FormatDelegate(_continuation)}]";
|
||||
_timerPoolDepletionAmount++;
|
||||
|
||||
#if DEBUG_TIMERS
|
||||
internal static Dictionary<int, string> _stackTraces = new();
|
||||
logger.Warning("Timer pool depleted and timer was allocated.\n{StackTrace}", new StackTrace());
|
||||
#endif
|
||||
return new DelayCallTimer(delay, interval, count, callback);
|
||||
}
|
||||
|
||||
~DelayCallTimer()
|
||||
public override string ToString() => $"DelayCallTimer[{FormatDelegate(_continuation)}]";
|
||||
|
||||
#if DEBUG_TIMERS
|
||||
internal static Dictionary<int, string> _stackTraces = new();
|
||||
|
||||
~DelayCallTimer()
|
||||
{
|
||||
if (!_allowFinalization)
|
||||
{
|
||||
if (!_allowFinalization)
|
||||
{
|
||||
logger.Warning("Pooled timer was not returned to the pool.\n{StackTrace}", _stackTraces[GetHashCode()]);
|
||||
}
|
||||
logger.Warning("Pooled timer was not returned to the pool.\n{StackTrace}", _stackTraces[GetHashCode()]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
public DelayCallTimer GetAwaiter() => this;
|
||||
public DelayCallTimer GetAwaiter() => this;
|
||||
|
||||
public bool IsCompleted => _complete;
|
||||
public bool IsCompleted => _complete;
|
||||
|
||||
public void OnCompleted(Action continuation) => _continuation = continuation;
|
||||
public void OnCompleted(Action continuation) => _continuation = continuation;
|
||||
|
||||
public void GetResult()
|
||||
{
|
||||
}
|
||||
public void GetResult()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue