Files
SamWaf/utils/port_check.go
T
2025-05-03 20:47:20 +08:00

22 lines
406 B
Go

package utils
import (
"fmt"
"net"
"time"
)
// CheckPortReady 测试端口可用性
func CheckPortReady(port int, timeout time.Duration) bool {
end := time.Now().Add(timeout)
for time.Now().Before(end) {
conn, err := net.DialTimeout("tcp", fmt.Sprintf(":%d", port), 500*time.Millisecond)
if err == nil {
conn.Close()
return true
}
time.Sleep(300 * time.Millisecond)
}
return false
}