Skip to content

Commit 785be45

Browse files
committed
build params
1 parent fe2855f commit 785be45

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

singlestoredb/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .utils.config import check_float # noqa: F401
1010
from .utils.config import check_int # noqa: F401
1111
from .utils.config import check_optional_bool # noqa: F401
12+
from .utils.config import check_socket_options # noqa: F401
1213
from .utils.config import check_str # noqa: F401
1314
from .utils.config import check_url # noqa: F401
1415
from .utils.config import describe_option # noqa: F401
@@ -263,6 +264,11 @@
263264
environ='SINGLESTOREDB_FUSION_ENABLED',
264265
)
265266

267+
register_option(
268+
'socket_options', 'dict', check_socket_options, None,
269+
'Format for socket options',
270+
)
271+
266272
#
267273
# Query results options
268274
#

singlestoredb/connection.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,6 @@ def connect(
13201320
ssl_ca: Optional[str] = None, ssl_disabled: Optional[bool] = None,
13211321
ssl_cipher: Optional[str] = None, ssl_verify_cert: Optional[bool] = None,
13221322
tls_sni_servername: Optional[str] = None,
1323-
socket_options: Optional[Dict[int, Dict[int, Any]]] = None,
13241323
ssl_verify_identity: Optional[bool] = None,
13251324
conv: Optional[Dict[int, Callable[..., Any]]] = None,
13261325
credential_type: Optional[str] = None,
@@ -1341,6 +1340,7 @@ def connect(
13411340
vector_data_format: Optional[str] = None,
13421341
parse_json: Optional[bool] = None,
13431342
interpolate_query_with_empty_args: Optional[bool] = None,
1343+
socket_options: Optional[Dict[int, Dict[int, Any]]] = None,
13441344
) -> Connection:
13451345
"""
13461346
Return a SingleStoreDB connection.
@@ -1429,6 +1429,11 @@ def connect(
14291429
interpolate_query_with_empty_args : bool, optional
14301430
Should the connector apply parameter interpolation even when the
14311431
parameters are empty? This corresponds to pymysql/mysqlclient's handling
1432+
socket_options : dict, optional
1433+
Socket options to set on the underlying socket. The keys should be
1434+
socket level constants (e.g., socket.SOL_SOCKET) and the values should be
1435+
dictionaries mapping socket option constants (e.g., socket.SO_KEEPALIVE) to
1436+
the desired value for that option.
14321437
14331438
Examples
14341439
--------

singlestoredb/mysql/connection.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,8 +1113,6 @@ def connect(self, sock=None):
11131113
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
11141114
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
11151115

1116-
# setting TCP keepalive for mysql
1117-
# 60s idle, 30s interval, 5 times before close
11181116
for level, options in self._socket_options.items():
11191117
for opt, value in options.items():
11201118
sock.setsockopt(level, opt, value)

singlestoredb/utils/config.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,55 @@ def check_str(
646646
return out
647647

648648

649+
def check_socket_options(
650+
value: Any,
651+
) -> Optional[Dict[int, Dict[int, Any]]]:
652+
"""
653+
Validate socket options.
654+
655+
Parameters
656+
----------
657+
value : dict
658+
The value to validate. It must be a dictionary where the keys are
659+
socket level constants (e.g., socket.SOL_SOCKET) and the values are
660+
dictionaries mapping socket option constants (e.g., socket.SO_KEEPALIVE)
661+
to the desired value for that option.
662+
663+
Returns
664+
-------
665+
dict
666+
The validated socket options
667+
668+
"""
669+
if value is None:
670+
return None
671+
672+
if not isinstance(value, Mapping):
673+
raise ValueError(
674+
'value {} must be of type dict'.format(value),
675+
)
676+
677+
out: dict[int, dict[int, Any]] = {}
678+
for level, options in value.items():
679+
if not isinstance(level, int):
680+
raise ValueError(
681+
f'keys in {value} must be integers corresponding to socket levels',
682+
)
683+
if not isinstance(options, Mapping):
684+
raise ValueError(
685+
f'values in {value} must be dicts.',
686+
)
687+
out[level] = {}
688+
for opt, val in options.items():
689+
if not isinstance(opt, int):
690+
raise ValueError(
691+
f'keys in sub-dicts of {value} must be integers.',
692+
)
693+
out[level][opt] = val
694+
695+
return out
696+
697+
649698
def check_dict_str_str(
650699
value: Any,
651700
) -> Optional[Dict[str, str]]:

0 commit comments

Comments
 (0)