mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
onDestry hook for connection pool
This commit is contained in:
parent
57bb3d62dc
commit
8d009017c9
@ -36,8 +36,9 @@ namespace mongo {
|
||||
}
|
||||
}
|
||||
|
||||
void PoolForHost::done( DBClientBase * c ) {
|
||||
void PoolForHost::done( DBConnectionPool * pool, DBClientBase * c ) {
|
||||
if ( _pool.size() >= _maxPerHost ) {
|
||||
pool->onDestory( c );
|
||||
delete c;
|
||||
}
|
||||
else {
|
||||
@ -45,15 +46,16 @@ namespace mongo {
|
||||
}
|
||||
}
|
||||
|
||||
DBClientBase * PoolForHost::get() {
|
||||
DBClientBase * PoolForHost::get( DBConnectionPool * pool ) {
|
||||
|
||||
time_t now = time(0);
|
||||
|
||||
while ( ! _pool.empty() ) {
|
||||
StoredConnection sc = _pool.top();
|
||||
_pool.pop();
|
||||
if ( sc.ok( now ) )
|
||||
if ( sc.ok( now ) )
|
||||
return sc.conn;
|
||||
pool->onDestory( sc.conn );
|
||||
delete sc.conn;
|
||||
}
|
||||
|
||||
@ -127,7 +129,7 @@ namespace mongo {
|
||||
assert( ! inShutdown() );
|
||||
scoped_lock L(_mutex);
|
||||
PoolForHost& p = _pools[ident];
|
||||
return p.get();
|
||||
return p.get( this );
|
||||
}
|
||||
|
||||
DBClientBase* DBConnectionPool::_finishCreate( const string& host , DBClientBase* conn ) {
|
||||
@ -174,6 +176,17 @@ namespace mongo {
|
||||
return _finishCreate( host , c );
|
||||
}
|
||||
|
||||
void DBConnectionPool::release(const string& host, DBClientBase *c) {
|
||||
if ( c->isFailed() ) {
|
||||
onDestory( c );
|
||||
delete c;
|
||||
return;
|
||||
}
|
||||
scoped_lock L(_mutex);
|
||||
_pools[host].done(this,c);
|
||||
}
|
||||
|
||||
|
||||
DBConnectionPool::~DBConnectionPool() {
|
||||
// connection closing is handled by ~PoolForHost
|
||||
}
|
||||
@ -208,6 +221,15 @@ namespace mongo {
|
||||
}
|
||||
}
|
||||
|
||||
void DBConnectionPool::onDestory( DBClientBase * conn ) {
|
||||
if ( _hooks->size() == 0 )
|
||||
return;
|
||||
|
||||
for ( list<DBConnectionHook*>::iterator i = _hooks->begin(); i != _hooks->end(); i++ ) {
|
||||
(*i)->onDestory( conn );
|
||||
}
|
||||
}
|
||||
|
||||
void DBConnectionPool::appendInfo( BSONObjBuilder& b ) {
|
||||
BSONObjBuilder bb( b.subobjStart( "hosts" ) );
|
||||
int avail = 0;
|
||||
@ -270,6 +292,7 @@ namespace mongo {
|
||||
|
||||
for ( size_t i=0; i<toDelete.size(); i++ ) {
|
||||
try {
|
||||
onDestory( toDelete[i] );
|
||||
delete toDelete[i];
|
||||
}
|
||||
catch ( ... ) {
|
||||
|
@ -26,6 +26,7 @@
|
||||
namespace mongo {
|
||||
|
||||
class Shard;
|
||||
class DBConnectionPool;
|
||||
|
||||
/**
|
||||
* not thread safe
|
||||
@ -46,7 +47,7 @@ namespace mongo {
|
||||
|
||||
int numAvailable() const { return (int)_pool.size(); }
|
||||
|
||||
void createdOne( DBClientBase * base);
|
||||
void createdOne( DBClientBase * base );
|
||||
long long numCreated() const { return _created; }
|
||||
|
||||
ConnectionString::ConnectionType type() const { assert(_created); return _type; }
|
||||
@ -54,9 +55,9 @@ namespace mongo {
|
||||
/**
|
||||
* gets a connection or return NULL
|
||||
*/
|
||||
DBClientBase * get();
|
||||
DBClientBase * get( DBConnectionPool * pool);
|
||||
|
||||
void done( DBClientBase * c );
|
||||
void done( DBConnectionPool * pool , DBClientBase * c );
|
||||
|
||||
void flush();
|
||||
|
||||
@ -87,6 +88,7 @@ namespace mongo {
|
||||
virtual ~DBConnectionHook() {}
|
||||
virtual void onCreate( DBClientBase * conn ) {}
|
||||
virtual void onHandedOut( DBClientBase * conn ) {}
|
||||
virtual void onDestory( DBClientBase * conn ) {}
|
||||
};
|
||||
|
||||
/** Database connection pool.
|
||||
@ -116,20 +118,15 @@ namespace mongo {
|
||||
|
||||
void onCreate( DBClientBase * conn );
|
||||
void onHandedOut( DBClientBase * conn );
|
||||
void onDestory( DBClientBase * conn );
|
||||
|
||||
void flush();
|
||||
|
||||
DBClientBase *get(const string& host);
|
||||
DBClientBase *get(const ConnectionString& host);
|
||||
|
||||
void release(const string& host, DBClientBase *c) {
|
||||
if ( c->isFailed() ) {
|
||||
delete c;
|
||||
return;
|
||||
}
|
||||
scoped_lock L(_mutex);
|
||||
_pools[host].done(c);
|
||||
}
|
||||
void release(const string& host, DBClientBase *c);
|
||||
|
||||
void addHook( DBConnectionHook * hook ); // we take ownership
|
||||
void appendInfo( BSONObjBuilder& b );
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user