From 8bb1a3bdf7671806c9c16a78f63a1526c958226a Mon Sep 17 00:00:00 2001 From: Kleissner Date: Mon, 19 Apr 2021 15:32:14 +0200 Subject: [PATCH] GetRTT function --- Connection.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Connection.go b/Connection.go index b7d7db7..f25defb 100644 --- a/Connection.go +++ b/Connection.go @@ -223,6 +223,24 @@ func (peer *PeerInfo) removeInactiveConnection(input *Connection) { } } +// GetRTT returns the round-trip time for the most recent active connection. 0 if not available. +func (peer *PeerInfo) GetRTT() (rtt time.Duration) { + peer.Lock() + defer peer.Unlock() + + if peer.connectionLatest != nil && peer.connectionLatest.RoundTripTime > 0 { + return peer.connectionLatest.RoundTripTime + } + + for _, connection := range peer.connectionActive { + if connection.RoundTripTime > 0 { + return connection.RoundTripTime + } + } + + return 0 +} + // ---- sending code ---- // send sends a raw packet to the peer. Only uses active connections.