Copilot commented on code in PR #13737: URL: https://github.com/apache/trafficserver/pull/13737#discussion_r4339023789
########## tests/uranium_tests/client_connection/throttle_client.py: ########## @@ -0,0 +1,159 @@ +"""Client connections for test_connections_throttle_exempt.py.""" +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from collections.abc import Callable +import socket +import ssl +import time + +TIMEOUT = 10.0 +POLL_INTERVAL = 0.1 + + +def proxy_header(source: str, port: int) -> bytes: + """Return a PROXY protocol v1 header that claims the connection comes from @a source. + + :param source: The claimed source address. + :param port: The destination port. + """ + + if ":" in source: + return f"PROXY TCP6 {source} ::1 40000 {port}\r\n".encode() + return f"PROXY TCP4 {source} 127.0.0.1 40000 {port}\r\n".encode() + + +def connect(host: str, port: int, tls: bool = False, proxy_source: str = "") -> socket.socket: + """Open a connection to @a host, an IPv4 or IPv6 address. + + :param tls: Use TLS. + :param proxy_source: Start with a PROXY protocol header that claims this source address. + """ + + sock = socket.create_connection((host, port), timeout=TIMEOUT) + if proxy_source: + sock.sendall(proxy_header(proxy_source, port)) + if tls: + context = ssl.create_default_context() + context.check_hostname = False + context.verify_mode = ssl.CERT_NONE + sock = context.wrap_socket(sock, server_hostname="healthcheck") + return sock + + +def exchange(sock: socket.socket, close: bool) -> str: + """Send a health check request on @a sock and return the response status line. + + :param close: Ask the proxy to close the connection after the response. + :return: The status line, or an empty string if the proxy closed the connection first. + """ + + request = b"GET / HTTP/1.1\r\nHost: healthcheck\r\n" + if close: + request += b"Connection: close\r\n" + try: + sock.sendall(request + b"\r\n") + response = b"" + while b"\r\n\r\n" not in response: + data = sock.recv(4096) + if not data: + return "" + response += data + except (ConnectionResetError, BrokenPipeError, ssl.SSLError): + return "" Review Comment: This helper sets a socket timeout (`socket.create_connection(..., timeout=TIMEOUT)`), but `exchange()` doesn’t handle `socket.timeout` during `recv()` / `sendall()`. A transient stall can raise `socket.timeout` and fail the test suite instead of being treated as a refusal/closed-connection outcome (consistent with the other caught exceptions). Consider catching `socket.timeout` (and, if appropriate, `TimeoutError`) here and returning an empty status like the other connection-level failures. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
