-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionPool.h
107 lines (93 loc) · 2.44 KB
/
ConnectionPool.h
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
99
100
101
102
103
104
105
106
107
//
// Created by hj on 2022/9/2.
//
#ifndef RESTER_CONNECTIONPOOL_H
#define RESTER_CONNECTIONPOOL_H
#include <list>
#include "cpp_redis/cpp_redis"
class ConnectionPool
{
public:
//ConnectionPool()=delete;
ConnectionPool& operator=(const ConnectionPool&)=delete;
ConnectionPool(const ConnectionPool&)=delete;
~ConnectionPool()
{
for(auto& cli:clients_)
{
cli->shutdown();
delete cli;
}
}
static ConnectionPool& GetInstance();
static void SetSize(int size)
{
size_=size;
}
static int Size()
{
return size_;
}
cpp_redis::client* GetClient()
{
std::lock_guard<std::mutex> guard_clients(mut_clients_);
if(!clients_.empty())
{
auto cli= clients_.front();
clients_.pop_front();
return cli;
}
else
{
return nullptr;
}
}
void ReturnClient(cpp_redis::client* cli)
{
std::lock_guard<std::mutex> guard_clients(mut_clients_);
clients_.push_back(cli);
}
private:
ConnectionPool()
{
for(int i=0;i<size_;i++)
{
cpp_redis::client* client=new cpp_redis::client;
client->connect("115.156.245.91", 6379,//115.156.245.91 192.168.56.1
[](const std::string &host, std::size_t port, cpp_redis::client::connect_state status)
{
if (status == cpp_redis::client::connect_state::failed)
{
std::cout<<"connect failed: "<<(int)status<<std::endl;
exit(-20);
}
else if(status == cpp_redis::client::connect_state::ok)
{
// std::cout << "client connected to " << host << ":" << port
// <<" connection status: "<<(int)status<< std::endl;
}
});
clients_.push_back(client);
}
}
std::mutex mut_clients_;
std::list<cpp_redis::client*> clients_;
static int size_;
};
class ClientHelper
{
public:
ClientHelper(ConnectionPool& connection_pool,cpp_redis::client* cli)
:cli_(cli),
connection_pool_(connection_pool)
{
}
~ClientHelper()
{
connection_pool_.ReturnClient(cli_);
}
private:
cpp_redis::client* cli_;
ConnectionPool& connection_pool_;
};
#endif //RESTER_CONNECTIONPOOL_H