forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionPool.cpp
More file actions
158 lines (141 loc) · 4.35 KB
/
Copy pathConnectionPool.cpp
File metadata and controls
158 lines (141 loc) · 4.35 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <Client/ConnectionPool.h>
#include <Core/Settings.h>
#include <IO/WriteHelpers.h>
#include <boost/functional/hash.hpp>
namespace DB
{
namespace Setting
{
extern const SettingsMilliseconds connection_pool_max_wait_ms;
}
IConnectionPool::IConnectionPool(String host_, UInt16 port_, Priority config_priority_)
: host(host_), port(port_), address(host + ":" + toString(port_)), config_priority(config_priority_)
{
}
ConnectionPool::ConnectionPool(
unsigned max_connections_,
const String & host_,
UInt16 port_,
const String & default_database_,
const String & user_,
const String & password_,
const String & proto_send_chunked_,
const String & proto_recv_chunked_,
const String & quota_key_,
const String & cluster_,
const String & cluster_secret_,
const String & client_name_,
Protocol::Compression compression_,
Protocol::Secure secure_,
const String & bind_host_,
Priority config_priority_)
: IConnectionPool(host_, port_, config_priority_)
, Base(max_connections_, getLogger("ConnectionPool (" + host_ + ":" + toString(port_) + ")"))
, default_database(default_database_)
, user(user_)
, password(password_)
, proto_send_chunked(proto_send_chunked_)
, proto_recv_chunked(proto_recv_chunked_)
, quota_key(quota_key_)
, cluster(cluster_)
, cluster_secret(cluster_secret_)
, client_name(client_name_)
, compression(compression_)
, secure(secure_)
, bind_host(bind_host_)
{
}
std::string ConnectionPool::getDescription() const
{
return host + ":" + toString(port);
}
ConnectionPoolPtr ConnectionPoolFactory::get(
unsigned max_connections,
String host,
UInt16 port,
String default_database,
String user,
String password,
String proto_send_chunked,
String proto_recv_chunked,
String quota_key,
String cluster,
String cluster_secret,
String client_name,
Protocol::Compression compression,
Protocol::Secure secure,
String bind_host,
Priority priority)
{
Key key{
max_connections, host, port, default_database, user, password, proto_send_chunked, proto_recv_chunked, quota_key, cluster, cluster_secret, client_name, compression, secure, bind_host, priority};
std::lock_guard lock(mutex);
auto [it, inserted] = pools.emplace(key, ConnectionPoolPtr{});
if (!inserted)
if (auto res = it->second.lock())
return res;
ConnectionPoolPtr ret
{
new ConnectionPool(
max_connections,
host,
port,
default_database,
user,
password,
proto_send_chunked,
proto_recv_chunked,
quota_key,
cluster,
cluster_secret,
client_name,
compression,
secure,
bind_host,
priority),
[key, this](auto ptr)
{
{
std::lock_guard another_lock(mutex);
pools.erase(key);
}
delete ptr;
}
};
it->second = ConnectionPoolWeakPtr(ret);
return ret;
}
size_t ConnectionPoolFactory::KeyHash::operator()(const ConnectionPoolFactory::Key & k) const
{
using boost::hash_combine;
using boost::hash_value;
size_t seed = 0;
hash_combine(seed, hash_value(k.max_connections));
hash_combine(seed, hash_value(k.host));
hash_combine(seed, hash_value(k.port));
hash_combine(seed, hash_value(k.default_database));
hash_combine(seed, hash_value(k.user));
hash_combine(seed, hash_value(k.password));
hash_combine(seed, hash_value(k.cluster));
hash_combine(seed, hash_value(k.cluster_secret));
hash_combine(seed, hash_value(k.client_name));
hash_combine(seed, hash_value(k.compression));
hash_combine(seed, hash_value(k.secure));
hash_combine(seed, hash_value(k.bind_host));
hash_combine(seed, hash_value(k.priority.value));
return seed;
}
ConnectionPoolFactory & ConnectionPoolFactory::instance()
{
static ConnectionPoolFactory ret;
return ret;
}
IConnectionPool::Entry ConnectionPool::get(const DB::ConnectionTimeouts& timeouts, const DB::Settings& settings,
bool force_connected)
{
Entry entry = Base::get(settings[Setting::connection_pool_max_wait_ms].totalMilliseconds());
if (force_connected)
entry->forceConnected(timeouts);
return entry;
}
}