From ecc04b21f379a8aac661a1b177226fb3d8117e02 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 7 Jan 2013 14:42:46 -0800 Subject: [PATCH] Be smarter about when we clean up ledger acquires. --- src/cpp/ripple/LedgerAcquire.cpp | 13 ++++++++++++- src/cpp/ripple/LedgerAcquire.h | 3 +++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/cpp/ripple/LedgerAcquire.cpp b/src/cpp/ripple/LedgerAcquire.cpp index 719d2e0744..2d008260a5 100644 --- a/src/cpp/ripple/LedgerAcquire.cpp +++ b/src/cpp/ripple/LedgerAcquire.cpp @@ -20,6 +20,7 @@ DECLARE_INSTANCE(LedgerAcquire); PeerSet::PeerSet(const uint256& hash, int interval) : mHash(hash), mTimerInterval(interval), mTimeouts(0), mComplete(false), mFailed(false), mProgress(true), mTimer(theApp->getIOService()) { + mLastAction = time(NULL); assert((mTimerInterval > 10) && (mTimerInterval < 30000)); } @@ -170,6 +171,7 @@ void LedgerAcquire::done() if (mSignaled) return; mSignaled = true; + touch(); #ifdef LA_DEBUG cLog(lsTRACE) << "Done acquiring ledger " << mHash; #endif @@ -559,7 +561,10 @@ LedgerAcquire::pointer LedgerAcquireMaster::findCreate(const uint256& hash) boost::mutex::scoped_lock sl(mLock); LedgerAcquire::pointer& ptr = mLedgers[hash]; if (ptr) + { + ptr->touch(); return ptr; + } ptr = boost::make_shared(hash); ptr->addPeers(); ptr->setTimer(); // Cannot call in constructor @@ -572,7 +577,10 @@ LedgerAcquire::pointer LedgerAcquireMaster::find(const uint256& hash) boost::mutex::scoped_lock sl(mLock); std::map::iterator it = mLedgers.find(hash); if (it != mLedgers.end()) + { + it->second->touch(); return it->second; + } return LedgerAcquire::pointer(); } @@ -726,12 +734,15 @@ bool LedgerAcquireMaster::isFailure(const uint256& hash) void LedgerAcquireMaster::sweep() { + time_t now = time(NULL); boost::mutex::scoped_lock sl(mLock); std::map::iterator it = mLedgers.begin(); while (it != mLedgers.end()) { - if (it->second->isDone()) + if (it->second->getLastAction() > now) + it->second->touch(); + else if ((it->second->getLastAction() + 500) < now) mLedgers.erase(it++); else ++it; diff --git a/src/cpp/ripple/LedgerAcquire.h b/src/cpp/ripple/LedgerAcquire.h index bda77279ea..2ef1384d70 100644 --- a/src/cpp/ripple/LedgerAcquire.h +++ b/src/cpp/ripple/LedgerAcquire.h @@ -26,6 +26,7 @@ protected: uint256 mHash; int mTimerInterval, mTimeouts; bool mComplete, mFailed, mProgress; + time_t mLastAction; boost::recursive_mutex mLock; boost::asio::deadline_timer mTimer; @@ -45,6 +46,8 @@ public: void progress() { mProgress = true; } bool isProgress() { return mProgress; } + void touch() { mLastAction = time(NULL); } + time_t getLastAction() { return mLastAction; } void peerHas(Peer::ref); void badPeer(Peer::ref);