Cut 9 Soul Pass #2 fix: DirectoryReporter IAsyncDisposable, lease lock, 5s DELETE timeout, and wave dirty check
This commit is contained in:
parent
fe1eec54a8
commit
75e4095b23
3 changed files with 97 additions and 27 deletions
|
|
@ -79,7 +79,7 @@ namespace RustToDust.Net
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class DirectoryReporter : IDisposable
|
public sealed class DirectoryReporter : IDisposable, IAsyncDisposable
|
||||||
{
|
{
|
||||||
private sealed record HeartbeatBody(
|
private sealed record HeartbeatBody(
|
||||||
Guid? ServerId,
|
Guid? ServerId,
|
||||||
|
|
@ -115,6 +115,7 @@ namespace RustToDust.Net
|
||||||
private readonly bool _ownsHttp;
|
private readonly bool _ownsHttp;
|
||||||
private readonly CancellationTokenSource _cts = new();
|
private readonly CancellationTokenSource _cts = new();
|
||||||
private readonly SemaphoreSlim _wakeSignal = new(0, 1);
|
private readonly SemaphoreSlim _wakeSignal = new(0, 1);
|
||||||
|
private readonly object _leaseGate = new();
|
||||||
private readonly Task _loopTask;
|
private readonly Task _loopTask;
|
||||||
|
|
||||||
private Guid? _serverId;
|
private Guid? _serverId;
|
||||||
|
|
@ -141,14 +142,24 @@ namespace RustToDust.Net
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_http = new HttpClient { Timeout = TimeSpan.FromSeconds(4) };
|
_http = new HttpClient { Timeout = TimeSpan.FromSeconds(6) };
|
||||||
_ownsHttp = true;
|
_ownsHttp = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
_loopTask = Task.Run(RunLoopAsync);
|
_loopTask = Task.Run(RunLoopAsync);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Guid? CurrentServerId => _serverId;
|
public Guid? CurrentServerId
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
lock (_leaseGate)
|
||||||
|
{
|
||||||
|
return _serverId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string? LastError { get; private set; }
|
public string? LastError { get; private set; }
|
||||||
|
|
||||||
public void NotifyChanged()
|
public void NotifyChanged()
|
||||||
|
|
@ -199,9 +210,17 @@ namespace RustToDust.Net
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var snap = _snapshotProvider();
|
var snap = _snapshotProvider();
|
||||||
|
Guid? curServerId;
|
||||||
|
string? curLeaseToken;
|
||||||
|
lock (_leaseGate)
|
||||||
|
{
|
||||||
|
curServerId = _serverId;
|
||||||
|
curLeaseToken = _leaseToken;
|
||||||
|
}
|
||||||
|
|
||||||
var body = new HeartbeatBody(
|
var body = new HeartbeatBody(
|
||||||
_serverId,
|
curServerId,
|
||||||
_leaseToken,
|
curLeaseToken,
|
||||||
snap.Name,
|
snap.Name,
|
||||||
snap.PublicHost,
|
snap.PublicHost,
|
||||||
snap.UdpPort,
|
snap.UdpPort,
|
||||||
|
|
@ -217,8 +236,11 @@ namespace RustToDust.Net
|
||||||
using var resp = await _http.PostAsJsonAsync($"{_baseUrl}/api/v1/servers/heartbeat", body, ct).ConfigureAwait(false);
|
using var resp = await _http.PostAsJsonAsync($"{_baseUrl}/api/v1/servers/heartbeat", body, ct).ConfigureAwait(false);
|
||||||
if (resp.StatusCode == HttpStatusCode.Conflict || resp.StatusCode == HttpStatusCode.NotFound)
|
if (resp.StatusCode == HttpStatusCode.Conflict || resp.StatusCode == HttpStatusCode.NotFound)
|
||||||
{
|
{
|
||||||
_serverId = null;
|
lock (_leaseGate)
|
||||||
_leaseToken = null;
|
{
|
||||||
|
_serverId = null;
|
||||||
|
_leaseToken = null;
|
||||||
|
}
|
||||||
LastError = $"Directory returned {(int)resp.StatusCode}; resetting lease.";
|
LastError = $"Directory returned {(int)resp.StatusCode}; resetting lease.";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -232,8 +254,11 @@ namespace RustToDust.Net
|
||||||
var ack = await resp.Content.ReadFromJsonAsync<HeartbeatAck>(JsonOptions, ct).ConfigureAwait(false);
|
var ack = await resp.Content.ReadFromJsonAsync<HeartbeatAck>(JsonOptions, ct).ConfigureAwait(false);
|
||||||
if (ack is not null && ack.ServerId != Guid.Empty && !string.IsNullOrEmpty(ack.LeaseToken))
|
if (ack is not null && ack.ServerId != Guid.Empty && !string.IsNullOrEmpty(ack.LeaseToken))
|
||||||
{
|
{
|
||||||
_serverId = ack.ServerId;
|
lock (_leaseGate)
|
||||||
_leaseToken = ack.LeaseToken;
|
{
|
||||||
|
_serverId = ack.ServerId;
|
||||||
|
_leaseToken = ack.LeaseToken;
|
||||||
|
}
|
||||||
LastError = null;
|
LastError = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -246,33 +271,30 @@ namespace RustToDust.Net
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
private (Guid? ServerId, string? LeaseToken) ReadLeaseSnapshot()
|
||||||
{
|
{
|
||||||
if (Interlocked.Exchange(ref _disposed, 1) != 0) return;
|
lock (_leaseGate)
|
||||||
|
{
|
||||||
|
return (_serverId, _leaseToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_cts.Cancel();
|
private async Task SendDeleteOnceAsync(Guid serverId, string leaseToken)
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_loopTask.Wait(TimeSpan.FromMilliseconds(500));
|
using var deleteCts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
||||||
|
using var req = new HttpRequestMessage(HttpMethod.Delete, $"{_baseUrl}/api/v1/servers/{serverId}");
|
||||||
|
req.Headers.TryAddWithoutValidation("X-Lease-Token", leaseToken);
|
||||||
|
using var _ = await _http.SendAsync(req, deleteCts.Token).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (_serverId is { } serverId && !string.IsNullOrEmpty(_leaseToken))
|
private void DisposeResources()
|
||||||
{
|
{
|
||||||
try
|
|
||||||
{
|
|
||||||
using var deleteCts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1500));
|
|
||||||
using var req = new HttpRequestMessage(HttpMethod.Delete, $"{_baseUrl}/api/v1/servers/{serverId}");
|
|
||||||
req.Headers.TryAddWithoutValidation("X-Lease-Token", _leaseToken);
|
|
||||||
_http.SendAsync(req, deleteCts.Token).GetAwaiter().GetResult();
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_cts.Dispose();
|
_cts.Dispose();
|
||||||
_wakeSignal.Dispose();
|
_wakeSignal.Dispose();
|
||||||
if (_ownsHttp)
|
if (_ownsHttp)
|
||||||
|
|
@ -280,5 +302,49 @@ namespace RustToDust.Net
|
||||||
_http.Dispose();
|
_http.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
if (Interlocked.Exchange(ref _disposed, 1) != 0) return;
|
||||||
|
|
||||||
|
_cts.Cancel();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_loopTask.Wait(TimeSpan.FromSeconds(5));
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
var (serverId, leaseToken) = ReadLeaseSnapshot();
|
||||||
|
if (serverId is { } sid && !string.IsNullOrEmpty(leaseToken))
|
||||||
|
{
|
||||||
|
SendDeleteOnceAsync(sid, leaseToken).GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
DisposeResources();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask DisposeAsync()
|
||||||
|
{
|
||||||
|
if (Interlocked.Exchange(ref _disposed, 1) != 0) return;
|
||||||
|
|
||||||
|
_cts.Cancel();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _loopTask.WaitAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
var (serverId, leaseToken) = ReadLeaseSnapshot();
|
||||||
|
if (serverId is { } sid && !string.IsNullOrEmpty(leaseToken))
|
||||||
|
{
|
||||||
|
await SendDeleteOnceAsync(sid, leaseToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
DisposeResources();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -136,6 +136,7 @@ namespace RustToDust.Server
|
||||||
MatchPhase lastReportedPhase = host.Session.State.Phase;
|
MatchPhase lastReportedPhase = host.Session.State.Phase;
|
||||||
ArenaMapKind lastReportedMap = host.Session.State.MapKind;
|
ArenaMapKind lastReportedMap = host.Session.State.MapKind;
|
||||||
int lastReportedSeed = host.Session.State.Seed;
|
int lastReportedSeed = host.Session.State.Seed;
|
||||||
|
int lastReportedWave = host.Session.State.OrbitalDrop.CurrentWaveIndex;
|
||||||
|
|
||||||
while (!cts.IsCancellationRequested && sw.Elapsed.TotalSeconds < endSec)
|
while (!cts.IsCancellationRequested && sw.Elapsed.TotalSeconds < endSec)
|
||||||
{
|
{
|
||||||
|
|
@ -147,15 +148,18 @@ namespace RustToDust.Server
|
||||||
{
|
{
|
||||||
var state = host.Session.State;
|
var state = host.Session.State;
|
||||||
int curHumans = host.ConnectedHumanCount;
|
int curHumans = host.ConnectedHumanCount;
|
||||||
|
int curWave = state.OrbitalDrop.CurrentWaveIndex;
|
||||||
if (curHumans != lastReportedHumans ||
|
if (curHumans != lastReportedHumans ||
|
||||||
state.Phase != lastReportedPhase ||
|
state.Phase != lastReportedPhase ||
|
||||||
state.MapKind != lastReportedMap ||
|
state.MapKind != lastReportedMap ||
|
||||||
state.Seed != lastReportedSeed)
|
state.Seed != lastReportedSeed ||
|
||||||
|
curWave != lastReportedWave)
|
||||||
{
|
{
|
||||||
lastReportedHumans = curHumans;
|
lastReportedHumans = curHumans;
|
||||||
lastReportedPhase = state.Phase;
|
lastReportedPhase = state.Phase;
|
||||||
lastReportedMap = state.MapKind;
|
lastReportedMap = state.MapKind;
|
||||||
lastReportedSeed = state.Seed;
|
lastReportedSeed = state.Seed;
|
||||||
|
lastReportedWave = curWave;
|
||||||
DirectoryHeartbeatSnapshot updated = BuildSnapshot(host, publicHost);
|
DirectoryHeartbeatSnapshot updated = BuildSnapshot(host, publicHost);
|
||||||
lock (dirSnapGate)
|
lock (dirSnapGate)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -225,7 +225,7 @@ namespace RustToDust.Tests
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
reporter.Dispose();
|
await reporter.DisposeAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
var afterDispose = await DirectoryBrowserClient.FetchServersAsync(baseUrl);
|
var afterDispose = await DirectoryBrowserClient.FetchServersAsync(baseUrl);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue