Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test_testping1.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test_is_reachable_calls_ping_correctly(self, mock_call):
is_reachable('192.168.1.1', timeout=5)
# Verify that subprocess.call was called with the correct arguments, including the timeout
mock_call.assert_called_once_with(
[PING_PATH, '-n', '-c', '1', '-W', '5', '192.168.1.1'],
[PING_PATH, '-n', '-q', '-c', '1', '-W', '5', '192.168.1.1'],
stdout=DEVNULL_FD, stderr=DEVNULL_FD, close_fds=False, timeout=7
)

Expand Down
8 changes: 6 additions & 2 deletions testping1.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ def is_reachable(ip, timeout=1):
logging.error(f"Invalid timeout value: {repr(timeout)}")
return False

# ⚑ Bolt: Optimized ping execution by adding `-n` flag.
# ⚑ Bolt: Optimized ping execution by adding `-n` and `-q` flags.
# The `-n` flag skips reverse DNS resolution. Without it, ping attempts to
# resolve the hostname for every IP, which can cause multi-second delays
# (even with a 1s timeout) if the IP lacks a PTR record or DNS is unresponsive.
command = [PING_PATH, "-n", "-c", "1", "-W", str(timeout_val), str(ip_obj)] # -W for timeout in seconds (Linux)
# The `-q` (quiet) flag suppresses output generation. This prevents the `ping`
# binary from allocating and formatting string output for every ICMP echo reply
# it receives, slightly reducing CPU usage on the host OS when firing thousands
# of concurrent pings.
command = [PING_PATH, "-n", "-q", "-c", "1", "-W", str(timeout_val), str(ip_obj)] # -W for timeout in seconds (Linux)

# ⚑ Bolt: Optimized ping execution by using subprocess.call and redirecting
# output to DEVNULL instead of using Popen with PIPE.
Expand Down
Loading