2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-10-15 16:57:10 +00:00
|
|
|
|
|
|
|
package derp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2023-06-22 17:01:26 +00:00
|
|
|
"tailscale.com/net/tcpinfo"
|
2022-10-15 16:57:10 +00:00
|
|
|
)
|
|
|
|
|
2024-05-14 16:28:01 +00:00
|
|
|
func (c *sclient) startStatsLoop(ctx context.Context) {
|
2023-06-22 17:01:26 +00:00
|
|
|
// Get the RTT initially to verify it's supported.
|
|
|
|
conn := c.tcpConn()
|
|
|
|
if conn == nil {
|
2022-10-15 16:57:10 +00:00
|
|
|
c.s.tcpRtt.Add("non-tcp", 1)
|
2024-05-14 16:28:01 +00:00
|
|
|
return
|
2022-10-15 16:57:10 +00:00
|
|
|
}
|
2023-06-22 17:01:26 +00:00
|
|
|
if _, err := tcpinfo.RTT(conn); err != nil {
|
|
|
|
c.logf("error fetching initial RTT: %v", err)
|
2022-10-15 16:57:10 +00:00
|
|
|
c.s.tcpRtt.Add("error", 1)
|
2024-05-14 16:28:01 +00:00
|
|
|
return
|
2022-10-15 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const statsInterval = 10 * time.Second
|
|
|
|
|
2024-05-14 16:28:01 +00:00
|
|
|
// Don't launch a goroutine; use a timer instead.
|
|
|
|
var gatherStats func()
|
|
|
|
gatherStats = func() {
|
|
|
|
// Do nothing if the context is finished.
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-10-15 16:57:10 +00:00
|
|
|
|
2024-05-14 16:28:01 +00:00
|
|
|
// Reschedule ourselves when this stats gathering is finished.
|
|
|
|
defer c.s.clock.AfterFunc(statsInterval, gatherStats)
|
2022-10-15 16:57:10 +00:00
|
|
|
|
2024-05-14 16:28:01 +00:00
|
|
|
// Gather TCP RTT information.
|
|
|
|
rtt, err := tcpinfo.RTT(conn)
|
|
|
|
if err == nil {
|
2022-10-15 16:57:10 +00:00
|
|
|
c.s.tcpRtt.Add(durationToLabel(rtt), 1)
|
|
|
|
}
|
2024-05-14 16:28:01 +00:00
|
|
|
|
|
|
|
// TODO(andrew): more metrics?
|
2022-10-15 16:57:10 +00:00
|
|
|
}
|
2024-05-14 16:28:01 +00:00
|
|
|
|
|
|
|
// Kick off the initial timer.
|
|
|
|
c.s.clock.AfterFunc(statsInterval, gatherStats)
|
2022-10-15 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// tcpConn attempts to get the underlying *net.TCPConn from this client's
|
|
|
|
// Conn; if it cannot, then it will return nil.
|
|
|
|
func (c *sclient) tcpConn() *net.TCPConn {
|
|
|
|
nc := c.nc
|
|
|
|
for {
|
|
|
|
switch v := nc.(type) {
|
|
|
|
case *net.TCPConn:
|
|
|
|
return v
|
|
|
|
case *tls.Conn:
|
|
|
|
nc = v.NetConn()
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func durationToLabel(dur time.Duration) string {
|
|
|
|
switch {
|
|
|
|
case dur <= 10*time.Millisecond:
|
|
|
|
return "10ms"
|
|
|
|
case dur <= 20*time.Millisecond:
|
|
|
|
return "20ms"
|
|
|
|
case dur <= 50*time.Millisecond:
|
|
|
|
return "50ms"
|
|
|
|
case dur <= 100*time.Millisecond:
|
|
|
|
return "100ms"
|
|
|
|
case dur <= 150*time.Millisecond:
|
|
|
|
return "150ms"
|
|
|
|
case dur <= 250*time.Millisecond:
|
|
|
|
return "250ms"
|
|
|
|
case dur <= 500*time.Millisecond:
|
|
|
|
return "500ms"
|
|
|
|
default:
|
|
|
|
return "inf"
|
|
|
|
}
|
|
|
|
}
|