mirror of
https://gitee.com/samwaf/SamWaf.git
synced 2026-11-03 03:31:58 +08:00
22 lines
406 B
Go
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
|
|
}
|