From c8b48da9a93bf7b28e1183d00152ebf350296b4b Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Fri, 12 Jan 2024 15:27:37 +0100 Subject: Synchelper: better initial sync detection --- LibMatrix/Helpers/SyncHelper.cs | 34 ++++++++++++++++++++---- Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs | 2 ++ Utilities/LibMatrix.DevTestBot/appsettings.json | 3 ++- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/LibMatrix/Helpers/SyncHelper.cs b/LibMatrix/Helpers/SyncHelper.cs index 0a11849..691b964 100644 --- a/LibMatrix/Helpers/SyncHelper.cs +++ b/LibMatrix/Helpers/SyncHelper.cs @@ -26,11 +26,12 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg Console.WriteLine("Null passed as homeserver for SyncHelper!"); throw new ArgumentNullException(nameof(homeserver), "Null passed as homeserver for SyncHelper!"); } + if (homeserver.ClientHttpClient is null) { Console.WriteLine("Homeserver for SyncHelper is not properly configured!"); throw new ArgumentNullException(nameof(homeserver.ClientHttpClient), "Null passed as homeserver for SyncHelper!"); } - + var sw = Stopwatch.StartNew(); var url = $"/_matrix/client/v3/sync?timeout={Timeout}&set_presence={SetPresence}&full_state={(FullState ? "true" : "false")}"; @@ -43,7 +44,8 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg if (httpResp is null) throw new NullReferenceException("Failed to send HTTP request"); logger?.LogInformation("Got sync response: {} bytes, {} elapsed", httpResp.Content.Headers.ContentLength ?? -1, sw.Elapsed); var deserializeSw = Stopwatch.StartNew(); - var resp = await httpResp.Content.ReadFromJsonAsync(cancellationToken: cancellationToken ?? CancellationToken.None, jsonTypeInfo: SyncResponseSerializerContext.Default.SyncResponse); + var resp = await httpResp.Content.ReadFromJsonAsync(cancellationToken: cancellationToken ?? CancellationToken.None, + jsonTypeInfo: SyncResponseSerializerContext.Default.SyncResponse); logger?.LogInformation("Deserialized sync response: {} bytes, {} elapsed, {} total", httpResp.Content.Headers.ContentLength ?? -1, deserializeSw.Elapsed, sw.Elapsed); var timeToWait = MinimumDelay.Subtract(sw.Elapsed); if (timeToWait.TotalMilliseconds > 0) @@ -74,16 +76,38 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg public async Task RunSyncLoopAsync(bool skipInitialSyncEvents = true, CancellationToken? cancellationToken = null) { var sw = Stopwatch.StartNew(); int emptyInitialSyncCount = 0; + int syncCount = 0; var oldTimeout = Timeout; Timeout = 0; await foreach (var sync in EnumerateSyncAsync(cancellationToken)) { - if (sync.ToJson(ignoreNull: true, indent: false).Length < 250) { + syncCount++; + if (sync is { + AccountData: null or { + Events: null or { Count: 0 } + }, + Rooms: null or { + Invite: null or { Count: 0 }, + Join: null or { Count: 0 }, + Leave: null or { Count: 0 } + }, + Presence: null or { + Events: null or { Count: 0 } + }, + DeviceLists: null or { + Changed: null or { Count: 0 }, + Left: null or { Count: 0 } + }, + ToDevice: null or { + Events: null or { Count: 0 } + } + }) { emptyInitialSyncCount++; - if (emptyInitialSyncCount > 5) { + if (emptyInitialSyncCount >= 2) { IsInitialSync = false; Timeout = oldTimeout; } - } + } else if (syncCount > 15) + Console.WriteLine(sync.ToJson(ignoreNull: true, indent: true)); await RunSyncLoopCallbacksAsync(sync, IsInitialSync && skipInitialSyncEvents); } diff --git a/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs b/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs index e41321a..b2f597c 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs @@ -44,6 +44,8 @@ public class DevTestBot : IHostedService { throw; } + var res = await hs.ResolveRoomAliasAsync("#watercooler:maunium.net"); + var syncHelper = new SyncHelper(hs); await (hs.GetRoom("!DoHEdFablOLjddKWIp:rory.gay")).JoinAsync(); diff --git a/Utilities/LibMatrix.DevTestBot/appsettings.json b/Utilities/LibMatrix.DevTestBot/appsettings.json index 5668b53..6613979 100644 --- a/Utilities/LibMatrix.DevTestBot/appsettings.json +++ b/Utilities/LibMatrix.DevTestBot/appsettings.json @@ -8,6 +8,7 @@ }, "Bot": { "Homeserver": "rory.gay", - "AccessToken": "syt_xxxxxxxxxxxxxxxxx" + "AccessToken": "syt_xxxxxxxxxxxxxxxxx", + "Prefix": "!" } } \ No newline at end of file -- cgit 1.4.1