Skip to content

Commit f242c0f

Browse files
cathleenyIsaac
andcommitted
kernel: skip ip_address for host names on the host:port path
_is_bare_ipv6 now returns early unless the text has at least two colons and no leading bracket. ip_address raised a ValueError for every host name and host:port string, which made the folding path about 2x slower than bc7c09e. A bare IPv6 literal always has two or more colons, so behavior is unchanged; the session tests and the realkernel loopback tests pass. Microbenchmark, median us per call (20000 calls x 7 repeats): - bare host, _port 8443: 1.305 base, 1.261 new - host:port, _port 8443: 1.364 base, 1.393 new (+2%) - IPv6 bracketed, _port 8443: 1.340 base, 1.355 new (+1%) - IPv6 bare, _port 8443: 1.413 base, 2.642 new (+87%, the successful ip_address parse) Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
1 parent bdeef64 commit f242c0f

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

‎src/databricks/sql/session.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def _redact(value: str) -> str:
9696

9797
def _is_bare_ipv6(text: str) -> bool:
9898
"""True for an unbracketed IPv6 literal such as ``::1``, which urlsplit reads as a port."""
99+
# A bare IPv6 literal has at least two colons and no brackets. Checking that first skips
100+
# the costly ValueError that ip_address raises for names and host:port strings.
101+
if text.count(":") < 2 or text.startswith("["):
102+
return False
99103
try:
100104
return ipaddress.ip_address(text).version == 6
101105
except ValueError:
@@ -124,7 +128,7 @@ def _split_host(host: str) -> Tuple[str, str, Optional[int]]:
124128
f"Invalid server_hostname {_redact(host)!r}: malformed IPv6 literal or port; "
125129
"a port must be 1-65535"
126130
) from None
127-
if parts.password is not None:
131+
if "@" in parts.netloc and parts.password is not None:
128132
# A colon before '@' is userinfo, not a port, but a credential-bearing host is
129133
# still refused when a port is requested (as on main), so it is never folded.
130134
raise ValueError(

0 commit comments

Comments
 (0)