From b6821746d0adf9bb9cf5bf942a4b209c6ebf1cad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Kaan=20K=C3=B6se?= Date: Wed, 26 Feb 2025 23:11:49 +0100 Subject: [PATCH] Locked busy scope to handle disconnections properly. --- Wino.Core/Integration/WinoImapClient.cs | 34 ++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/Wino.Core/Integration/WinoImapClient.cs b/Wino.Core/Integration/WinoImapClient.cs index 5701cf93..f4712268 100644 --- a/Wino.Core/Integration/WinoImapClient.cs +++ b/Wino.Core/Integration/WinoImapClient.cs @@ -1,4 +1,6 @@ -using MailKit; +using System; +using System.Threading; +using MailKit; using MailKit.Net.Imap; using Serilog; @@ -9,6 +11,8 @@ namespace Wino.Core.Integration; /// internal class WinoImapClient : ImapClient { + private int _busyCount; + /// /// Gets or internally sets whether the QRESYNC extension is enabled. /// It is set by ImapClientPool immidiately after the authentication. @@ -47,6 +51,34 @@ internal class WinoImapClient : ImapClient } } + public bool IsBusy() => _busyCount > 0; + + public IDisposable GetBusyScope() + { + Interlocked.Increment(ref _busyCount); + return new BusyScope(this); + } + + private class BusyScope : IDisposable + { + private readonly WinoImapClient _client; + private bool _disposed; + + public BusyScope(WinoImapClient client) + { + _client = client; + } + + public void Dispose() + { + if (!_disposed) + { + Interlocked.Decrement(ref _client._busyCount); + _disposed = true; + } + } + } + protected override void Dispose(bool disposing) { base.Dispose(disposing);