github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/latency/tcpcl.go (about) 1 package latency 2 3 import ( 4 "context" 5 "net" 6 "time" 7 8 "github.com/Asutorufa/yuhaiin/pkg/net/dialer" 9 ) 10 11 // TCPDelay get once delay by tcp 12 func TCPConnectLatency(address, port string) (time.Duration, error) { 13 timeNow := time.Now() 14 ctx, cancel := context.WithTimeout(context.TODO(), 3*time.Second) 15 defer cancel() 16 conn, err := dialer.DialContext(ctx, "tcp", net.JoinHostPort(address, port)) 17 if err != nil { 18 return 0, err 19 } 20 defer func() { 21 _ = conn.Close() 22 }() 23 return time.Since(timeNow), nil 24 }