From 7707eaf0457317f289fd29a16b658596b41fa09a Mon Sep 17 00:00:00 2001
From: EndrII <EndrIIMail@gmail.com>
Date: Wed, 19 Feb 2025 13:28:56 +0100
Subject: [PATCH] added more logs and mutex

---
 src/qTbot/src/public/qTbot/ibot.cpp | 34 ++++++++++++++++++++++++-----
 src/qTbot/src/public/qTbot/ibot.h   |  4 +++-
 2 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/src/qTbot/src/public/qTbot/ibot.cpp b/src/qTbot/src/public/qTbot/ibot.cpp
index 2dbd22a..c45e94d 100644
--- a/src/qTbot/src/public/qTbot/ibot.cpp
+++ b/src/qTbot/src/public/qTbot/ibot.cpp
@@ -115,7 +115,23 @@ void IBot::setParallelActiveNetworkThreads(int newParallelActiveNetworkThreads)
 }
 
 void IBot::setCurrentParallelActiveNetworkThreads(int newParallelActiveNetworkThreads) {
+    bool wasBusy = _currentParallelActiveNetworkThreads == _parallelActiveNetworkThreads;
+    static bool lastMessageWasFree = false;
+
     _currentParallelActiveNetworkThreads = newParallelActiveNetworkThreads;
+
+    if (_currentParallelActiveNetworkThreads == _parallelActiveNetworkThreads) {
+        qInfo() << "All network threads are busy!";
+        lastMessageWasFree = false;
+
+    } else if (wasBusy) {
+        qInfo() << "Network threads are free! available: " << _currentParallelActiveNetworkThreads << " from " << _parallelActiveNetworkThreads;
+        lastMessageWasFree = false;
+
+    } else if (_currentParallelActiveNetworkThreads == 0 && !lastMessageWasFree) {
+        qInfo() << "All network threads are free!";
+        lastMessageWasFree = true;
+    }
 }
 
 int IBot::reqestLimitPerSecond() const {
@@ -131,9 +147,11 @@ IBot::sendRequest(const QSharedPointer<iRequest> &rquest) {
     auto&& responce = QSharedPointer<QPromise<QByteArray>>::create();
     responce->start();
 
-
-    _requestQueue.insert(makeKey(rquest->priority()),
-                         RequestData{rquest, "", responce});
+    {
+        QMutexLocker lock(&_mutex);
+        _requestQueue.insert(makeKey(rquest->priority()),
+                             RequestData{rquest, "", responce});
+    }
 
     if (!_requestExecutor->isActive()) {
         handleEcxecuteRequest();
@@ -150,8 +168,12 @@ IBot::sendRequest(const QSharedPointer<iRequest> &rquest,
     auto&& responce = QSharedPointer<QPromise<QByteArray>>::create();
     responce->start();
 
-    _requestQueue.insert(makeKey(rquest->priority()),
-                         RequestData{rquest, pathToResult, responce});
+    {
+        QMutexLocker lock(&_mutex);
+        _requestQueue.insert(makeKey(rquest->priority()),
+                             RequestData{rquest, pathToResult, responce});
+    }
+
 
     if (!_requestExecutor->isActive()) {
         handleEcxecuteRequest();
@@ -184,6 +206,8 @@ void IBot::handleIncomeNewUpdate(const QSharedPointer<iUpdate> & message) {
 }
 
 void IBot::handleEcxecuteRequest() {
+    QMutexLocker lock(&_mutex);
+
     if (!_requestQueue.size()) {
         _requestExecutor->stop();
         return;
diff --git a/src/qTbot/src/public/qTbot/ibot.h b/src/qTbot/src/public/qTbot/ibot.h
index f9517e3..7f95228 100644
--- a/src/qTbot/src/public/qTbot/ibot.h
+++ b/src/qTbot/src/public/qTbot/ibot.h
@@ -305,7 +305,7 @@ signals:
     /**
      * @brief sigReceiveUpdate emit when but receive any updates from users.
      */
-    void sigReceiveUpdate(const QSharedPointer<iUpdate>& );
+    void sigReceiveUpdate(const QSharedPointer<qTbot::iUpdate>& );
 
     /**
      * @brief sigStopRequire just custm event for stop bot if tou use services.
@@ -339,6 +339,8 @@ private:
     int _currentParallelActiveNetworkThreads = 0;
     int _parallelActiveNetworkThreads = 5;
 
+    QRecursiveMutex _mutex;
+
 
 
 };