From e9cf1bd814e28f465d756fdb8e88bc029ad7895b Mon Sep 17 00:00:00 2001 From: Sergey Nazarov Date: Wed, 20 Mar 2024 19:55:33 +0400 Subject: [PATCH] Add sync primitives to singletons' operations --- .../TelegramClientContainer.cs | 22 ++++++++---- .../UpdateListeners/TelegramRegistry.cs | 35 ++++++++++++------- .../UpdateListenerBackgroundService.cs | 1 - 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramClientContainer.cs b/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramClientContainer.cs index fd8bca7..578bc78 100644 --- a/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramClientContainer.cs +++ b/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramClientContainer.cs @@ -12,6 +12,8 @@ public sealed class TelegramClientContainer : ITelegramClientContainer, IDisposa public bool Initialized { get; private set; } + private readonly object _syncRoot = new(); + public TelegramClientContainer(IOptions options) { _options = options.Value ?? throw new ArgumentNullException(nameof(options)); @@ -19,19 +21,25 @@ public sealed class TelegramClientContainer : ITelegramClientContainer, IDisposa public void Initialize() { - if (Initialized) - return; + lock (_syncRoot) + { + if (Initialized) + return; - _client = new Client(ConfigureWTelegramClient); + _client = new Client(ConfigureWTelegramClient); - Initialized = true; + Initialized = true; + } } public void Reset() { - Initialized = false; - Dispose(); - _client = null; + lock (_syncRoot) + { + Initialized = false; + Dispose(); + _client = null; + } } private string ConfigureWTelegramClient(string what) diff --git a/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramRegistry.cs b/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramRegistry.cs index 13d271e..3208898 100644 --- a/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramRegistry.cs +++ b/src/Nocr.TelegramListener.AppServices/UpdateListeners/TelegramRegistry.cs @@ -9,26 +9,35 @@ public sealed class TelegramRegistry public ConcurrentDictionary Users = new(); public ConcurrentDictionary Chats = new(); + private readonly object _syncRoot = new(); + public void SetMy(User my) { - if (my == null) + lock (_syncRoot) { - throw new ArgumentNullException(nameof(my)); - } - - if (My == null) - { - My = my; - return; - } + if (my == null) + { + throw new ArgumentNullException(nameof(my)); + } - throw new InvalidOperationException("My already set"); + if (My == null) + { + My = my; + Users[my.id] = my; + return; + } + + throw new InvalidOperationException("My already set"); + } } public void Clear() { - My = null; - Users.Clear(); - Chats.Clear(); + lock (_syncRoot) + { + My = null; + Users.Clear(); + Chats.Clear(); + } } } \ No newline at end of file diff --git a/src/Nocr.TelegramListener.AppServices/UpdateListeners/UpdateListenerBackgroundService.cs b/src/Nocr.TelegramListener.AppServices/UpdateListeners/UpdateListenerBackgroundService.cs index d1e3c2a..e3031b0 100644 --- a/src/Nocr.TelegramListener.AppServices/UpdateListeners/UpdateListenerBackgroundService.cs +++ b/src/Nocr.TelegramListener.AppServices/UpdateListeners/UpdateListenerBackgroundService.cs @@ -47,7 +47,6 @@ public sealed class UpdateListenerBackgroundService : BackgroundService client.OnUpdate += HandleUpdates; var my = await client.LoginUserIfNeeded(); _telegramRegistry.SetMy(my); - _telegramRegistry.Users[my.id] = my; // Note: on login, Telegram may sends a bunch of updates/messages that happened in the past and were not acknowledged Console.WriteLine(