From b475ba5cf146114a820b464e901b5129b2abfe84 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Wed, 11 Oct 2023 15:18:02 +0200 Subject: Fix bug with event sending, make initial sync detection deterministic --- LibMatrix/Helpers/SyncHelper.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'LibMatrix/Helpers') diff --git a/LibMatrix/Helpers/SyncHelper.cs b/LibMatrix/Helpers/SyncHelper.cs index a793d76..6bdf5ad 100644 --- a/LibMatrix/Helpers/SyncHelper.cs +++ b/LibMatrix/Helpers/SyncHelper.cs @@ -14,7 +14,6 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg public SyncFilter? Filter { get; set; } public bool FullState { get; set; } = false; - public async Task SyncAsync(CancellationToken? cancellationToken = null) { var url = $"/_matrix/client/v3/sync?timeout={Timeout}&set_presence={SetPresence}&full_state={(FullState ? "true" : "false")}"; if (!string.IsNullOrWhiteSpace(Since)) url += $"&since={Since}"; @@ -37,7 +36,7 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg } public async IAsyncEnumerable EnumerateSyncAsync(CancellationToken? cancellationToken = null) { - while(!cancellationToken?.IsCancellationRequested ?? true) { + while (!cancellationToken?.IsCancellationRequested ?? true) { var sync = await SyncAsync(cancellationToken); if (sync is null) continue; Since = sync.NextBatch ?? Since; @@ -47,14 +46,25 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg public async Task RunSyncLoopAsync(bool skipInitialSyncEvents = true, CancellationToken? cancellationToken = null) { var sw = Stopwatch.StartNew(); + bool isInitialSync = true; + int emptyInitialSyncCount = 0; + var oldTimeout = Timeout; + Timeout = 0; await foreach (var sync in EnumerateSyncAsync(cancellationToken)) { logger?.LogInformation("Got sync response: {} bytes, {} elapsed", sync?.ToJson(ignoreNull: true, indent: false).Length ?? -1, sw.Elapsed); - await RunSyncLoopCallbacksAsync(sync, Since is null && skipInitialSyncEvents); + if (sync?.ToJson(ignoreNull: true, indent: false).Length < 250) { + emptyInitialSyncCount++; + if (emptyInitialSyncCount > 5) { + isInitialSync = false; + Timeout = oldTimeout; + } + } + + await RunSyncLoopCallbacksAsync(sync, isInitialSync && skipInitialSyncEvents); } } private async Task RunSyncLoopCallbacksAsync(SyncResponse syncResponse, bool isInitialSync) { - var tasks = SyncReceivedHandlers.Select(x => x(syncResponse)).ToList(); await Task.WhenAll(tasks); -- cgit 1.4.1