MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
database_pool.hpp
1#ifndef MSTL_DATABASE_DATABASE_POOL_HPP__
2#define MSTL_DATABASE_DATABASE_POOL_HPP__
3#include "../core/container/queue.hpp"
8#include "db_interface.hpp"
10
11class MSTL_API database_pool {
12private:
13 db_config config_;
14 size_t init_size_;
15 size_t max_size_;
16 size_t max_idle_time_; // s
17 size_t connect_timeout_; // ms
18
19 _MSTL unique_ptr<idb_factory> factory_ = nullptr;
20 _MSTL queue<idb_connect*> connect_queue_;
21 _MSTL mutex queue_mtx_;
22 _MSTL condition_variable cv_;
23 _MSTL atomic<bool> running_{false};
24
25 _MSTL thread produce_;
26 _MSTL thread scanner_;
27
28 void produce_connect_task();
29 void scanner_connect_task();
30
31 template <typename T>
32 shared_ptr<T> get_connect_impl();
33
34public:
35 database_pool(DB_TYPE type, const db_config& config,
36 size_t init_size = 50, size_t max_size = 1024,
37 size_t max_idle_time = 30, size_t connect_timeout = 100);
38
39 ~database_pool() { stop(); }
40
41 database_pool(const database_pool&) = delete;
42 database_pool& operator =(const database_pool&) = delete;
43 database_pool(database_pool&&) = delete;
44 database_pool& operator =(database_pool&&) = delete;
45
46 void stop();
47
48 _MSTL shared_ptr<idb_connect> get_connect() {
49 return get_connect_impl<idb_connect>();
50 }
51 _MSTL shared_ptr<idb_tb_connect> get_tb_connect() {
52 return get_connect_impl<idb_tb_connect>();
53 }
54 _MSTL shared_ptr<idb_kv_connect> get_kv_connect() {
55 return get_connect_impl<idb_kv_connect>();
56 }
57};
58
59
60template <typename T>
61shared_ptr<T> database_pool::get_connect_impl() {
63
64 while (connect_queue_.empty() && running_) {
65 if (cv_.wait_for(lock, milliseconds(connect_timeout_)) == _MSTL cv_status::timeout) {
66 if (connect_queue_.empty()) {
67 if (connect_queue_.size() < max_size_) {
68 auto* new_conn = factory_->create_connect();
69 if (new_conn != nullptr) {
70 new_conn->refresh_alive();
71 connect_queue_.push(new_conn);
72 continue;
73 }
74 }
75 return nullptr;
76 }
77 }
78 }
79
80 idb_connect* raw_conn = connect_queue_.front();
81 connect_queue_.pop();
82
83 if (!raw_conn->is_valid()) {
84 try {
85 if (!raw_conn->reset_connect(config_)) {
86 delete raw_conn;
87 raw_conn = factory_->create_connect();
88 if (raw_conn == nullptr) {
89 cv_.notify_all();
90 return nullptr;
91 }
92 }
93 } catch (...) {
94 delete raw_conn;
95 cv_.notify_all();
96 return nullptr;
97 }
98 }
99
100 shared_ptr<T> conn_ptr {
101 dynamic_cast<T*>(raw_conn),
102 [this](T* p) {
103 _MSTL lock<_MSTL mutex> lock1(queue_mtx_);
104 if (p->is_valid()) {
105 p->refresh_alive();
106 connect_queue_.push(p);
107 }
108 else {
109 delete p;
110 }
111 cv_.notify_all();
112 }
113 };
114
115 cv_.notify_all();
116 return conn_ptr;
117}
118
120#endif // MSTL_DATABASE_DATABASE_POOL_HPP__
MSTL原子类型完整实现
锁管理器模板
共享智能指针类模板
MSTL条件变量行为
duration< int64_t, milli > milliseconds
毫秒持续时间
lock< Mutex, true > smart_lock
智能锁管理器的便捷类型别名
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
MSTL共享智能指针实现
MSTL线程支持