RunTCPPingLoop 使用 tcp 进行 ping
(ipport string, count int)
| 15 | |
| 16 | // RunTCPPingLoop 使用 tcp 进行 ping |
| 17 | func RunTCPPingLoop(ipport string, count int) (r ICMPPingResult) { |
| 18 | r = ICMPPingResult{ |
| 19 | PacketsSent: count, |
| 20 | PacketsLoss: count, |
| 21 | AvgTimeMill: 9999, |
| 22 | } |
| 23 | if count <= 0 { |
| 24 | return |
| 25 | } |
| 26 | durs := make([]int64, 0, count) |
| 27 | for i := 0; i < count; i++ { |
| 28 | d, err := tcping(ipport) |
| 29 | if err == nil { |
| 30 | r.PacketsLoss-- |
| 31 | durs = append(durs, d) |
| 32 | } |
| 33 | time.Sleep(time.Millisecond * 100) |
| 34 | } |
| 35 | |
| 36 | if len(durs) > 0 { |
| 37 | r.AvgTimeMill = 0 |
| 38 | for _, d := range durs { |
| 39 | r.AvgTimeMill += d |
| 40 | } |
| 41 | if len(durs) > 1 { |
| 42 | r.AvgTimeMill /= int64(len(durs)) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | func tcping(ipport string) (int64, error) { |
| 50 | t := time.Now().UnixMilli() |
no test coverage detected