summaryrefslogtreecommitdiff
path: root/client/connpool.h
blob: c5c91b58519bbf067015505a91cde8ad1870dcef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/** @file 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"

namespace mongo {

    struct PoolForHost {
        std::queue<DBClientBase*> pool;
    };

    /** Database connection pool.

        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:

        {
           ScopedDbConnection c("myserver");
           c.conn()...
        }
    */
    class DBConnectionPool {
        boost::mutex poolMutex;
        map<string,PoolForHost*> pools; // servername -> pool
    public:
        DBClientBase *get(const string& host);
        void release(const string& host, DBClientBase *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;
        DBClientBase *_conn;
    public:
        /** get the associated connection object */
        DBClientBase* operator->(){ 
            uassert( "did you call done already" , _conn );
            return _conn; 
        }

        /** get the associated connection object */
        DBClientBase& conn() {
            uassert( "did you call done already" , _conn );
            return *_conn;
        }

        /** throws UserException if can't connect */
        ScopedDbConnection(const string& _host) :
                host(_host), _conn( pool.get(_host) ) { }

        /** 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.
        */
        void kill() {
            delete _conn;
            _conn = 0;
        }

        /** 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.
        */
        void done() {
            if ( ! _conn )
                return;

            /* we could do this, but instead of assume one is using autoreconnect mode on the connection 
            if ( _conn->isFailed() )
                kill();
            else
            */
                pool.release(host, _conn);
            _conn = 0;
        }

        ~ScopedDbConnection() {
            if ( _conn ) {
                /* see done() comments above for why we log this line */
                out() << "~ScopedDBConnection: _conn != null\n";
                kill();
            }
        }
    };

} // namespace mongo