bneradt commented on code in PR #13198:
URL: https://github.com/apache/trafficserver/pull/13198#discussion_r3300912989


##########
tests/gold_tests/autest-site/ports.py:
##########
@@ -236,6 +236,45 @@ def _get_port_by_bind():
     return port
 
 
+def _reserve_port():
+    """
+    Get a port from the global port queue.
+
+    Returns:
+        A tuple containing the port value and whether it should be recycled
+        into the queue when the caller is done with it.
+    """
+    _setup_port_queue()
+    if g_ports.qsize() > 0:
+        try:
+            port = _get_available_port(g_ports)
+            host.WriteVerbose("_reserve_port", f"Using port from port queue: 
{port}")
+            return port, True
+        except PortQueueSelectionError:
+            port = _get_port_by_bind()
+            host.WriteVerbose("_reserve_port", f"Queue was drained. Using port 
from a bound socket: {port}")
+            return port, False
+
+    # Since the queue could not be populated, use a port via bind.
+    port = _get_port_by_bind()
+    host.WriteVerbose("_reserve_port", f"Queue is empty. Using port from a 
bound socket: {port}")
+    return port, False
+
+
+def get_port_number():
+    """
+    Get a port number from the same allocator used by get_port().
+
+    This is useful for helper code that needs a temporary listening port but
+    does not have an AuTest object with Setup hooks for recycling it.
+
+    Returns:
+        A port value.
+    """
+    port, _ = _reserve_port()
+    return port

Review Comment:
   Done. get_port_number() now returns a context manager and recycles 
queue-backed reservations when the caller exits the context.



-- 
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]

Reply via email to