aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Server/waitServersToFinish.cpp
blob: 3b07c0820672bcf83b9fe8f361c091078540e917 (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
#include <Server/waitServersToFinish.h>
#include <Server/ProtocolServerAdapter.h>
#include <base/sleep.h>

namespace DB
{

size_t waitServersToFinish(std::vector<DB::ProtocolServerAdapter> & servers, std::mutex & mutex, size_t seconds_to_wait)
{
    const size_t sleep_max_ms = 1000 * seconds_to_wait;
    const size_t sleep_one_ms = 100;
    size_t sleep_current_ms = 0;
    size_t current_connections = 0;
    for (;;)
    {
        current_connections = 0;

        {
            std::scoped_lock lock{mutex};
            for (auto & server : servers)
            {
                server.stop();
                current_connections += server.currentConnections();
            }
        }

        if (!current_connections)
            break;

        sleep_current_ms += sleep_one_ms;
        if (sleep_current_ms < sleep_max_ms)
            sleepForMilliseconds(sleep_one_ms);
        else
            break;
    }
    return current_connections;
}

}