Repository navigation
Expand file tree
/
Copy pathConnectionPool.cpp
More file actions
111 lines (99 loc) · 2.98 KB
/
Copy pathConnectionPool.cpp
File metadata and controls
111 lines (99 loc) · 2.98 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
#include <Client/ConnectionPool.h>
#include <Core/Settings.h>
#include <boost/functional/hash.hpp>
namespace DB
{
namespace Setting
{
extern const SettingsMilliseconds connection_pool_max_wait_ms;
}
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;
}
}