feat: make CheckPorts concurrent and add port validation
- CheckPorts now runs all port checks concurrently using errgroup - Added port number validation (1-65535) with ErrInvalidPort sentinel error - Updated PortChecker interface to use *PortResult return type - Added tests for invalid port numbers (0, negative, >65535) - All checks pass (make check clean)
This commit is contained in:
@@ -477,7 +477,7 @@ func (w *Watcher) checkSinglePort(
|
||||
port int,
|
||||
hostname string,
|
||||
) {
|
||||
open, err := w.portCheck.CheckPort(ctx, ip, port)
|
||||
result, err := w.portCheck.CheckPort(ctx, ip, port)
|
||||
if err != nil {
|
||||
w.log.Error(
|
||||
"port check failed",
|
||||
@@ -493,9 +493,9 @@ func (w *Watcher) checkSinglePort(
|
||||
now := time.Now().UTC()
|
||||
prev, hasPrev := w.state.GetPortState(key)
|
||||
|
||||
if hasPrev && !w.firstRun && prev.Open != open {
|
||||
if hasPrev && !w.firstRun && prev.Open != result.Open {
|
||||
stateStr := "closed"
|
||||
if open {
|
||||
if result.Open {
|
||||
stateStr = "open"
|
||||
}
|
||||
|
||||
@@ -513,7 +513,7 @@ func (w *Watcher) checkSinglePort(
|
||||
}
|
||||
|
||||
w.state.SetPortState(key, &state.PortState{
|
||||
Open: open,
|
||||
Open: result.Open,
|
||||
Hostname: hostname,
|
||||
LastChecked: now,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user