aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Client/ConnectionPool.cpp
blob: 5cabb1465d1a8d79ee530ddf417803b75b333950 (plain) (blame)
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
#include <Client/ConnectionPool.h>

#include <boost/functional/hash.hpp>

namespace DB
{

ConnectionPoolPtr ConnectionPoolFactory::get(
    unsigned max_connections,
    String host,
    UInt16 port,
    String default_database,
    String user,
    String password,
    String quota_key,
    String cluster,
    String cluster_secret,
    String client_name,
    Protocol::Compression compression,
    Protocol::Secure secure,
    Priority priority)
{
    Key key{
        max_connections, host, port, default_database, user, password, quota_key, cluster, cluster_secret, client_name, compression, secure, 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,
            quota_key,
            cluster,
            cluster_secret,
            client_name,
            compression,
            secure,
            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.priority.value));
    return seed;
}


ConnectionPoolFactory & ConnectionPoolFactory::instance()
{
    static ConnectionPoolFactory ret;
    return ret;
}

}