From 45d9d8db480c712d562900de3358788a8b12beff Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 31 Oct 2008 19:17:54 -0500 Subject: move connpool to client folder --- client/connpool.cpp | 46 +++++++++++++++++++++++++++ client/connpool.h | 78 ++++++++++++++++++++++++++++++++++++++++++++++ db/makefile | 2 +- dbgrid/connpool.cpp | 45 -------------------------- dbgrid/connpool.h | 73 ------------------------------------------- dbgrid/dbgrid.cpp | 2 +- dbgrid/dbgrid.vcproj | 36 +++++++++++---------- dbgrid/dbgrid_commands.cpp | 2 +- dbgrid/gridconfig.cpp | 2 +- dbgrid/griddb.cpp | 2 +- dbgrid/request.cpp | 2 +- 11 files changed, 150 insertions(+), 140 deletions(-) create mode 100644 client/connpool.cpp create mode 100644 client/connpool.h delete mode 100644 dbgrid/connpool.cpp delete mode 100644 dbgrid/connpool.h 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 . +*/ + +// _ 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 . +*/ + +#pragma once + +#include +#include "dbclient.h" + +struct PoolForHost { + queue pool; +}; + +class DBConnectionPool { + boost::mutex poolMutex; + map 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; + } + } +}; diff --git a/db/makefile b/db/makefile index 089e85f680a..377adb4ba74 100644 --- a/db/makefile +++ b/db/makefile @@ -13,7 +13,7 @@ JVM_LIBS = -L/opt/java/lib/ OBJS=../stdafx.o ../util/sock.o ../grid/message.o ../util/mmap.o pdfile.o query.o jsobj.o introspect.o btree.o clientcursor.o ../util/util.o javajs.o tests.o json.o repl.o ../client/dbclient.o btreecursor.o cloner.o namespace.o commands.o matcher.o dbcommands.o dbeval.o -DBGRID_OBJS=../stdafx.o json.o ../util/sock.o ../grid/message.o ../util/util.o jsobj.o ../client/dbclient.o ../dbgrid/dbgrid.o ../dbgrid/request.o ../dbgrid/connpool.o ../dbgrid/gridconfig.o commands.o ../dbgrid/dbgrid_commands.o ../dbgrid/griddb.o ../client/model.o +DBGRID_OBJS=../stdafx.o json.o ../util/sock.o ../grid/message.o ../util/util.o jsobj.o ../client/dbclient.o ../dbgrid/dbgrid.o ../dbgrid/request.o ../client/connpool.o ../dbgrid/gridconfig.o commands.o ../dbgrid/dbgrid_commands.o ../dbgrid/griddb.o ../client/model.o GPP = g++ diff --git a/dbgrid/connpool.cpp b/dbgrid/connpool.cpp deleted file mode 100644 index eed74bfe52e..00000000000 --- a/dbgrid/connpool.cpp +++ /dev/null @@ -1,45 +0,0 @@ -// 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 . -*/ - -// _ 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/dbgrid/connpool.h b/dbgrid/connpool.h deleted file mode 100644 index de79781128b..00000000000 --- a/dbgrid/connpool.h +++ /dev/null @@ -1,73 +0,0 @@ -/* 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 . -*/ - -#pragma once - -#include -#include "../client/dbclient.h" - -struct PoolForHost { - queue pool; -}; - -class DBConnectionPool { - boost::mutex poolMutex; - map pools; -public: - DBClientConnection *get(const string& host); - void release(const string& host, DBClientConnection *c) { - boostlock L(poolMutex); - pools[host]->pool.push(c); - } -}; - -extern DBConnectionPool pool; - -/* create these to get a connection from the pool. then 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) ) { } - - 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. - */ - cout << "~ScopedDBConnection: _conn != null\n"; - delete _conn; - } - } -}; diff --git a/dbgrid/dbgrid.cpp b/dbgrid/dbgrid.cpp index ae5cde2eb04..e5cccc114c0 100644 --- a/dbgrid/dbgrid.cpp +++ b/dbgrid/dbgrid.cpp @@ -19,7 +19,7 @@ #include "stdafx.h" #include "../grid/message.h" #include "../util/unittest.h" -#include "connpool.h" +#include "../client/connpool.h" #include "gridconfig.h" const char *curNs = ""; diff --git a/dbgrid/dbgrid.vcproj b/dbgrid/dbgrid.vcproj index ec177196f2f..a734f114311 100644 --- a/dbgrid/dbgrid.vcproj +++ b/dbgrid/dbgrid.vcproj @@ -174,10 +174,6 @@ Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx" UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}" > - - @@ -204,18 +200,6 @@ Filter="h;hpp;hxx;hm;inl;inc;xsd" UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}" > - - - - - - @@ -244,6 +228,18 @@ RelativePath="..\stdafx.h" > + + + + + + + + + + diff --git a/dbgrid/dbgrid_commands.cpp b/dbgrid/dbgrid_commands.cpp index 2a1e9b898c7..74464b0851a 100644 --- a/dbgrid/dbgrid_commands.cpp +++ b/dbgrid/dbgrid_commands.cpp @@ -32,7 +32,7 @@ #include "stdafx.h" #include "../grid/message.h" #include "../db/dbmessage.h" -#include "connpool.h" +#include "../client/connpool.h" #include "../db/commands.h" #include "gridconfig.h" diff --git a/dbgrid/gridconfig.cpp b/dbgrid/gridconfig.cpp index b1cc0981827..da915161386 100644 --- a/dbgrid/gridconfig.cpp +++ b/dbgrid/gridconfig.cpp @@ -20,7 +20,7 @@ #include "../grid/message.h" #include "../util/unittest.h" #include "database.h" -#include "connpool.h" +#include "../client/connpool.h" #include "../db/pdfile.h" #include "gridconfig.h" #include "../client/model.h" diff --git a/dbgrid/griddb.cpp b/dbgrid/griddb.cpp index 8353883936b..95435577db9 100644 --- a/dbgrid/griddb.cpp +++ b/dbgrid/griddb.cpp @@ -20,7 +20,7 @@ #include "../grid/message.h" #include "../util/unittest.h" #include "database.h" -#include "connpool.h" +#include "../client/connpool.h" #include "../db/pdfile.h" #include "gridconfig.h" #include "../client/model.h" diff --git a/dbgrid/request.cpp b/dbgrid/request.cpp index 9a2cda7f95a..79082c40bd3 100644 --- a/dbgrid/request.cpp +++ b/dbgrid/request.cpp @@ -32,7 +32,7 @@ #include "stdafx.h" #include "../grid/message.h" #include "../db/dbmessage.h" -#include "connpool.h" +#include "../client/connpool.h" const char *tempHost = "localhost:27018"; -- cgit v1.2.1