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"
|
2010-04-23 23:52:10 +02:00
|
|
|
#include "redef_macros.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 {
|
2010-04-29 05:59:24 +02:00
|
|
|
PoolForHost()
|
|
|
|
: created(0){}
|
2009-03-30 15:47:58 +02:00
|
|
|
std::stack<DBClientBase*> pool;
|
2010-04-29 05:59:24 +02:00
|
|
|
long long created;
|
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-04-29 05:59:24 +02:00
|
|
|
mongo::mutex _mutex;
|
|
|
|
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) {
|
2010-05-03 14:48:15 +02:00
|
|
|
if ( c->isFailed() ){
|
|
|
|
delete c;
|
2009-09-14 20:31:46 +02:00
|
|
|
return;
|
2010-05-03 14:48:15 +02:00
|
|
|
}
|
2010-04-29 05:59:24 +02:00
|
|
|
scoped_lock L(_mutex);
|
|
|
|
_pools[host]->pool.push(c);
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
2009-09-11 22:10:48 +02:00
|
|
|
void addHook( DBConnectionHook * hook );
|
2010-04-29 05:59:24 +02:00
|
|
|
void appendInfo( BSONObjBuilder& b );
|
2009-01-15 16:17:11 +01:00
|
|
|
};
|
2010-04-29 05:59:24 +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.
|
|
|
|
*/
|
2010-04-19 22:14:46 +02:00
|
|
|
class ScopedDbConnection : boost::noncopyable {
|
2010-04-26 06:05:10 +02:00
|
|
|
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;
|
|
|
|
}
|
2010-04-19 22:55:16 +02:00
|
|
|
|
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
|
|
|
|
2010-04-19 22:55:16 +02:00
|
|
|
/** get the associated connection object */
|
|
|
|
DBClientBase* get() {
|
|
|
|
uassert( 13102 , "did you call done already" , _conn );
|
|
|
|
return _conn;
|
|
|
|
}
|
2010-04-26 06:05:10 +02:00
|
|
|
|
|
|
|
ScopedDbConnection()
|
2010-04-29 16:15:05 +02:00
|
|
|
: _host( "" ) , _conn(0) {
|
2010-04-26 06:05:10 +02:00
|
|
|
}
|
2010-04-19 22:55:16 +02:00
|
|
|
|
2009-02-09 23:17:40 +01:00
|
|
|
/** throws UserException if can't connect */
|
2010-04-26 06:05:10 +02:00
|
|
|
ScopedDbConnection(const string& host)
|
|
|
|
: _host(host), _conn( pool.get(host) ) {
|
2009-03-30 15:47:58 +02:00
|
|
|
}
|
2010-04-29 16:15:05 +02:00
|
|
|
|
2010-04-26 06:05:10 +02:00
|
|
|
ScopedDbConnection(const string& host, DBClientBase* conn )
|
|
|
|
: _host( host ) , _conn( conn ){
|
|
|
|
}
|
|
|
|
|
2010-04-29 16:15:05 +02:00
|
|
|
string getHost() const { return _host; }
|
|
|
|
|
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
|
|
|
*/
|
2010-04-26 06:05:10 +02:00
|
|
|
pool.release(_host, _conn);
|
2009-01-15 16:17:11 +01:00
|
|
|
_conn = 0;
|
|
|
|
}
|
2010-03-22 16:47:37 +01:00
|
|
|
|
2010-04-26 06:05:10 +02:00
|
|
|
ScopedDbConnection * steal();
|
|
|
|
|
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
|
2010-04-23 23:52:10 +02:00
|
|
|
|
|
|
|
#include "undef_macros.h"
|