Refactor network.port_is_up function to improve default parameter handling and timeout configuration. Update log messages for better clarity when falling back to basic port checks.
This commit is contained in:
@@ -40,15 +40,22 @@ end
|
|||||||
|
|
||||||
-- Check if a TCP port is accepting connections within a timeout (seconds)
|
-- Check if a TCP port is accepting connections within a timeout (seconds)
|
||||||
function network.port_is_up(host, port, timeout_sec)
|
function network.port_is_up(host, port, timeout_sec)
|
||||||
host = tostring(host or "127.0.0.1")
|
-- Set defaults with clear intent
|
||||||
port = tonumber(port or 0) or 0
|
host = host or "127.0.0.1"
|
||||||
local timeout = tonumber(timeout_sec or 1) or 1
|
port = port or 0
|
||||||
|
timeout_sec = timeout_sec or 30
|
||||||
|
|
||||||
|
-- Convert to proper types
|
||||||
|
host = tostring(host)
|
||||||
|
port = tonumber(port) or 0
|
||||||
|
local timeout = tonumber(timeout_sec) or 30
|
||||||
|
|
||||||
if port <= 0 then return false end
|
if port <= 0 then return false end
|
||||||
|
|
||||||
-- Fallback to basic check if socket is not available
|
-- Fallback to basic check if socket is not available
|
||||||
if not network.is_socket_available() then
|
if not network.is_socket_available() then
|
||||||
utils.log("Socket module not available, using basic port check")
|
utils.log("Socket module not available, using basic port check with " .. timeout .. "s timeout")
|
||||||
local cmd = string.format("nc -z -w1 %s %d 2>/dev/null", host, port)
|
local cmd = string.format("nc -z -w%d %s %d 2>/dev/null", timeout, host, port)
|
||||||
local result = os.execute(cmd)
|
local result = os.execute(cmd)
|
||||||
return result == 0
|
return result == 0
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user