MySQL 9.4.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
session_manager.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef ROUTER_SRC_REST_MRS_SRC_MRS_REST_SESSION_MANAGER_H_
27#define ROUTER_SRC_REST_MRS_SRC_MRS_REST_SESSION_MANAGER_H_
28
29#include <chrono>
30#include <map>
31#include <memory>
32#include <mutex>
33#include <optional>
34#include <type_traits>
35#include <vector>
36
40
41namespace mrs {
42
43// The following timeout constants are expressed in minutes.
44const uint64_t k_maximum_expire_timeout{43200};
45const uint64_t k_maximum_inactivity_timeout{43200};
46const uint64_t k_default_expire_timeout{15};
47
49
50namespace http {
51
53 public:
55 using SessionId = std::string;
56 using system_clock = std::chrono::system_clock;
60
62 public:
64 std::optional<minutes> inactivity_timeout{};
65
68 };
69
71
72 class Session {
73 public:
75 public:
76 virtual ~SessionData() = default;
78 };
79
80 enum State {
86 };
87
88 public:
90 const AuthorizationHandlerId &authorization,
91 const std::string &holder_name);
92
93 template <typename Derived>
94 Derived *get_data() const {
96
97 return dynamic_cast<Derived *>(data_.get());
98 }
99
100 void set_data(SessionData *data) {
101 data_.reset(data);
102 data_->internal_session = this;
103 }
104
105 void set_data(std::unique_ptr<SessionData> &&data) {
106 data_ = std::move(data);
107 data_->internal_session = this;
108 }
109
112 }
113
114 const SessionId &get_session_id() const { return id_; }
115
116 const std::string &get_holder_name() const { return holder_name_; }
117
118 system_clock::time_point get_access_time() const { return access_time_; }
119 system_clock::time_point get_create_time() const { return create_time_; }
120
121 system_clock::time_point update_access_time() {
122 return access_time_ = system_clock::now();
123 }
124
125 bool has_access_timeout(system_clock::duration timeout) const {
126 return access_time_ + timeout <= system_clock::now();
127 }
128
129 bool is_expired(system_clock::duration timeout) const {
130 return create_time_ + timeout <= system_clock::now();
131 }
132
133 void enable_db_session_pool(uint32_t passthrough_pool_size);
134
136 bool generate_token{false};
138 std::optional<std::string> users_on_complete_url_redirection;
140 std::string handler_name;
143 std::string proto;
144 std::string host;
145
146 std::shared_ptr<collector::MysqlFixedPoolManager> db_session_pool;
147
148 private:
149 friend class SessionManager;
150 std::unique_ptr<SessionData> data_;
152 system_clock::time_point access_time_;
153 system_clock::time_point create_time_;
155 std::string holder_name_;
156 };
157
158 using SessionPtr = std::shared_ptr<Session>;
159
160 public:
162
163 void configure(const Configuration &config);
164 const Configuration &configuration() const { return config_; }
165
169 const std::string &holder_name);
170 SessionPtr new_session(const SessionId &session_id);
171 bool change_session_id(SessionPtr session, const SessionId &new_session_id);
172
173 template <class Generator>
174 void set_unique_session_secondary_id(Session *session, const Generator &g) {
175 std::lock_guard<std::mutex> lck{mutex_};
176 std::string id;
177 do {
178 id = g();
180
181 session->handler_secondary_id = id;
182 }
183
184 void remove_session(const Session::SessionData *session_data);
185 bool remove_session(const SessionPtr &session);
186 bool remove_session(const SessionId session);
187 void remove_timeouted();
188
189 std::function<void(const SessionPtr &)> on_session_delete;
190
191 private:
192 bool remove_session_impl(const Session *session);
193 // Methods with postfix "_impl" at end of method name, marks that the methods
194 // doesn't use mutexes, thus it should be used after locking `mutex_` object.
197
200 void remove_inactive_impl(const system_clock::time_point &now);
201 void remove_expired_impl(const system_clock::time_point &now);
202
203 std::vector<SessionPtr> sessions_;
204 std::mutex mutex_;
205 system_clock::time_point oldest_inactive_session_;
206 system_clock::time_point oldest_session_;
208};
209
210} // namespace http
211} // namespace mrs
212
213#endif // ROUTER_SRC_REST_MRS_SRC_MRS_REST_SESSION_MANAGER_H_
Definition: cache_manager.h:41
MySqlFixedCacheManagerImpl::CachedObject CachedObject
Definition: mysql_fixed_pool_manager.h:45
Definition: session_manager.h:61
minutes expire_timeout
Definition: session_manager.h:63
uint32_t max_passthrough_sessions_per_user
Definition: session_manager.h:66
std::optional< minutes > inactivity_timeout
Definition: session_manager.h:64
Definition: session_manager.h:74
Session * internal_session
Definition: session_manager.h:77
Definition: session_manager.h:72
std::unique_ptr< SessionData > data_
Definition: session_manager.h:150
Session(SessionManager *owner, const SessionId id, const AuthorizationHandlerId &authorization, const std::string &holder_name)
Definition: session_manager.cc:43
bool generate_token
Definition: session_manager.h:136
Derived * get_data() const
Definition: session_manager.h:94
std::shared_ptr< collector::MysqlFixedPoolManager > db_session_pool
Definition: session_manager.h:146
system_clock::time_point get_create_time() const
Definition: session_manager.h:119
system_clock::time_point update_access_time()
Definition: session_manager.h:121
AuthorizationHandlerId get_authorization_handler_id() const
Definition: session_manager.h:110
std::string users_on_complete_timeout
Definition: session_manager.h:139
system_clock::time_point create_time_
Definition: session_manager.h:153
std::string host
Definition: session_manager.h:144
const std::string & get_holder_name() const
Definition: session_manager.h:116
State
Definition: session_manager.h:80
@ kUserVerified
Definition: session_manager.h:85
@ kWaitingForCode
Definition: session_manager.h:82
@ kUninitialized
Definition: session_manager.h:81
@ kTokenVerified
Definition: session_manager.h:84
@ kGettingTokken
Definition: session_manager.h:83
system_clock::time_point access_time_
Definition: session_manager.h:152
void enable_db_session_pool(uint32_t passthrough_pool_size)
Definition: session_manager.cc:54
bool is_expired(system_clock::duration timeout) const
Definition: session_manager.h:129
State state
Definition: session_manager.h:137
std::string holder_name_
Definition: session_manager.h:155
SessionId id_
Definition: session_manager.h:151
const SessionId & get_session_id() const
Definition: session_manager.h:114
void set_data(SessionData *data)
Definition: session_manager.h:100
void set_data(std::unique_ptr< SessionData > &&data)
Definition: session_manager.h:105
bool has_access_timeout(system_clock::duration timeout) const
Definition: session_manager.h:125
AuthorizationHandlerId authorization_handler_id_
Definition: session_manager.h:154
std::string handler_secondary_id
Definition: session_manager.h:141
AuthUser user
Definition: session_manager.h:142
std::optional< std::string > users_on_complete_url_redirection
Definition: session_manager.h:138
system_clock::time_point get_access_time() const
Definition: session_manager.h:118
std::string handler_name
Definition: session_manager.h:140
std::string proto
Definition: session_manager.h:143
SessionManager * owner
Definition: session_manager.h:135
Definition: session_manager.h:52
system_clock::time_point oldest_session_
Definition: session_manager.h:206
std::chrono::system_clock system_clock
Definition: session_manager.h:56
std::string SessionId
Definition: session_manager.h:55
system_clock::time_point oldest_inactive_session_
Definition: session_manager.h:205
Allocation
Definition: session_manager.h:70
@ OnlyExisting
Definition: session_manager.h:70
@ CreateWhenNotExisting
Definition: session_manager.h:70
std::chrono::minutes minutes
Definition: session_manager.h:58
SessionPtr get_session_handler_specific_id_impl(const SessionId &id)
Definition: session_manager.cc:161
std::shared_ptr< Session > SessionPtr
Definition: session_manager.h:158
bool change_session_id(SessionPtr session, const SessionId &new_session_id)
Definition: session_manager.cc:87
bool remove_session_impl(const Session *session)
Definition: session_manager.cc:146
void remove_timeouted()
Definition: session_manager.cc:132
void remove_expired_impl(const system_clock::time_point &now)
Definition: session_manager.cc:226
SessionManager()
Definition: session_manager.cc:60
const Configuration & configuration() const
Definition: session_manager.h:164
void remove_session(const Session::SessionData *session_data)
Definition: session_manager.cc:137
SessionPtr new_session(const AuthorizationHandlerId id, const std::string &holder_name)
Definition: session_manager.cc:71
mrs::database::entry::UniversalId AuthorizationHandlerId
Definition: session_manager.h:57
std::mutex mutex_
Definition: session_manager.h:204
SessionPtr get_session_secondary_id(const SessionId &id)
Definition: session_manager.cc:98
void remove_timeouted_impl()
Definition: session_manager.cc:195
SessionPtr get_session_impl(const SessionId &id)
Definition: session_manager.cc:174
std::function< void(const SessionPtr &)> on_session_delete
Definition: session_manager.h:189
void set_unique_session_secondary_id(Session *session, const Generator &g)
Definition: session_manager.h:174
void remove_inactive_impl(const system_clock::time_point &now)
Definition: session_manager.cc:203
void configure(const Configuration &config)
Definition: session_manager.cc:66
mrs::database::entry::AuthUser AuthUser
Definition: session_manager.h:54
Configuration config_
Definition: session_manager.h:207
std::vector< SessionPtr > sessions_
Definition: session_manager.h:203
SessionId generate_session_id_impl()
Definition: session_manager.cc:186
SessionPtr get_session(const SessionId &id)
Definition: session_manager.cc:105
Definition: connection.h:56
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:498
std::chrono::minutes minutes
Definition: authorize_manager.cc:69
mrs::database::entry::AuthUser AuthUser
Definition: scram_handler.cc:54
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
SessionManager::SessionPtr SessionPtr
Definition: session_manager.cc:41
Definition: authorize_manager.h:48
const uint64_t k_maximum_expire_timeout
Definition: session_manager.h:44
const uint64_t k_maximum_inactivity_timeout
Definition: session_manager.h:45
mrs::database::entry::UniversalId UniversalId
Definition: universal_id.h:33
const uint32_t k_default_passthrough_max_sessions_per_user
Definition: session_manager.h:48
const uint64_t k_default_expire_timeout
Definition: session_manager.h:46
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510