PageRenderTime 32ms CodeModel.GetById 21ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause
  1. #ifndef __MORDOR_PQ_CONNECTIONPOOL_H__
  2. #define __MORDOR_PQ_CONNECTIONPOOL_H__
  3. #include <list>
  4. #include <boost/shared_ptr.hpp>
  5. #include "mordor/fibersynchronization.h"
  6. namespace Mordor {
  7. class IOManager;
  8. namespace PQ {
  9. class Connection;
  10. class ConnectionPool : boost::noncopyable {
  11. public:
  12. typedef boost::shared_ptr<ConnectionPool> ptr;
  13. /// Constructor of ConnectionPool
  14. /// @param conninfo db connect string
  15. /// @param iomanager IO manager
  16. /// @param size pool size, 5 by default
  17. /// @param idleTolerance re-connect interval(in us) for idle
  18. /// connection, disable re-connect by setting it to 0
  19. ConnectionPool(const std::string &conninfo, IOManager *iomanager,
  20. size_t size = 5, unsigned long long idleTolerance = 0);
  21. ~ConnectionPool();
  22. boost::shared_ptr<Connection> getConnection();
  23. void resize(size_t num);
  24. private:
  25. void releaseConnection(Mordor::PQ::Connection* conn);
  26. bool connectionExpired(unsigned long long freeTime) const;
  27. private:
  28. std::list<boost::shared_ptr<Mordor::PQ::Connection> > m_busyConnections;
  29. /// @note element of the free list is pair of connection and a timestamp, the
  30. /// timestamp indicates that when the connection was put into the free list.
  31. // Before re-using any connection in the free list, should use the timestamp
  32. // to calculate wheter the connection has been idle for too long, if so should
  33. // re-connect it.
  34. std::list<std::pair<
  35. boost::shared_ptr<Mordor::PQ::Connection>, unsigned long long> > m_freeConnections;
  36. std::string m_conninfo;
  37. IOManager *m_iomanager;
  38. FiberMutex m_mutex;
  39. FiberCondition m_condition;
  40. size_t m_total;
  41. unsigned long long m_idleTolerance;
  42. };
  43. }}
  44. #endif