fixes bug 97833 "http should periodically prune it's idle connection list of

dead connections"
patch=badami@netscape.com
r=dougt@netscape.com
sr=darin@netscape.com
This commit is contained in:
darin%netscape.com 2001-12-07 01:40:18 +00:00
parent 86a29f6562
commit 74d47a6f68
6 changed files with 54 additions and 2 deletions

View File

@ -42,6 +42,7 @@ REQUIRES = xpcom \
intl \
uconv \
caps \
timer \
$(ZLIB_REQUIRES) \
$(NULL)

View File

@ -34,6 +34,7 @@ REQUIRES = xpcom \
locale \
intl \
uconv \
timer \
zlib \
caps \
$(NULL)

View File

@ -36,6 +36,7 @@ REQUIRES = xpcom \
intl \
exthandler \
caps \
timer \
$(NULL)
CPPSRCS = \

View File

@ -29,6 +29,7 @@ REQUIRES = xpcom \
intl \
exthandler \
caps \
timer \
$(NULL)
include <$(DEPTH)/config/config.mak>

View File

@ -134,6 +134,9 @@ nsHttpHandler::nsHttpHandler()
nsHttpHandler::~nsHttpHandler()
{
// We do not deal with the timer cancellation in the destructor since
// it is taken care of in xpcom shutdown event in the Observe method.
LOG(("Deleting nsHttpHandler [this=%x]\n", this));
nsHttp::DestroyAtomTable();
@ -255,7 +258,17 @@ nsHttpHandler::Init()
if (observerSvc) {
observerSvc->AddObserver(this, "profile-before-change", PR_TRUE);
observerSvc->AddObserver(this, "session-logout", PR_TRUE);
observerSvc->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_TRUE);
}
mTimer = do_CreateInstance("@mozilla.org/timer;1");
// failure to create a timer is not a fatal error, but idle connections
// may not be cleaned up as aggressively.
if (mTimer)
mTimer->Init(DeadConnectionCleanupCB, this, 15*1000, // 15 seconds
NS_PRIORITY_NORMAL,
NS_TYPE_REPEATING_SLACK);
return NS_OK;
}
@ -442,6 +455,21 @@ nsHttpHandler::ReclaimConnection(nsHttpConnection *conn)
return NS_OK;
}
nsresult
nsHttpHandler::PurgeDeadConnections()
{
nsAutoLock lock(mConnectionLock);
for (PRInt32 i = 0; i < mIdleConnections.Count(); ++i) {
nsHttpConnection *conn = (nsHttpConnection *) mIdleConnections[i];
if (conn && !conn->CanReuse()) {
// Dead and idle connection; purge it
mIdleConnections.RemoveElement(conn);
NS_RELEASE(conn);
}
}
return NS_OK;
}
// called from the socket thread (see nsHttpConnection::OnDataAvailable)
nsresult
nsHttpHandler::ProcessTransactionQ()
@ -895,6 +923,14 @@ nsHttpHandler::RemovePendingTransaction_Locked(nsHttpTransaction *trans)
return NS_ERROR_NOT_AVAILABLE;
}
void
nsHttpHandler::DeadConnectionCleanupCB(nsITimer *timer, void *closure)
{
nsHttpHandler *self = NS_STATIC_CAST(nsHttpHandler*, closure);
if (self)
self->PurgeDeadConnections();
}
void
nsHttpHandler::DropConnections(nsVoidArray &connections)
{
@ -1875,6 +1911,13 @@ nsHttpHandler::Observe(nsISupports *subject,
// depend on this value.
mSessionStartTime = NowInSeconds();
}
else if (!nsCRT::strcmp(topic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
if (mTimer) {
mTimer->Cancel();
mTimer = 0;
}
}
return NS_OK;
}

View File

@ -42,6 +42,7 @@
#include "nsWeakReference.h"
#include "nsVoidArray.h"
#include "nsIIDNService.h"
#include "nsITimer.h"
class nsHttpConnection;
class nsHttpConnectionInfo;
@ -123,6 +124,8 @@ public:
// longer than mMaxRequestDelay.
nsresult ProcessTransactionQ();
nsresult PurgeDeadConnections();
//
// The HTTP handler caches pointers to specific XPCOM services, and
// provides the following helper routines for accessing those services:
@ -189,7 +192,8 @@ private:
nsresult SetAcceptEncodings(const char *);
nsresult SetAcceptCharsets(const char *);
static PRInt32 PR_CALLBACK PrefsCallback(const char *, void *);
// timer callback for cleansing the idle connection list
static void DeadConnectionCleanupCB(nsITimer *, void *);
private:
static nsHttpHandler *mGlobalInstance;
@ -202,6 +206,7 @@ private:
nsCOMPtr<nsIStreamConverterService> mStreamConvSvc;
nsCOMPtr<nsIMIMEService> mMimeService;
nsCOMPtr<nsIIDNService> mIDNConverter;
nsCOMPtr<nsITimer> mTimer;
// the authentication credentials cache
nsHttpAuthCache *mAuthCache;
@ -234,7 +239,7 @@ private:
nsCOMPtr<nsICacheSession> mCacheSession_MEM;
PRUint32 mLastUniqueID;
PRUint32 mSessionStartTime;
// connection management
nsVoidArray mActiveConnections; // list of nsHttpConnection objects
nsVoidArray mIdleConnections; // list of nsHttpConnection objects