@@ -230,6 +230,10 @@ class Connection(BaseConnection):
230230 Set to true to check the server's identity.
231231 tls_sni_servername: str, optional
232232 Set server host name for TLS connection
233+ socket_options: Dict[int, Dict[int, any]], optional
234+ A dictionary of socket options to set on the connection.
235+ The keys are the socket level constants (e.g., socket.SOL_SOCKET),
236+ and the values are dictionaries mapping option names to values.
233237 read_default_group : str, optional
234238 Group to read from in the configuration file.
235239 autocommit : bool, optional
@@ -341,6 +345,7 @@ def __init__( # noqa: C901
341345 ssl_verify_cert = None ,
342346 ssl_verify_identity = None ,
343347 tls_sni_servername = None ,
348+ socket_options = {},
344349 parse_json = True ,
345350 invalid_values = None ,
346351 pure_python = None ,
@@ -477,7 +482,7 @@ def _config(key, arg):
477482 self .collation = collation
478483 self .use_unicode = use_unicode
479484 self .encoding_errors = encoding_errors
480-
485+ self . socket_options = socket_options
481486 self .encoding = charset_by_name (self .charset ).encoding
482487
483488 client_flag |= CLIENT .CAPABILITIES
@@ -1110,9 +1115,10 @@ def connect(self, sock=None):
11101115
11111116 # setting TCP keepalive for mysql
11121117 # 60s idle, 30s interval, 5 times before close
1113- sock .setsockopt (socket .IPPROTO_TCP , socket .TCP_KEEPIDLE , 60 )
1114- sock .setsockopt (socket .IPPROTO_TCP , socket .TCP_KEEPINTVL , 30 )
1115- sock .setsockopt (socket .IPPROTO_TCP , socket .TCP_KEEPCNT , 5 )
1118+ for level , options in self .socket_options .items ():
1119+ for opt , value in options .items ():
1120+ sock .setsockopt (level , opt , value )
1121+
11161122 sock .settimeout (None )
11171123
11181124 self ._sock = sock
0 commit comments