expose basic connection stats via Conn.ConnectionStats (#5281)

* Add ConnectionStats

* remove for loop

* Add comments

* Update comments

---------

Co-authored-by: Marco Munizaga <git@marcopolo.io>
This commit is contained in:
Marten Seemann
2025-08-13 16:45:14 +02:00
committed by GitHub
parent e0a836f597
commit da27fcf33f
9 changed files with 169 additions and 28 deletions

View File

@@ -13,12 +13,13 @@ func NewAckHandler(
initialPacketNumber protocol.PacketNumber,
initialMaxDatagramSize protocol.ByteCount,
rttStats *utils.RTTStats,
connStats *utils.ConnectionStats,
clientAddressValidated bool,
enableECN bool,
pers protocol.Perspective,
tracer *logging.ConnectionTracer,
logger utils.Logger,
) (SentPacketHandler, ReceivedPacketHandler) {
sph := newSentPacketHandler(initialPacketNumber, initialMaxDatagramSize, rttStats, clientAddressValidated, enableECN, pers, tracer, logger)
sph := newSentPacketHandler(initialPacketNumber, initialMaxDatagramSize, rttStats, connStats, clientAddressValidated, enableECN, pers, tracer, logger)
return sph, newReceivedPacketHandler(sph, logger)
}

View File

@@ -90,6 +90,7 @@ type sentPacketHandler struct {
congestion congestion.SendAlgorithmWithDebugInfos
rttStats *utils.RTTStats
connStats *utils.ConnectionStats
// The number of times a PTO has been sent without receiving an ack.
ptoCount uint32
@@ -121,6 +122,7 @@ func newSentPacketHandler(
initialPN protocol.PacketNumber,
initialMaxDatagramSize protocol.ByteCount,
rttStats *utils.RTTStats,
connStats *utils.ConnectionStats,
clientAddressValidated bool,
enableECN bool,
pers protocol.Perspective,
@@ -130,6 +132,7 @@ func newSentPacketHandler(
congestion := congestion.NewCubicSender(
congestion.DefaultClock{},
rttStats,
connStats,
initialMaxDatagramSize,
true, // use Reno
tracer,
@@ -142,6 +145,7 @@ func newSentPacketHandler(
handshakePackets: newPacketNumberSpace(0, false),
appDataPackets: newPacketNumberSpace(0, true),
rttStats: rttStats,
connStats: connStats,
congestion: congestion,
perspective: pers,
tracer: tracer,
@@ -216,6 +220,7 @@ func (h *sentPacketHandler) DropPackets(encLevel protocol.EncryptionLevel, now t
}
func (h *sentPacketHandler) ReceivedBytes(n protocol.ByteCount, t time.Time) {
h.connStats.BytesReceived.Add(uint64(n))
wasAmplificationLimit := h.isAmplificationLimited()
h.bytesReceived += n
if wasAmplificationLimit && !h.isAmplificationLimited() {
@@ -224,6 +229,7 @@ func (h *sentPacketHandler) ReceivedBytes(n protocol.ByteCount, t time.Time) {
}
func (h *sentPacketHandler) ReceivedPacket(l protocol.EncryptionLevel, t time.Time) {
h.connStats.PacketsReceived.Add(1)
if h.perspective == protocol.PerspectiveServer && l == protocol.EncryptionHandshake && !h.peerAddressValidated {
h.peerAddressValidated = true
h.setLossDetectionTimer(t)
@@ -253,6 +259,8 @@ func (h *sentPacketHandler) SentPacket(
isPathProbePacket bool,
) {
h.bytesSent += size
h.connStats.BytesSent.Add(uint64(size))
h.connStats.PacketsSent.Add(1)
pnSpace := h.getPacketNumberSpace(encLevel)
if h.logger.Debug() && (pnSpace.history.HasOutstandingPackets() || pnSpace.history.HasOutstandingPathProbes()) {
@@ -992,6 +1000,7 @@ func (h *sentPacketHandler) MigratedPath(now time.Time, initialMaxDatagramSize p
h.congestion = congestion.NewCubicSender(
congestion.DefaultClock{},
h.rttStats,
h.connStats,
initialMaxDatagramSize,
true, // use Reno
h.tracer,

View File

@@ -107,6 +107,7 @@ func testSentPacketHandlerSendAndAcknowledge(t *testing.T, encLevel protocol.Enc
0,
1200,
&utils.RTTStats{},
&utils.ConnectionStats{},
false,
false,
protocol.PerspectiveClient,
@@ -160,6 +161,7 @@ func TestSentPacketHandlerAcknowledgeSkippedPacket(t *testing.T) {
0,
1200,
&utils.RTTStats{},
&utils.ConnectionStats{},
false,
false,
protocol.PerspectiveClient,
@@ -214,6 +216,7 @@ func testSentPacketHandlerRTTs(t *testing.T, encLevel protocol.EncryptionLevel,
0,
1200,
&rttStats,
&utils.ConnectionStats{},
false,
false,
protocol.PerspectiveClient,
@@ -253,7 +256,7 @@ func testSentPacketHandlerRTTs(t *testing.T, encLevel protocol.EncryptionLevel,
for i := 0; i < 5; i++ {
packets = append(packets, sendPacket(now))
}
expectedRTTStatsNoAckDelay := expectedRTTStats
expectedRTTStatsNoAckDelay := expectedRTTStats.Clone()
for i := 0; i < 5; i++ {
const ackDelay = 500 * time.Millisecond
expectedRTTStats.UpdateRTT(time.Duration(i+1)*time.Second, ackDelay)
@@ -302,6 +305,7 @@ func testSentPacketHandlerAmplificationLimitServer(t *testing.T, addressValidate
0,
1200,
&utils.RTTStats{},
&utils.ConnectionStats{},
addressValidated,
false,
protocol.PerspectiveServer,
@@ -371,6 +375,7 @@ func testSentPacketHandlerAmplificationLimitClient(t *testing.T, dropHandshake b
0,
1200,
&utils.RTTStats{},
&utils.ConnectionStats{},
true,
false,
protocol.PerspectiveClient,
@@ -425,6 +430,7 @@ func TestSentPacketHandlerDelayBasedLossDetection(t *testing.T) {
0,
1200,
&rttStats,
&utils.ConnectionStats{},
true,
false,
protocol.PerspectiveServer,
@@ -477,6 +483,7 @@ func TestSentPacketHandlerPacketBasedLossDetection(t *testing.T) {
0,
1200,
&rttStats,
&utils.ConnectionStats{},
true,
false,
protocol.PerspectiveServer,
@@ -541,6 +548,7 @@ func testSentPacketHandlerPTO(t *testing.T, encLevel protocol.EncryptionLevel, p
0,
1200,
&rttStats,
&utils.ConnectionStats{},
true,
false,
protocol.PerspectiveServer,
@@ -680,6 +688,7 @@ func TestSentPacketHandlerPacketNumberSpacesPTO(t *testing.T) {
0,
1200,
&rttStats,
&utils.ConnectionStats{},
true,
false,
protocol.PerspectiveServer,
@@ -770,6 +779,7 @@ func TestSentPacketHandler0RTT(t *testing.T) {
0,
1200,
&utils.RTTStats{},
&utils.ConnectionStats{},
true,
false,
protocol.PerspectiveClient,
@@ -819,6 +829,7 @@ func TestSentPacketHandlerCongestion(t *testing.T) {
0,
1200,
&rttStats,
&utils.ConnectionStats{},
true,
false,
protocol.PerspectiveServer,
@@ -918,6 +929,7 @@ func testSentPacketHandlerRetry(t *testing.T, rtt, expectedRTT time.Duration) {
0,
1200,
&rttStats,
&utils.ConnectionStats{},
true,
false,
protocol.PerspectiveClient,
@@ -968,6 +980,7 @@ func TestSentPacketHandlerRetryAfterPTO(t *testing.T) {
0,
1200,
&rttStats,
&utils.ConnectionStats{},
true,
false,
protocol.PerspectiveClient,
@@ -1011,6 +1024,7 @@ func TestSentPacketHandlerECN(t *testing.T) {
0,
1200,
&utils.RTTStats{},
&utils.ConnectionStats{},
true,
false,
protocol.PerspectiveClient,
@@ -1113,6 +1127,7 @@ func TestSentPacketHandlerPathProbe(t *testing.T) {
0,
1200,
&rttStats,
&utils.ConnectionStats{},
true,
false,
protocol.PerspectiveClient,
@@ -1191,6 +1206,7 @@ func TestSentPacketHandlerPathProbeAckAndLoss(t *testing.T) {
0,
1200,
&rttStats,
&utils.ConnectionStats{},
true,
false,
protocol.PerspectiveClient,
@@ -1265,6 +1281,7 @@ func testSentPacketHandlerRandomized(t *testing.T, seed uint64) {
0,
1200,
&rttStats,
&utils.ConnectionStats{},
true,
false,
protocol.PerspectiveClient,