0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

timeout connections idle for 30 minutes

This commit is contained in:
Eliot Horowitz 2011-05-17 14:22:53 -04:00
parent ec722585a9
commit fa07a30464
2 changed files with 55 additions and 4 deletions

View File

@ -75,14 +75,34 @@ namespace mongo {
}
}
void PoolForHost::getStaleConnections( vector<DBClientBase*>& stale ) {
time_t now = time(0);
vector<StoredConnection> all;
while ( ! _pool.empty() ) {
StoredConnection c = _pool.top();
_pool.pop();
if ( c.ok( now ) )
all.push_back( c );
else
stale.push_back( c.conn );
}
for ( size_t i=0; i<all.size(); i++ ) {
_pool.push( all[i] );
}
}
PoolForHost::StoredConnection::StoredConnection( DBClientBase * c ) {
conn = c;
when = time(0);
}
bool PoolForHost::StoredConnection::ok( time_t now ) {
// if connection has been idle for an hour, kill it
return ( now - when ) < 3600;
// if connection has been idle for 30 minutes, kill it
return ( now - when ) < 1800;
}
void PoolForHost::createdOne( DBClientBase * base) {
@ -236,6 +256,28 @@ namespace mongo {
return ap < bp;
}
void DBConnectionPool::taskDoWork() {
vector<DBClientBase*> toDelete;
{
// we need to get the connections inside the lock
// but we can actually delete them outside
scoped_lock lk( _mutex );
for ( PoolMap::iterator i=_pools.begin(); i!=_pools.end(); ++i ) {
i->second.getStaleConnections( toDelete );
}
}
for ( size_t i=0; i<toDelete.size(); i++ ) {
try {
delete toDelete[i];
}
catch ( ... ) {
// we don't care if there was a socket error
}
}
}
// ------ ScopedDbConnection ------
ScopedDbConnection * ScopedDbConnection::steal() {

View File

@ -21,6 +21,8 @@
#include "dbclient.h"
#include "redef_macros.h"
#include "../util/background.h"
namespace mongo {
class Shard;
@ -57,6 +59,8 @@ namespace mongo {
void done( DBClientBase * c );
void flush();
void getStaleConnections( vector<DBClientBase*>& stale );
static void setMaxPerHost( unsigned max ) { _maxPerHost = max; }
static unsigned getMaxPerHost() { return _maxPerHost; }
@ -100,7 +104,7 @@ namespace mongo {
c.conn()...
}
*/
class DBConnectionPool : boost::noncopyable {
class DBConnectionPool : public PeriodicTask {
public:
@ -134,8 +138,12 @@ namespace mongo {
bool operator()( const string& a , const string& b ) const;
};
private:
virtual string taskName() const { return "DBConnectionPool-cleaner"; }
virtual void taskDoWork();
private:
DBConnectionPool( DBConnectionPool& p );
DBClientBase* _get( const string& ident );
DBClientBase* _finishCreate( const string& ident , DBClientBase* conn );
@ -150,6 +158,7 @@ namespace mongo {
// pointers owned by me, right now they leak on shutdown
// _hooks itself also leaks because it creates a shutdown race condition
list<DBConnectionHook*> * _hooks;
};
extern DBConnectionPool pool;