コード例 #1
0
ファイル: network.go プロジェクト: okushchenko/suplat
func Ping() (common.Point, error) {
	start := time.Now().UTC()
	point := common.Point{Time: start}
	d := net.Dialer{Timeout: 5 * time.Second}
	conn, err := d.Dial("tcp", "8.8.8.8:53")
	if err != nil {
		point.Latency = 5 * time.Second
		point.Error = 1
		point.Interface = ief
		return point, nil
	}
	point.Latency = time.Since(start)
	point.Error = 0
	ief, err = getInterface(strings.Split(conn.LocalAddr().String(), ":")[0])
	if err != nil {
		return point, fmt.Errorf("Failed to get interface: %s", err.Error())
	}
	point.Interface = ief
	conn.Close()
	return point, nil
}