[Libreoffice-commits] online.git: loolwsd/LOOLWSD.cpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Sun Oct 23 21:18:41 UTC 2016


 loolwsd/LOOLWSD.cpp |   37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

New commits:
commit 70690f6dabffd1ffb5aff8e01349371700b940e1
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Sun Oct 23 15:59:22 2016 -0400

    loolwsd: remove dead DocumentBrokers
    
    Change-Id: If9b24ac1e45454e21222699a00defa70acc4ed80
    Reviewed-on: https://gerrit.libreoffice.org/30212
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/LOOLWSD.cpp b/loolwsd/LOOLWSD.cpp
index a9aa59d..a287320 100644
--- a/loolwsd/LOOLWSD.cpp
+++ b/loolwsd/LOOLWSD.cpp
@@ -224,6 +224,30 @@ void shutdownLimitReached(WebSocket& ws)
 
 }
 
+/// Remove dead DocBrokers.
+/// Returns true if at least one is removed.
+bool cleanupDocBrokers()
+{
+    Util::assertIsLocked(DocBrokersMutex);
+
+    const auto count = DocBrokers.size();
+    for (auto it = DocBrokers.begin(); it != DocBrokers.end(); )
+    {
+        // Cleanup used and dead entries.
+        if (it->second->isLoaded() && !it->second->isAlive())
+        {
+            Log::debug("Removing dead DocBroker [" + it->first + "].");
+            it = DocBrokers.erase(it);
+        }
+        else
+        {
+            ++it;
+        }
+    }
+
+    return (count != DocBrokers.size());
+}
+
 static void forkChildren(const int number)
 {
     Util::assertIsLocked(NewChildrenMutex);
@@ -273,6 +297,14 @@ static void preForkChildren()
 /// to load documents with alacrity.
 static void prespawnChildren()
 {
+    // First remove dead DocBrokers, if possible.
+    std::unique_lock<std::mutex> docBrokersLock(DocBrokersMutex, std::defer_lock);
+    if (docBrokersLock.try_lock())
+    {
+        cleanupDocBrokers();
+        docBrokersLock.unlock();
+    }
+
     std::unique_lock<std::mutex> lock(NewChildrenMutex, std::defer_lock);
     if (!lock.try_lock())
     {
@@ -468,6 +500,8 @@ private:
                     // In that case, we can use a pool and index by publicPath.
                     std::unique_lock<std::mutex> lock(DocBrokersMutex);
 
+                    cleanupDocBrokers();
+
                     //FIXME: What if the same document is already open? Need a fake dockey here?
                     Log::debug("New DocumentBroker for docKey [" + docKey + "].");
                     DocBrokers.emplace(docKey, docBroker);
@@ -683,6 +717,8 @@ private:
                 return;
             }
 
+            cleanupDocBrokers();
+
             // Lookup this document.
             auto it = DocBrokers.find(docKey);
             if (it != DocBrokers.end())
@@ -1991,6 +2027,7 @@ int LOOLWSD::main(const std::vector<std::string>& /*args*/)
                     try
                     {
                         std::unique_lock<std::mutex> DocBrokersLock(DocBrokersMutex);
+                        cleanupDocBrokers();
                         for (auto& brokerIt : DocBrokers)
                         {
                             brokerIt.second->autoSave(false, 0);


More information about the Libreoffice-commits mailing list