about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-04-23 23:39:17 +0200
committerRory& <root@rory.gay>2025-04-23 23:39:56 +0200
commit50f8db5284c344512ecf010bbe58857b69c5535f (patch)
tree176041be5a0e2f7a96e34c8a877d8cc16b6f81e0
parentUpdate devtestbot to use bot utils (diff)
downloadLibMatrix-50f8db5284c344512ecf010bbe58857b69c5535f.tar.xz
Allow early return in SyncHelper, trim access token if path used, fix shutdown of command listener
-rw-r--r--LibMatrix/Helpers/SyncHelper.cs23
-rw-r--r--LibMatrix/Helpers/SyncStateResolver.cs2
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs2
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs9
4 files changed, 24 insertions, 12 deletions
diff --git a/LibMatrix/Helpers/SyncHelper.cs b/LibMatrix/Helpers/SyncHelper.cs

index bdbd0b4..abbf541 100644 --- a/LibMatrix/Helpers/SyncHelper.cs +++ b/LibMatrix/Helpers/SyncHelper.cs
@@ -80,7 +80,7 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg _filterIsDirty = false; } - public async Task<SyncResponse?> SyncAsync(CancellationToken? cancellationToken = null) { + public async Task<SyncResponse?> SyncAsync(CancellationToken? cancellationToken = null, bool noDelay = false) { if (homeserver is null) { Console.WriteLine("Null passed as homeserver for SyncHelper!"); throw new ArgumentNullException(nameof(homeserver), "Null passed as homeserver for SyncHelper!"); @@ -91,7 +91,7 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg throw new ArgumentNullException(nameof(homeserver.ClientHttpClient), "Null passed as homeserver for SyncHelper!"); } - if (storageProvider is null) return await SyncAsyncInternal(cancellationToken); + if (storageProvider is null) return await SyncAsyncInternal(cancellationToken, noDelay); var key = Since ?? "init"; if (await storageProvider.ObjectExistsAsync(key)) { @@ -103,13 +103,13 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg } } - var sync = await SyncAsyncInternal(cancellationToken); + var sync = await SyncAsyncInternal(cancellationToken, noDelay); // Ditto here. if (sync is not null && sync.NextBatch != Since) await storageProvider.SaveObjectAsync(key, sync); return sync; } - private async Task<SyncResponse?> SyncAsyncInternal(CancellationToken? cancellationToken = null) { + private async Task<SyncResponse?> SyncAsyncInternal(CancellationToken? cancellationToken = null, bool noDelay = false) { var sw = Stopwatch.StartNew(); if (_filterIsDirty) await UpdateFilterAsync(); @@ -138,8 +138,11 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg } var timeToWait = MinimumDelay.Subtract(sw.Elapsed); - if (timeToWait.TotalMilliseconds > 0) + if (!noDelay && timeToWait.TotalMilliseconds > 0) { + logger?.LogWarning("SyncAsyncInternal: Waiting {delay}", timeToWait); await Task.Delay(timeToWait); + } + return resp; } catch (TaskCanceledException) { @@ -157,10 +160,18 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg public async IAsyncEnumerable<SyncResponse> EnumerateSyncAsync(CancellationToken? cancellationToken = null) { while (!cancellationToken?.IsCancellationRequested ?? true) { - var sync = await SyncAsync(cancellationToken); + var sw = Stopwatch.StartNew(); + var sync = await SyncAsync(cancellationToken, noDelay: true); if (sync is null) continue; if (!string.IsNullOrWhiteSpace(sync.NextBatch)) Since = sync.NextBatch; yield return sync; + + + var timeToWait = MinimumDelay.Subtract(sw.Elapsed); + if (timeToWait.TotalMilliseconds > 0) { + logger?.LogWarning("EnumerateSyncAsync: Waiting {delay}", timeToWait); + await Task.Delay(timeToWait); + } } } diff --git a/LibMatrix/Helpers/SyncStateResolver.cs b/LibMatrix/Helpers/SyncStateResolver.cs
index a8f5615..f111c79 100644 --- a/LibMatrix/Helpers/SyncStateResolver.cs +++ b/LibMatrix/Helpers/SyncStateResolver.cs
@@ -46,7 +46,7 @@ public class SyncStateResolver(AuthenticatedHomeserverGeneric homeserver, ILogge _syncHelper.Filter = Filter; _syncHelper.FullState = FullState; - var sync = await _syncHelper.SyncAsync(cancellationToken); + var sync = await _syncHelper.SyncAsync(cancellationToken, noDelay: false); if (sync is null) return await ContinueAsync(cancellationToken); if (MergedState is null) MergedState = sync; diff --git a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs
index 4947394..56ceb65 100644 --- a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs +++ b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs
@@ -32,7 +32,7 @@ public class BotInstaller(IServiceCollection services) { if (!string.IsNullOrWhiteSpace(config.AccessTokenPath)) { var token = File.ReadAllText(config.AccessTokenPath); - config.AccessToken = token; + config.AccessToken = token.Trim(); } var hs = hsProvider.GetAuthenticatedWithToken(config.Homeserver, config.AccessToken).Result; diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs
index d07090f..f9b46d2 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs
@@ -19,6 +19,7 @@ public class CommandListenerHostedService : IHostedService { private readonly Func<CommandResult, Task>? _commandResultHandler; private Task? _listenerTask; + private CancellationTokenSource _cts = new(); public CommandListenerHostedService(AuthenticatedHomeserverGeneric hs, ILogger<CommandListenerHostedService> logger, IServiceProvider services, LibMatrixBotConfiguration config, Func<CommandResult, Task>? commandResultHandler = null) { @@ -35,7 +36,7 @@ public class CommandListenerHostedService : IHostedService { /// <summary>Triggered when the application host is ready to start the service.</summary> /// <param name="cancellationToken">Indicates that the start process has been aborted.</param> public Task StartAsync(CancellationToken cancellationToken) { - _listenerTask = Run(cancellationToken); + _listenerTask = Run(_cts.Token); _logger.LogInformation("Command listener started (StartAsync)!"); return Task.CompletedTask; } @@ -54,7 +55,7 @@ public class CommandListenerHostedService : IHostedService { }); var syncHelper = new SyncHelper(_hs, _logger) { - Timeout = 300_000, + Timeout = 30_000, FilterId = filter }; @@ -96,7 +97,7 @@ public class CommandListenerHostedService : IHostedService { } }); - await syncHelper.RunSyncLoopAsync(cancellationToken: cancellationToken); + await syncHelper.RunSyncLoopAsync(cancellationToken: _cts.Token); } /// <summary>Triggered when the application host is performing a graceful shutdown.</summary> @@ -108,7 +109,7 @@ public class CommandListenerHostedService : IHostedService { return; } - await _listenerTask.WaitAsync(cancellationToken); + await _cts.CancelAsync(); } private async Task<string?> GetUsedPrefix(StateEventResponse evt) {