-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqlpp_pool.cpp
98 lines (78 loc) · 2.08 KB
/
mysqlpp_pool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* @author rench
* @email finyren@163.com
* @create date 2020-04-16 09:29:00
* @modify date 2020-04-16 09:29:00
* @desc [description]
*/
#include "mysqlpp_pool.h"
#include "mysqlpp_conn.h"
static const char *groups[]= {"mysql++", NULL};
mysqlpp_pool::mysqlpp_pool(struct event_base *evloop,
const std::string &host,
const int port,
const std::string &user,
const std::string &passwd,
const std::string &dbname,
int max_idle,
int max_conn)
: _evloop(evloop),
_host(host),
_port(port),
_user(user),
_passwd(passwd),
_dbname(dbname),
_max_idle(max_idle),
_max_conn(max_conn) {
}
mysqlpp_pool::~mysqlpp_pool() {
for (unsigned int i = 0; i < _conns.size(); i++) {
delete _conns[i];
}
_all = 0;
}
void mysqlpp_pool::init_library(int argc, const char **argv) {
mysqlpp_conn::init_library(argc, argv, (char **)groups);
}
mysqlpp_conn *mysqlpp_pool::get_connection() {
for (unsigned int i = 0; i < _conns.size(); i++) {
if (!_conns[i]->is_available()) {
continue;
}
_conns[i]->set_available(false);
return _conns[i];
}
_all++;
return new mysqlpp_conn(_evloop, _host, _port, _user, _passwd, _dbname, this);
}
int mysqlpp_pool::get_all_active() {
return _all;
}
int mysqlpp_pool::get_pool_active() {
int active = 0;
for (unsigned int i = 0; i < _conns.size(); i++) {
if (_conns[i]->is_available()) {
continue;
}
active++;
}
return active;
}
int mysqlpp_pool::get_available() {
int available = 0;
for (unsigned int i = 0; i < _conns.size(); i++) {
if (_conns[i]->is_available()) {
available++;
}
}
return available;
}
void mysqlpp_pool::add_connection(mysqlpp_conn *conn) {
if (_conns.size() > (unsigned int)_max_conn || !conn->_connected) {
_all--;
delete conn;
return;
}
conn->set_available(true);
_conns.push_back(conn);
}