diff --git a/modules/ripple_app/ledger/ripple_InboundLedgers.cpp b/modules/ripple_app/ledger/ripple_InboundLedgers.cpp index fc67d29465..14213e1de5 100644 --- a/modules/ripple_app/ledger/ripple_InboundLedgers.cpp +++ b/modules/ripple_app/ledger/ripple_InboundLedgers.cpp @@ -221,23 +221,41 @@ void InboundLedgers::sweep () { mRecentFailures.sweep (); - int now = UptimeTimer::getInstance ().getElapsedSeconds (); - boost::mutex::scoped_lock sl (mLock); + int const now = UptimeTimer::getInstance ().getElapsedSeconds (); - boost::unordered_map::iterator it = mLedgers.begin (); - - while (it != mLedgers.end ()) + // Make a list of things to sweep, while holding the lock + std::vector stuffToSweep; + std::size_t total; { - if (it->second->getLastAction () > now) + boost::mutex::scoped_lock sl (mLock); + MapType::iterator it (mLedgers.begin ()); + total = mLedgers.size (); + stuffToSweep.reserve (total); + + while (it != mLedgers.end ()) { - it->second->touch (); - ++it; + if (it->second->getLastAction () > now) + { + it->second->touch (); + ++it; + } + else if ((it->second->getLastAction () + 60) < now) + { + stuffToSweep.push_back (it->second); + // shouldn't cause the actual final delete + // since we are holding a reference in the vector. + it = mLedgers.erase (it); + } + else + { + ++it; + } } - else if ((it->second->getLastAction () + 60) < now) - it = mLedgers.erase (it); - else - ++it; } + + WriteLog (lsDEBUG, InboundLedger) << + "Sweeped " << stuffToSweep.size () << + " out of " << total << " inbound ledgers."; } int InboundLedgers::getFetchCount (int& timeoutCount) diff --git a/modules/ripple_app/ledger/ripple_InboundLedgers.h b/modules/ripple_app/ledger/ripple_InboundLedgers.h index 655475c0a2..4735aae91e 100644 --- a/modules/ripple_app/ledger/ripple_InboundLedgers.h +++ b/modules/ripple_app/ledger/ripple_InboundLedgers.h @@ -64,8 +64,10 @@ public: void sweep (); private: + typedef boost::unordered_map MapType; + boost::mutex mLock; - boost::unordered_map mLedgers; + MapType mLedgers; KeyCache mRecentFailures; };