连接池支持最大连接数设置

This commit is contained in:
fasiondog 2020-08-05 22:37:04 +08:00
parent f13afe26ea
commit 5a29b47c6c

View File

@ -35,7 +35,7 @@ public:
* @param maxIdleConnect 0
*/
explicit ConnectPool(const Parameter &param, size_t maxConnect = 0, size_t maxIdleConnect = 0)
: m_maxConnectSize(0),
: m_maxConnectSize(maxConnect),
m_maxIdelSize(maxIdleConnect),
m_count(0),
m_param(param),
@ -60,10 +60,10 @@ public:
/** 获取可用连接,如超出允许的最大连接数将返回空指针 */
ConnectPtr getConnect() noexcept {
std::lock_guard<std::mutex> lock(m_mutex);
if (m_maxConnectSize > 0 && m_count > m_maxConnectSize) {
return ConnectPtr();
}
if (m_connectList.empty()) {
if (m_maxConnectSize > 0 && m_count >= m_maxConnectSize) {
return ConnectPtr();
}
m_count++;
return ConnectPtr(new ConnectType(m_param), m_closer);
}