aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/SessionTracker.h
blob: 0827213aeed175f2745afae30bf5766efed25ed8 (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
#pragma once

#include "ClientInfo.h"

#include <list>
#include <map>
#include <memory>
#include <mutex>

namespace DB
{

struct SessionInfo
{
    const String session_id;
};

using SessionInfos = std::list<SessionInfo>;

using SessionsForUser = std::unordered_map<UUID, SessionInfos>;

class SessionTracker;

class SessionTracker
{
public:
    class Session : boost::noncopyable
    {
    public:
        explicit Session(SessionTracker & tracker_,
                         const UUID & user_id_,
                         SessionInfos::const_iterator session_info_iter_) noexcept;

        ~Session();

    private:
        friend class SessionTracker;

        SessionTracker & tracker;
        const UUID user_id;
        const SessionInfos::const_iterator session_info_iter;
    };

    using SessionTrackerHandle = std::unique_ptr<SessionTracker::Session>;

    SessionTrackerHandle trackSession(const UUID & user_id,
                                      const SessionInfo & session_info,
                                      size_t max_sessions_for_user);

private:
    /// disallow manual messing with session tracking
    friend class Session;

    std::mutex mutex;
    SessionsForUser sessions_for_user TSA_GUARDED_BY(mutex);

    void stopTracking(const UUID& user_id, SessionInfos::const_iterator session_info_iter);
};

}