MySQL 9.5.0
Source Code Documentation
connection_delay.h
Go to the documentation of this file.
1/* Copyright (c) 2024, 2025, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef CONNECTION_DELAY_H
25#define CONNECTION_DELAY_H
26
27#include <my_hostname.h> /* HOSTNAME_LENGTH */
28#include <my_inttypes.h>
31#include <algorithm>
32#include "connection_control_data.h" /* variables and status */
33#include "connection_control_interfaces.h" /* Observer interface */
35#include "connection_delay_api.h" /* Constants */
36
37namespace connection_control {
38/**
39 Connection event action to enforce max failed login constraint
40*/
41
44 public:
45 Connection_delay_action(int64 threshold, int64 min_delay, int64 max_delay,
46 bool exempt_unknown_users,
47 opt_connection_control *sys_vars,
48 size_t sys_vars_size,
50 size_t status_vars_size, mysql_rwlock_t *lock);
51
52 /** Destructor */
54 deinit();
55 m_lock = nullptr;
56 }
57
58 void init(Connection_event_coordinator *coordinator);
59
60 /**
61 Set threshold value.
62
63 @param threshold [in] New threshold value
64 */
65
66 void set_threshold(int64 threshold) { m_threshold = threshold; }
67
68 /** Get threshold value */
69 int64 get_threshold() const { return m_threshold; }
70
71 /**
72 Set min/max delay
73
74 @param new_value [in] New m_min_delay/m_max_delay value
75 @param min [in] true for m_min_delay. false otherwise.
76
77 @returns whether m_min_delay/m_max_delay value was changed successfully or
78 not
79 @retval false Success
80 @retval true Failure. Invalid value specified.
81 */
82
83 bool set_delay(int64 new_value, bool min) {
84 const int64 current_max = get_max_delay();
85 const int64 current_min = get_min_delay();
86
87 if (new_value < MIN_DELAY) {
88 return true;
89 }
90 if (new_value > MAX_DELAY) {
91 return true;
92 }
93 if ((min && new_value > current_max) || (!min && new_value < current_min)) {
94 return true;
95 }
96
97 if (min) {
98 m_min_delay = new_value;
99 } else {
100 m_max_delay = new_value;
101 }
102 return false;
103 }
104
105 /** Get max value */
106 int64 get_max_delay() const { return m_max_delay; }
107
108 /** Get min value */
109 int64 get_min_delay() const { return m_min_delay; }
110
111 /**
112 Set if component should exempt un-authenticated connections
113 from connection control.
114
115 @param new_value [in] New value
116 */
117 void set_exempt_unknown_users(bool new_value) {
118 m_exempt_unknown_users = new_value;
119 }
120
121 /** Get exempt_unknown_users flag */
122 [[nodiscard]] bool get_exempt_unknown_users() const {
124 }
125
126 /** Overridden functions */
127 bool notify_event(
128 MYSQL_THD thd, Connection_event_coordinator *coordinator,
129 const mysql_event_tracking_connection_data *connection_event) override;
131 opt_connection_control variable,
132 void *new_value) override;
133
134 private:
135 void deinit();
136 void make_hash_key(MYSQL_THD thd, Sql_string &s);
138 /**
139 Generates wait time
140
141 @param count [in] Proposed delay in msec
142
143 @returns wait time
144 */
145
147 const int64 max_delay = get_max_delay();
148 const int64 min_delay = get_min_delay();
149
150 /*
151 if count < 0 (can happen in edge cases
152 we return max_delay.
153 Otherwise, following equation will be used:
154 wait_time = MIN(MAX(count, min_delay),
155 max_delay)
156 */
157 if (count < 0) {
158 return max_delay;
159 }
160 return std::min((std::max(count, min_delay)), max_delay);
161 }
163
164 /** Threshold value which triggers wait */
166 /** Lower cap on delay in msec to be generated */
168 /** Upper cap on delay in msec to be generated */
170 /** Do not apply delays for failing unauthenticated TCP connections */
172 /** System variables */
173 std::vector<opt_connection_control, CustomAllocator<opt_connection_control>>
175 /** Status variables */
179 /** RW lock */
181};
182} // namespace connection_control
183#endif /* !CONNECTION_DELAY_H */
#define MYSQL_THD
Definition: backup_page_tracker.h:38
Definition: connection_control_memory.h:41
Connection event action to enforce max failed login constraint.
Definition: connection_delay.h:43
void get_priv_account(MYSQL_THD thd, Sql_string &s)
ulonglong get_wait_time(int64 count) const
Generates wait time.
Definition: connection_delay.h:146
int64 m_threshold
Threshold value which triggers wait.
Definition: connection_delay.h:165
void set_threshold(int64 threshold)
Set threshold value.
Definition: connection_delay.h:66
mysql_rwlock_t * m_lock
RW lock.
Definition: connection_delay.h:180
Connection_delay_action(int64 threshold, int64 min_delay, int64 max_delay, bool exempt_unknown_users, opt_connection_control *sys_vars, size_t sys_vars_size, stats_connection_control *status_vars, size_t status_vars_size, mysql_rwlock_t *lock)
Connection_delay_action Constructor.
Definition: connection_delay.cc:75
void make_hash_key(MYSQL_THD thd, Sql_string &s)
Create hash key of the format 'user'@'host'.
Definition: connection_delay.cc:104
void conditional_wait(ulonglong wait_time)
Wait till the wait_time expires or thread is killed.
Definition: connection_delay.cc:161
bool m_exempt_unknown_users
Do not apply delays for failing unauthenticated TCP connections.
Definition: connection_delay.h:171
void set_exempt_unknown_users(bool new_value)
Set if component should exempt un-authenticated connections from connection control.
Definition: connection_delay.h:117
int64 get_min_delay() const
Get min value.
Definition: connection_delay.h:109
int64 get_threshold() const
Get threshold value.
Definition: connection_delay.h:69
int64 m_max_delay
Upper cap on delay in msec to be generated.
Definition: connection_delay.h:169
bool set_delay(int64 new_value, bool min)
Set min/max delay.
Definition: connection_delay.h:83
void init(Connection_event_coordinator *coordinator)
Subscribe with coordinator for connection events.
Definition: connection_delay.cc:405
void deinit()
Clear data from Connection_delay_action.
Definition: connection_delay.cc:422
int64 get_max_delay() const
Get max value.
Definition: connection_delay.h:106
int64 m_min_delay
Lower cap on delay in msec to be generated.
Definition: connection_delay.h:167
~Connection_delay_action() override
Destructor.
Definition: connection_delay.h:53
bool get_exempt_unknown_users() const
Get exempt_unknown_users flag.
Definition: connection_delay.h:122
bool notify_event(MYSQL_THD thd, Connection_event_coordinator *coordinator, const mysql_event_tracking_connection_data *connection_event) override
Overridden functions.
Definition: connection_delay.cc:250
std::vector< stats_connection_control, CustomAllocator< stats_connection_control > > m_stats_vars
Status variables.
Definition: connection_delay.h:178
bool notify_sys_var(Connection_event_coordinator *coordinator, opt_connection_control variable, void *new_value) override
Notification of a change in system variable value.
Definition: connection_delay.cc:355
std::vector< opt_connection_control, CustomAllocator< opt_connection_control > > m_sys_vars
System variables.
Definition: connection_delay.h:174
Connection event coordinator.
Definition: connection_control_coordinator.h:65
Interface for defining action on connection events.
Definition: connection_control_interfaces.h:42
Definition: connection_control_pfs_table.h:39
opt_connection_control
Enum for system variables : Must be in sync with members of Connection_control_variables.
Definition: connection_control_data.h:35
stats_connection_control
Enum for status variables : Must be in sync with members of Connection_control_statistics.
Definition: connection_control_data.h:47
Common definition used by mysys, performance schema and server & client.
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
int64_t int64
Definition: my_inttypes.h:68
static int count
Definition: myisam_ftdump.cc:45
static int wait_time
Definition: mysql.cc:217
SHOW_VAR status_vars[]
Definition: mysqld.cc:11655
Definition: connection_control.h:66
const int64 MIN_DELAY
Definition: connection_delay.cc:43
std::string Sql_string
Definition: connection_control_interfaces.h:34
const int64 MAX_DELAY
Definition: connection_delay.cc:44
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
ValueType max(X &&first)
Definition: gtid.h:103
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2880
Plugin service that provides access to the parser and some operations on the parse tree.
Structure for Connection event tracking.
Definition: event_tracking_connection_defs.h:60
An instrumented rwlock structure.
Definition: mysql_rwlock_bits.h:51