MySQL 9.2.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
connection_delay.h
Go to the documentation of this file.
1/* Copyright (c) 2024, 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 opt_connection_control *sys_vars,
47 size_t sys_vars_size,
49 size_t status_vars_size, mysql_rwlock_t *lock);
50
51 /** Destructor */
53 deinit();
54 m_lock = nullptr;
55 }
56
57 void init(Connection_event_coordinator *coordinator);
58
59 /**
60 Set threshold value.
61
62 @param threshold [in] New threshold value
63 */
64
65 void set_threshold(int64 threshold) { m_threshold = threshold; }
66
67 /** Get threshold value */
68 int64 get_threshold() const { return m_threshold; }
69
70 /**
71 Set min/max delay
72
73 @param new_value [in] New m_min_delay/m_max_delay value
74 @param min [in] true for m_min_delay. false otherwise.
75
76 @returns whether m_min_delay/m_max_delay value was changed successfully or
77 not
78 @retval false Success
79 @retval true Failure. Invalid value specified.
80 */
81
82 bool set_delay(int64 new_value, bool min) {
83 const int64 current_max = get_max_delay();
84 const int64 current_min = get_min_delay();
85
86 if (new_value < MIN_DELAY) {
87 return true;
88 }
89 if (new_value > MAX_DELAY) {
90 return true;
91 }
92 if ((min && new_value > current_max) || (!min && new_value < current_min)) {
93 return true;
94 }
95
96 if (min) {
97 m_min_delay = new_value;
98 } else {
99 m_max_delay = new_value;
100 }
101 return false;
102 }
103
104 /** Get max value */
105 int64 get_max_delay() const { return m_max_delay; }
106
107 /** Get min value */
108 int64 get_min_delay() const { return m_min_delay; }
109
110 /** Overridden functions */
111 bool notify_event(
112 MYSQL_THD thd, Connection_event_coordinator *coordinator,
113 const mysql_event_tracking_connection_data *connection_event) override;
115 opt_connection_control variable,
116 void *new_value) override;
117
118 private:
119 void deinit();
120 void make_hash_key(MYSQL_THD thd, Sql_string &s);
121 /**
122 Generates wait time
123
124 @param count [in] Proposed delay in msec
125
126 @returns wait time
127 */
128
130 const int64 max_delay = get_max_delay();
131 const int64 min_delay = get_min_delay();
132
133 /*
134 if count < 0 (can happen in edge cases
135 we return max_delay.
136 Otherwise, following equation will be used:
137 wait_time = MIN(MAX(count, min_delay),
138 max_delay)
139 */
140 if (count < 0) {
141 return max_delay;
142 }
143 return std::min((std::max(count, min_delay)), max_delay);
144 }
146
147 /** Threshold value which triggers wait */
149 /** Lower cap on delay in msec to be generated */
151 /** Upper cap on delay in msec to be generated */
153 /** System variables */
154 std::vector<opt_connection_control, CustomAllocator<opt_connection_control>>
156 /** Status variables */
160 /** RW lock */
162};
163} // namespace connection_control
164#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
ulonglong get_wait_time(int64 count)
Generates wait time.
Definition: connection_delay.h:129
int64 m_threshold
Threshold value which triggers wait.
Definition: connection_delay.h:148
void set_threshold(int64 threshold)
Set threshold value.
Definition: connection_delay.h:65
mysql_rwlock_t * m_lock
RW lock.
Definition: connection_delay.h:161
void make_hash_key(MYSQL_THD thd, Sql_string &s)
Create hash key of the format 'user'@'host'.
Definition: connection_delay.cc:102
void conditional_wait(ulonglong wait_time)
Wait till the wait_time expires or thread is killed.
Definition: connection_delay.cc:159
int64 get_min_delay() const
Get min value.
Definition: connection_delay.h:108
int64 get_threshold() const
Get threshold value.
Definition: connection_delay.h:68
int64 m_max_delay
Upper cap on delay in msec to be generated.
Definition: connection_delay.h:152
bool set_delay(int64 new_value, bool min)
Set min/max delay.
Definition: connection_delay.h:82
void init(Connection_event_coordinator *coordinator)
Subscribe with coordinator for connection events.
Definition: connection_delay.cc:362
void deinit()
Clear data from Connection_delay_action.
Definition: connection_delay.cc:379
int64 get_max_delay() const
Get max value.
Definition: connection_delay.h:105
int64 m_min_delay
Lower cap on delay in msec to be generated.
Definition: connection_delay.h:150
~Connection_delay_action() override
Destructor.
Definition: connection_delay.h:52
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:226
std::vector< stats_connection_control, CustomAllocator< stats_connection_control > > m_stats_vars
Status variables.
Definition: connection_delay.h:159
Connection_delay_action(int64 threshold, int64 min_delay, int64 max_delay, 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:74
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:317
std::vector< opt_connection_control, CustomAllocator< opt_connection_control > > m_sys_vars
System variables.
Definition: connection_delay.h:155
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:20
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:46
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:214
SHOW_VAR status_vars[]
Definition: mysqld.cc:11615
Definition: connection_control.h:70
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
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2876
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