[Asterisk-code-review] utils socket: range of random ports searched only once (testsuite[master])

Kevin Harwell asteriskteam at digium.com
Wed Apr 25 16:13:34 CDT 2018


Kevin Harwell has uploaded this change for review. ( https://gerrit.asterisk.org/8866


Change subject: utils_socket: range of random ports searched only once
......................................................................

utils_socket: range of random ports searched only once

If a range (greater than 1) of random ports was asked for, and if a port in
range was not available an exception was thrown stating the a port in range
was not free.

This patch makes it so the exception is caught and a new random port range is
tried again (up to "attempts" tries) before quitting.

Change-Id: I96affa9df38acf569f5d3c0965ac7b78d865455d
---
M lib/python/asterisk/utils_socket.py
1 file changed, 15 insertions(+), 3 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/testsuite refs/changes/66/8866/1

diff --git a/lib/python/asterisk/utils_socket.py b/lib/python/asterisk/utils_socket.py
index 2a11c97..8e4b32e 100644
--- a/lib/python/asterisk/utils_socket.py
+++ b/lib/python/asterisk/utils_socket.py
@@ -195,6 +195,9 @@
         if not isinstance(ports, list):
             ports = list(ports)
 
+        LOGGER.debug("Checking the following {0}/{1} ports for availability: "
+            "{2}".format(socket_type(socktype), socket_family(family), ports))
+
         res = []
         for port in ports:
             for attempt in range(attempts):
@@ -238,9 +241,18 @@
 
             # Need a random port first
             port = self.get_avail(host, 0, socktype, family)
-            ports = self.get_avail(
-                host, range(port[0] + step, port[0] + num, step),
-                socktype, family, attempts)
+
+            if abs(num) <= 1:
+                return port
+
+            try:
+                ports = self.get_avail(
+                    host, range(port[0] + step, port[0] + num, step),
+                    socktype, family, attempts)
+            except PortError:
+                # At least one port not free, try again
+                continue
+
             if ports:
                 return port + ports
 

-- 
To view, visit https://gerrit.asterisk.org/8866
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: testsuite
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I96affa9df38acf569f5d3c0965ac7b78d865455d
Gerrit-Change-Number: 8866
Gerrit-PatchSet: 1
Gerrit-Owner: Kevin Harwell <kharwell at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180425/62cd53f6/attachment-0001.html>


More information about the asterisk-code-review mailing list