PageRenderTime 182ms CodeModel.GetById 100ms app.highlight 4ms RepoModel.GetById 77ms app.codeStats 0ms

/mordor/pq/connectionpool.h

http://github.com/mozy/mordor
C Header | 58 lines | 33 code | 14 blank | 11 comment | 0 complexity | e6300f7e850a458cb3e9b95bd1e62935 MD5 | raw file
 1#ifndef __MORDOR_PQ_CONNECTIONPOOL_H__
 2#define __MORDOR_PQ_CONNECTIONPOOL_H__
 3
 4#include <list>
 5
 6#include <boost/shared_ptr.hpp>
 7
 8#include "mordor/fibersynchronization.h"
 9
10namespace Mordor {
11
12class IOManager;
13
14namespace PQ {
15
16class Connection;
17
18class ConnectionPool : boost::noncopyable {
19public:
20    typedef boost::shared_ptr<ConnectionPool> ptr;
21
22    /// Constructor of ConnectionPool
23    /// @param conninfo       db connect string
24    /// @param iomanager      IO manager
25    /// @param size           pool size, 5 by default
26    /// @param idleTolerance  re-connect interval(in us) for idle
27    ///                           connection, disable re-connect by setting it to 0
28    ConnectionPool(const std::string &conninfo, IOManager *iomanager,
29        size_t size = 5, unsigned long long idleTolerance = 0);
30    ~ConnectionPool();
31    boost::shared_ptr<Connection> getConnection();
32    void resize(size_t num);
33
34private:
35    void releaseConnection(Mordor::PQ::Connection* conn);
36    bool connectionExpired(unsigned long long freeTime) const;
37
38private:
39    std::list<boost::shared_ptr<Mordor::PQ::Connection> > m_busyConnections;
40
41    /// @note element of the free list is pair of connection and a timestamp, the
42    ///       timestamp indicates that when the connection was put into the free list.
43    //        Before re-using any connection in the free list, should use the timestamp
44    //        to calculate wheter the connection has been idle for too long, if so should
45    //        re-connect it.
46    std::list<std::pair<
47        boost::shared_ptr<Mordor::PQ::Connection>, unsigned long long> > m_freeConnections;
48    std::string m_conninfo;
49    IOManager *m_iomanager;
50    FiberMutex m_mutex;
51    FiberCondition m_condition;
52    size_t m_total;
53    unsigned long long m_idleTolerance;
54};
55
56}}
57
58#endif