Skip to content

Commit 25e6200

Browse files
committed
WL#15591: Improve the network module
This work-log introduces an improvement to the network module in the following regard: 1. Design 2. Implementation Now the MySQLSocket interface isn't responsible for processing `send` and `receive` events, this task is delegated to a lower-level object named `Network Broker`. The latter change allows the new design to be easier to comprehend and maintain. The `Network Broker` is a component specifically designed to manage communication events. Such a component is decoupled from the MySQLSocket component, making it easy to customize and maintain as well. This improvement also fixes BUG#29115406. The `recv` method when working in compressed mode was incomplete since it did not handle the case in which the last packet after unzipping a compressed payload is incomplete. Change-Id: Icb666a304082e32b083d1f322f5cfe1fc2c4cf4d
1 parent 3fe87a1 commit 25e6200

10 files changed

+708
-431
lines changed

CHANGES.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Copyright (c) 2009, 2023, Oracle and/or its affiliates.
88
Full release notes:
99
http://dev.mysql.com/doc/relnotes/connector-python/en/
1010

11+
v8.1.0
12+
======
13+
14+
- WL#15591: Improve the network module
15+
1116
v8.0.33
1217
=======
1318

lib/mysql/connector/connection.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def _do_auth(
256256
if ssl_options is None:
257257
ssl_options = {}
258258
if not self._ssl_disabled and (client_flags & ClientFlag.SSL):
259-
packet: bytes = self._protocol.make_auth_ssl(
259+
packet = self._protocol.make_auth_ssl(
260260
charset=charset, client_flags=client_flags
261261
)
262262
self._socket.send(packet)
@@ -410,7 +410,7 @@ def _handle_mfa(self, packet: bytes) -> Optional[OkPacketType]:
410410

411411
def _auth_continue(
412412
self, auth: BaseAuthPlugin, auth_plugin: str, auth_data: bytes
413-
) -> bytearray:
413+
) -> bytes:
414414
"""Continue with the authentication."""
415415
if auth_plugin == "authentication_ldap_sasl_client":
416416
logger.debug("# auth_data: %s", auth_data)
@@ -530,7 +530,7 @@ def _auth_continue(
530530
packet = self._socket.recv()
531531
logger.debug("<< Ok packet from server: %s", packet)
532532

533-
return packet
533+
return bytes(packet)
534534

535535
def _get_connection(self) -> SocketType:
536536
"""Get connection based on configuration
@@ -568,7 +568,11 @@ def _open_connection(self) -> None:
568568
self._socket = self._get_connection()
569569
try:
570570
self._socket.open_connection()
571+
572+
# do initial handshake
571573
self._do_handshake()
574+
575+
# start authentication negotiation
572576
self._do_auth(
573577
self._user,
574578
self._password,
@@ -579,9 +583,11 @@ def _open_connection(self) -> None:
579583
self._conn_attrs,
580584
)
581585
self.set_converter_class(self._converter_class)
586+
582587
if self._client_flags & ClientFlag.COMPRESS:
583-
self._socket.recv = self._socket.recv_compressed
584-
self._socket.send = self._socket.send_compressed
588+
# update the network layer accordingly
589+
self._socket.switch_to_compressed_mode()
590+
585591
self._socket.set_connection_timeout(None)
586592
except Exception:
587593
# close socket
@@ -666,9 +672,7 @@ def _send_cmd(
666672
return None
667673
return self._socket.recv()
668674

669-
def _send_data(
670-
self, data_file: BinaryIO, send_empty_packet: bool = False
671-
) -> bytearray:
675+
def _send_data(self, data_file: BinaryIO, send_empty_packet: bool = False) -> bytes:
672676
"""Send data to the MySQL server
673677
674678
This method accepts a file-like object and sends its data
@@ -698,7 +702,7 @@ def _send_data(
698702
except AttributeError as err:
699703
raise OperationalError("MySQL Connection not available") from err
700704

701-
return self._socket.recv()
705+
return bytes(self._socket.recv())
702706

703707
def _handle_server_status(self, flags: int) -> None:
704708
"""Handle the server flags found in MySQL packets
@@ -1117,7 +1121,7 @@ def cmd_refresh(self, options: int) -> OkPacketType:
11171121
"""
11181122
return self._handle_ok(self._send_cmd(ServerCmd.REFRESH, int4store(options)))
11191123

1120-
def cmd_quit(self) -> bytearray:
1124+
def cmd_quit(self) -> bytes:
11211125
"""Close the current connection with the server
11221126
11231127
This method sends the QUIT command to the MySQL server, closing the
@@ -1130,7 +1134,7 @@ def cmd_quit(self) -> bytearray:
11301134

11311135
packet = self._protocol.make_command(ServerCmd.QUIT)
11321136
self._socket.send(packet, 0, 0)
1133-
return packet
1137+
return bytes(packet)
11341138

11351139
def cmd_shutdown(self, shutdown_type: Optional[int] = None) -> EofPacketType:
11361140
"""Shut down the MySQL Server

lib/mysql/connector/constants.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
from .charsets import MYSQL_CHARACTER_SETS, MYSQL_CHARACTER_SETS_57
3737
from .errors import ProgrammingError
3838

39-
MAX_PACKET_LENGTH: int = 16777215
4039
NET_BUFFER_LENGTH: int = 8192
4140
MAX_MYSQL_TABLE_COLUMNS: int = 4096
4241
# Flag used to send the Query Attributes with 0 (or more) parameters.

0 commit comments

Comments
 (0)