Skip to content

Commit b6eecf3

Browse files
olethanhnesitor
authored andcommitted
Fast port allocation checker: move to its own file
1 parent 4ef9eed commit b6eecf3

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/aleph/vm/models.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
)
3333
from aleph.vm.network.firewall import add_port_redirect_rule, remove_port_redirect_rule
3434
from aleph.vm.network.interfaces import TapInterface
35-
from aleph.vm.network.port_availability_checker import get_available_host_port
35+
from aleph.vm.network.port_availability_checker import fast_get_available_host_port
3636
from aleph.vm.orchestrator.metrics import (
3737
ExecutionRecord,
3838
delete_record,
@@ -65,9 +65,6 @@ def to_dict(self):
6565
return self.__dict__
6666

6767

68-
LAST_ASSIGNED_HOST_PORT = 24000
69-
70-
7168
class VmExecution:
7269
"""
7370
Control the execution of a VM on a high level.
@@ -136,13 +133,9 @@ async def update_port_redirects(self, requested_ports: dict[int, dict[str, bool]
136133
host_port = current["host"]
137134
remove_port_redirect_rule(interface, host_port, vm_port, protocol)
138135
del self.mapped_ports[vm_port]
139-
140136
for vm_port in redirect_to_add:
141137
target = requested_ports[vm_port]
142-
host_port = get_available_host_port(start_port=LAST_ASSIGNED_HOST_PORT)
143-
LAST_ASSIGNED_HOST_PORT = host_port
144-
if LAST_ASSIGNED_HOST_PORT > 65535:
145-
LAST_ASSIGNED_HOST_PORT = 24000
138+
host_port = fast_get_available_host_port()
146139

147140
for protocol in SUPPORTED_PROTOCOL_FOR_REDIRECT:
148141
if target[protocol]:

src/aleph/vm/network/port_availability_checker.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import socket
2-
from typing import Optional
32

43
from aleph.vm.network.firewall import check_nftables_redirections
54

65
MIN_DYNAMIC_PORT = 24000
76
MAX_PORT = 65535
87

98

10-
def get_available_host_port(start_port: Optional[int] = None) -> int:
9+
def get_available_host_port(start_port: int | None = None) -> int:
1110
"""Find an available port on the host system.
1211
1312
Args:
@@ -37,7 +36,31 @@ def get_available_host_port(start_port: Optional[int] = None) -> int:
3736

3837
return port
3938

40-
except (socket.error, OSError):
39+
except OSError:
4140
pass
4241

4342
raise RuntimeError(f"No available ports found in range {MIN_DYNAMIC_PORT}-{MAX_PORT}")
43+
44+
45+
LAST_ASSIGNED_HOST_PORT = MIN_DYNAMIC_PORT
46+
47+
48+
def fast_get_available_host_port() -> int:
49+
"""Find an available port on the host system.
50+
Use a global state to not start as each check may take several seconds and return a resulta faster
51+
52+
Args:
53+
start_port: Optional starting port number. If not provided, starts from MIN_DYNAMIC_PORT
54+
55+
Returns:
56+
An available port number
57+
58+
Raises:
59+
RuntimeError: If no ports are available in the valid range
60+
"""
61+
global LAST_ASSIGNED_HOST_PORT # noqa: PLW0603
62+
host_port = get_available_host_port(start_port=LAST_ASSIGNED_HOST_PORT)
63+
LAST_ASSIGNED_HOST_PORT = host_port
64+
if LAST_ASSIGNED_HOST_PORT > MAX_PORT:
65+
LAST_ASSIGNED_HOST_PORT = MIN_DYNAMIC_PORT
66+
return host_port

0 commit comments

Comments
 (0)