2009-02-10 20:04:03 +01:00
|
|
|
/** @file connpool.h */
|
2008-09-30 00:00:53 +02:00
|
|
|
|
2009-10-27 20:58:27 +01:00
|
|
|
/* Copyright 2009 10gen Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2008-09-30 00:00:53 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2009-03-30 15:47:58 +02:00
|
|
|
#include <stack>
|
2008-11-01 01:17:54 +01:00
|
|
|
#include "dbclient.h"
|
2008-09-30 00:00:53 +02:00
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
namespace mongo {
|
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
struct PoolForHost {
|
2009-03-30 15:47:58 +02:00
|
|
|
std::stack<DBClientBase*> pool;
|
2009-01-15 16:17:11 +01:00
|
|
|
};
|
2009-09-11 22:10:48 +02:00
|
|
|
|
|
|
|
class DBConnectionHook {
|
|
|
|
public:
|
|
|
|
virtual ~DBConnectionHook(){}
|
2009-09-14 17:33:20 +02:00
|
|
|
|
|
|
|
virtual void onCreate( DBClientBase * conn ){}
|
|
|
|
virtual void onHandedOut( DBClientBase * conn ){}
|
|
|
|
|
2009-09-11 22:10:48 +02:00
|
|
|
};
|
2008-09-30 00:00:53 +02:00
|
|
|
|
2009-01-27 03:15:08 +01:00
|
|
|
/** Database connection pool.
|
2009-02-09 23:17:40 +01:00
|
|
|
|
2009-01-27 03:15:08 +01:00
|
|
|
Generally, use ScopedDbConnection and do not call these directly.
|
|
|
|
|
|
|
|
This class, so far, is suitable for use with unauthenticated connections.
|
|
|
|
Support for authenticated connections requires some adjustements: please
|
|
|
|
request...
|
|
|
|
|
|
|
|
Usage:
|
2009-02-20 22:39:55 +01:00
|
|
|
|
2009-01-27 03:15:08 +01:00
|
|
|
{
|
|
|
|
ScopedDbConnection c("myserver");
|
|
|
|
c.conn()...
|
|
|
|
}
|
|
|
|
*/
|
2009-01-15 16:17:11 +01:00
|
|
|
class DBConnectionPool {
|
2010-03-15 17:42:01 +01:00
|
|
|
mongo::mutex poolMutex;
|
2009-01-27 03:15:08 +01:00
|
|
|
map<string,PoolForHost*> pools; // servername -> pool
|
2009-09-11 22:10:48 +02:00
|
|
|
list<DBConnectionHook*> _hooks;
|
2009-09-14 17:33:20 +02:00
|
|
|
|
2009-09-11 22:10:48 +02:00
|
|
|
void onCreate( DBClientBase * conn );
|
2009-09-14 17:33:20 +02:00
|
|
|
void onHandedOut( DBClientBase * conn );
|
2009-01-15 16:17:11 +01:00
|
|
|
public:
|
2009-02-20 22:39:55 +01:00
|
|
|
void flush();
|
2009-02-07 15:21:29 +01:00
|
|
|
DBClientBase *get(const string& host);
|
|
|
|
void release(const string& host, DBClientBase *c) {
|
2009-09-14 20:31:46 +02:00
|
|
|
if ( c->isFailed() )
|
|
|
|
return;
|
2010-03-15 17:42:01 +01:00
|
|
|
scoped_lock L(poolMutex);
|
2009-01-15 16:17:11 +01:00
|
|
|
pools[host]->pool.push(c);
|
|
|
|
}
|
2009-09-11 22:10:48 +02:00
|
|
|
void addHook( DBConnectionHook * hook );
|
2009-01-15 16:17:11 +01:00
|
|
|
};
|
2008-09-30 00:00:53 +02:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
extern DBConnectionPool pool;
|
2008-09-30 00:00:53 +02:00
|
|
|
|
2009-01-27 03:15:08 +01:00
|
|
|
/** Use to get a connection from the pool. On exceptions things
|
2009-01-15 16:17:11 +01:00
|
|
|
clean up nicely.
|
|
|
|
*/
|
|
|
|
class ScopedDbConnection {
|
|
|
|
const string host;
|
2009-02-07 15:21:29 +01:00
|
|
|
DBClientBase *_conn;
|
2009-01-15 16:17:11 +01:00
|
|
|
public:
|
2009-02-09 23:17:40 +01:00
|
|
|
/** get the associated connection object */
|
2009-02-09 19:34:40 +01:00
|
|
|
DBClientBase* operator->(){
|
2009-12-28 22:43:43 +01:00
|
|
|
uassert( 11004 , "did you call done already" , _conn );
|
2009-02-09 19:34:40 +01:00
|
|
|
return _conn;
|
|
|
|
}
|
|
|
|
|
2009-02-09 23:17:40 +01:00
|
|
|
/** get the associated connection object */
|
2009-02-07 15:21:29 +01:00
|
|
|
DBClientBase& conn() {
|
2009-12-28 22:43:43 +01:00
|
|
|
uassert( 11005 , "did you call done already" , _conn );
|
2009-01-15 16:17:11 +01:00
|
|
|
return *_conn;
|
|
|
|
}
|
2008-09-30 00:00:53 +02:00
|
|
|
|
2009-02-09 23:17:40 +01:00
|
|
|
/** throws UserException if can't connect */
|
2009-01-15 16:17:11 +01:00
|
|
|
ScopedDbConnection(const string& _host) :
|
2009-03-30 15:47:58 +02:00
|
|
|
host(_host), _conn( pool.get(_host) ) {
|
|
|
|
//cout << " for: " << _host << " got conn: " << _conn << endl;
|
|
|
|
}
|
2008-09-30 00:00:53 +02:00
|
|
|
|
2009-02-09 23:17:40 +01:00
|
|
|
/** Force closure of the connection. You should call this if you leave it in
|
|
|
|
a bad state. Destructor will do this too, but it is verbose.
|
2009-01-15 16:17:11 +01:00
|
|
|
*/
|
|
|
|
void kill() {
|
|
|
|
delete _conn;
|
|
|
|
_conn = 0;
|
|
|
|
}
|
2008-11-14 17:20:23 +01:00
|
|
|
|
2009-02-09 23:17:40 +01:00
|
|
|
/** Call this when you are done with the connection.
|
|
|
|
|
|
|
|
If you do not call done() before this object goes out of scope,
|
|
|
|
we can't be sure we fully read all expected data of a reply on the socket. so
|
|
|
|
we don't try to reuse the connection in that situation.
|
2009-01-15 16:17:11 +01:00
|
|
|
*/
|
|
|
|
void done() {
|
2009-02-03 17:04:54 +01:00
|
|
|
if ( ! _conn )
|
|
|
|
return;
|
|
|
|
|
2009-02-07 15:21:29 +01:00
|
|
|
/* we could do this, but instead of assume one is using autoreconnect mode on the connection
|
2009-01-15 16:17:11 +01:00
|
|
|
if ( _conn->isFailed() )
|
|
|
|
kill();
|
|
|
|
else
|
2009-02-07 15:21:29 +01:00
|
|
|
*/
|
2009-01-15 16:17:11 +01:00
|
|
|
pool.release(host, _conn);
|
|
|
|
_conn = 0;
|
|
|
|
}
|
2010-03-22 16:47:37 +01:00
|
|
|
|
|
|
|
~ScopedDbConnection();
|
2008-09-30 00:32:39 +02:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
};
|
2009-01-14 23:09:51 +01:00
|
|
|
|
|
|
|
} // namespace mongo
|