From 75f7e8260ebe802aed8d3af364d758143c005c8c Mon Sep 17 00:00:00 2001 From: lhl <1281544429@qq.com> Date: 星期四, 07 十一月 2024 11:29:31 +0800 Subject: [PATCH] Merge branch 'master' of ssh://115.28.86.8:29418/~admin/昆仑_1024 --- Server/王琨元/code/ConnectionPool.cpp | 182 +++++++++++++++++++------------------------- 1 files changed, 79 insertions(+), 103 deletions(-) diff --git "a/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.cpp" "b/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.cpp" index ce7acb2..2fff64c 100644 --- "a/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.cpp" +++ "b/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.cpp" @@ -1,125 +1,101 @@ #include "stdafx.h" #include "ConnectionPool.h" -#include <fstream> -#include <iostream> -#include <thread> - -ConnectionPool* ConnectionPool::getInstance() { - static ConnectionPool connPool; - return &connPool; -} ConnectionPool::ConnectionPool() { - // 加载配置文件 - if (!parseJsonFile()) { - std::cout << "parseJsonFile is failed!" << std::endl; - return; - } - for ( int i = 0 ; i < m_min_conn ; i++ ) { - addConnection( ); - } - // 创建一个线程,对连接数进行监控 ,连接数不足就再继续创建 - std::thread producer(&ConnectionPool::produce, this); - // 对连接数进行监控 ,如果有太多空闲得线程 ,那么就对其进行销毁 - std::thread recycler( &ConnectionPool::recycle, this); - // 线程分离 - producer.detach(); - recycler.detach(); + + m_min_conn = 5; // 假设最小连接数为 5 + m_max_conn = 10; // 假设最大连接数为 10 + m_timeout = 1000; // 假设连接超时时间为 1000 毫秒 + max_del_time = 60000; // 假设最大空闲时间为 60000 毫秒 + + for (int i = 0; i < m_min_conn; i++) { + addConnection(); + } + // 创建一个线程,对连接数进行监控 ,连接数不足就再继续创建 + thread producer(&ConnectionPool::produce, this); + // 对连接数进行监控 ,如果有太多空闲得线程 ,那么就对其进行销毁 + thread recycler(&ConnectionPool::recycle, this); + + // 线程分离 + producer.detach(); + recycler.detach(); } -bool ConnectionPool::parseJsonFile() { - - /*std::ifstream ifs("dbconf.json"); - Reader rd; - Value root; - rd.parse(ifs, root); - if ( root.isObject( ) ) { - m_ip = root["ip"].asString(); - m_port = root["port"].asInt(); - m_userName = root["userName"].asString(); - m_passwd = root["password"].asString(); - m_db = root["dbName"].asString(); - m_min_conn = root["minSize"].asInt(); - m_max_conn = root["maxSize"].asInt(); - max_del_time = root["maxDleTime"].asInt(); - m_timeout = root["timeout"].asInt(); - return true; - }*/ - return false; -} - -void ConnectionPool::description() { - std::cout << m_ip << ". " << m_userName << ". " << m_passwd << ". " << m_db << ". " - << m_port << ". " << m_max_conn << ". " << m_min_conn << ". " << m_timeout << ". " - << max_del_time << std::endl; -} ConnectionPool::~ConnectionPool() { - while (!m_connkQueue.empty()) { - MysqlConn* conn = m_connkQueue.front(); - m_connkQueue.pop(); - delete conn; - } - + while (!m_connkQueue.empty()) { + MysqlConn* conn = m_connkQueue.front(); + m_connkQueue.pop(); + delete conn; + } } void ConnectionPool::addConnection() { - MysqlConn* conn = new MysqlConn(); - if ( !conn->connect( m_ip, m_userName, m_passwd, m_db, m_port ) ) { - std::cout << "ConnectionPool connect to mysql is failed!" << std::endl; - delete conn; - return; - } - conn->refreshActiveTime( ); - m_connkQueue.push(conn); - m_cond.notify_one(); // 唤醒一个线程 + MysqlConn* conn = new MysqlConn(); + if (!conn->isConnected()) { + cout << "ConnectionPool connect to mysql is failed!" << endl; + delete conn; + return; + } + conn->refreshActiveTime(); + m_connkQueue.push(conn); + m_cond.notify_one(); // 唤醒一个线程 } -void ConnectionPool::produce(){ - while (true) { - std::unique_lock<std::mutex> lock(m_mutex); - while (m_connkQueue.size() >= m_min_conn) { // 连接队列的数量大于最小的连接数 - m_cond.wait(lock); - } - addConnection(); - } +void ConnectionPool::produce() { + while (true) { + unique_lock<mutex> lock(m_mutex); + while (m_connkQueue.size() >= m_min_conn) { // 连接队列的数量大于最小的连接数 + m_cond.wait(lock); + } + addConnection(); + } } // 删除空闲连接 void ConnectionPool::recycle() { - while ( true ) { - std::this_thread::sleep_for( std::chrono::milliseconds(500)); // 休眠500 ms - std::unique_lock<std::mutex>lock(m_mutex); - while ( m_connkQueue.size() > m_min_conn ) { - MysqlConn* conn = m_connkQueue.front( ); - if (conn->getActiveTime() >= max_del_time) { - m_connkQueue.pop(); - delete conn; - } - else { - break; - } - } - } + while (true) { + this_thread::sleep_for(chrono::milliseconds(500)); // 休眠 500 ms + unique_lock<mutex> lock(m_mutex); + while (m_connkQueue.size() > m_min_conn) { + MysqlConn* conn = m_connkQueue.front(); + if (conn->getActiveTime() >= max_del_time) { + m_connkQueue.pop(); + delete conn; + } + else { + break; + } + } + } } -std::shared_ptr<MysqlConn> ConnectionPool::getMysqlConn() { - - std::unique_lock<std::mutex>lock(m_mutex); - while ( m_connkQueue.empty()) { - // 如果等待一段时间后,队列还是为空,返回一个 null - if ( std::cv_status::timeout == m_cond.wait_for(lock, std::chrono::milliseconds(m_timeout)) ) { - if( m_connkQueue.empty( ) ) return nullptr; - } - } - std::shared_ptr<MysqlConn> connPtr(std::move(m_connkQueue.front()), [this](MysqlConn* conn) { - std::unique_lock <std::mutex>lock(m_mutex) ; - conn->refreshActiveTime(); - m_connkQueue.push(conn); - }); - m_connkQueue.pop(); - m_cond.notify_one(); // 唤醒阻塞的生产者线程,开始生产 - return connPtr; +shared_ptr<MysqlConn> ConnectionPool::getMysqlConn() { + unique_lock<mutex> lock(m_mutex); + while (m_connkQueue.empty()) { + // 如果等待一段时间后,队列还是为空,返回一个 null + if (cv_status::timeout == m_cond.wait_for(lock, chrono::milliseconds(m_timeout))) { + if (m_connkQueue.empty()) return nullptr; + } + } + shared_ptr<MysqlConn> connPtr(move(m_connkQueue.front()), [this](MysqlConn* conn) { + unique_lock<mutex> lock(m_mutex); + conn->refreshActiveTime(); + m_connkQueue.push(conn); + }); + m_connkQueue.pop(); + m_cond.notify_one(); // 唤醒阻塞的生产者线程,开始生产 + return connPtr; +} +void ConnectionPool::description() { + cout << m_ip << ". " << m_userName << ". " << m_passwd << ". " << m_db << ". " + << m_port << ". " << m_max_conn << ". " << m_min_conn << ". " << m_timeout << ". " + << max_del_time << endl; +} + +ConnectionPool* ConnectionPool::getInstance() { + static ConnectionPool connPool; + return &connPool; } \ No newline at end of file -- Gitblit v1.8.0