summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorunknown <Administrator@.(none)>2008-10-31 19:17:54 -0500
committerunknown <Administrator@.(none)>2008-10-31 19:17:54 -0500
commit45d9d8db480c712d562900de3358788a8b12beff (patch)
tree4e83d066c1f1e55a794ce45f86f4469e5a7698ab /client
parentac9bd9ce6020142548432b006d500dc70f8cfee2 (diff)
downloadmongo-45d9d8db480c712d562900de3358788a8b12beff.tar.gz
move connpool to client folder
Diffstat (limited to 'client')
-rw-r--r--client/connpool.cpp46
-rw-r--r--client/connpool.h78
2 files changed, 124 insertions, 0 deletions
diff --git a/client/connpool.cpp b/client/connpool.cpp
new file mode 100644
index 00000000000..d997aa49e2d
--- /dev/null
+++ b/client/connpool.cpp
@@ -0,0 +1,46 @@
+/* connpool.cpp
+*/
+
+/**
+* Copyright (C) 2008 10gen Inc.
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License, version 3,
+* as published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+// _ todo: reconnect?
+
+#include "stdafx.h"
+#include "connpool.h"
+
+DBConnectionPool pool;
+
+DBClientConnection* DBConnectionPool::get(const string& host) {
+ boostlock L(poolMutex);
+
+ PoolForHost *&p = pools[host];
+ if( p == 0 )
+ p = new PoolForHost();
+ if( p->pool.empty() ) {
+ string errmsg;
+ DBClientConnection *c = new DBClientConnection();
+ if( !c->connect(host.c_str(), errmsg) ) {
+ delete c;
+ uassert("dbconnectionpool: connect failed", false);
+ return 0;
+ }
+ return c;
+ }
+ DBClientConnection *c = p->pool.front();
+ p->pool.pop();
+ return c;
+}
diff --git a/client/connpool.h b/client/connpool.h
new file mode 100644
index 00000000000..6a4b5d92f00
--- /dev/null
+++ b/client/connpool.h
@@ -0,0 +1,78 @@
+/* connpool.h */
+
+/**
+* Copyright (C) 2008 10gen Inc.
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License, version 3,
+* as published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#pragma once
+
+#include <queue>
+#include "dbclient.h"
+
+struct PoolForHost {
+ queue<DBClientConnection*> pool;
+};
+
+class DBConnectionPool {
+ boost::mutex poolMutex;
+ map<string,PoolForHost*> pools;
+public:
+
+ /* generally, use ScopedDbConnection and do not call these directly */
+ DBClientConnection *get(const string& host);
+ void release(const string& host, DBClientConnection *c) {
+ boostlock L(poolMutex);
+ pools[host]->pool.push(c);
+ }
+};
+
+extern DBConnectionPool pool;
+
+/* Use to get a connection from the pool. On exceptions things
+ clean up nicely.
+*/
+class ScopedDbConnection {
+ const string host;
+ DBClientConnection *_conn;
+public:
+ DBClientConnection& conn() { return *_conn; }
+
+ /* throws UserAssertionAcception if can't connect */
+ ScopedDbConnection(const string& _host) :
+ host(_host), _conn( pool.get(_host) ) { }
+
+ /* Call this when you are done with the ocnnection.
+ Why? See note in the destructor below.
+ */
+ void done() {
+ if( _conn->isFailed() )
+ delete _conn;
+ else
+ pool.release(host, _conn);
+ _conn = 0;
+ }
+
+ ~ScopedDbConnection() {
+ if( _conn ) {
+ /* you are supposed to call done(). if you did that, correctly, we
+ only get here if an exception was thrown. in such a scenario, 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. The cout is just informational.
+ */
+ cout << "~ScopedDBConnection: _conn != null\n";
+ delete _conn;
+ }
+ }
+};