Add proper support for server list provider cache

This commit is contained in:
Łukasz Domeradzki
2024-10-21 12:56:17 +02:00
parent 6bc29e8c3f
commit 2e6d8bc7f1
3 changed files with 12 additions and 11 deletions

View File

@@ -675,10 +675,6 @@ public static class ASF {
throw new InvalidOperationException(nameof(WebBrowser));
}
// Kill SK2 servers cache in order to force refresh during initial connection
// TODO: This should be removed when SK2 learns to purge its stale cache itself
await GlobalDatabase.ServerListProvider.UpdateServerListAsync([]).ConfigureAwait(false);
HashSet<string> botNames;
try {

View File

@@ -353,6 +353,9 @@ public sealed class Bot : IAsyncDisposable, IDisposable {
}
);
// Decrease the ServerList cache in order to fight with Steam gibberish data
SteamConfiguration.ServerList.ServerListBeforeRefreshTimeSpan = TimeSpan.FromHours(1);
// Initialize
SteamClient = new SteamClient(SteamConfiguration, botName);

View File

@@ -34,23 +34,25 @@ using SteamKit2.Discovery;
namespace ArchiSteamFarm.Steam.SteamKit2;
internal sealed class InMemoryServerListProvider : IServerListProvider {
// TODO
public DateTime LastServerListRefresh => DateTime.MinValue;
[JsonInclude]
public DateTime LastServerListRefresh { get; private set; }
[JsonDisallowNull]
[JsonInclude]
private ConcurrentHashSet<ServerRecordEndPoint> ServerRecords { get; init; } = [];
private ConcurrentList<ServerRecordEndPoint> ServerRecords { get; init; } = [];
public Task<IEnumerable<ServerRecord>> FetchServerListAsync() => Task.FromResult(ServerRecords.Where(static server => !string.IsNullOrEmpty(server.Host) && server is { Port: > 0, ProtocolTypes: > 0 }).Select(static server => ServerRecord.CreateServer(server.Host, server.Port, server.ProtocolTypes)));
public Task UpdateServerListAsync(IEnumerable<ServerRecord> endpoints) {
ArgumentNullException.ThrowIfNull(endpoints);
HashSet<ServerRecordEndPoint> newServerRecords = endpoints.Select(static endpoint => new ServerRecordEndPoint(endpoint.GetHost(), (ushort) endpoint.GetPort(), endpoint.ProtocolTypes)).ToHashSet();
LastServerListRefresh = DateTime.UtcNow;
if (ServerRecords.ReplaceIfNeededWith(newServerRecords)) {
ServerListUpdated?.Invoke(this, EventArgs.Empty);
}
IEnumerable<ServerRecordEndPoint> serverRecords = endpoints.Select(static endpoint => new ServerRecordEndPoint(endpoint.GetHost(), (ushort) endpoint.GetPort(), endpoint.ProtocolTypes));
ServerRecords.ReplaceWith(serverRecords);
ServerListUpdated?.Invoke(this, EventArgs.Empty);
return Task.CompletedTask;
}