Skip to content

Commit

Permalink
rework rtt stats
Browse files Browse the repository at this point in the history
  • Loading branch information
ingon committed Jan 18, 2025
1 parent 6c590e9 commit dbe804f
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 9 deletions.
7 changes: 5 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/connet-dev/connet/netc"
"github.com/connet-dev/connet/pb"
"github.com/connet-dev/connet/pbs"
"github.com/connet-dev/connet/quicc"
"github.com/connet-dev/connet/statusc"
"github.com/klev-dev/kleverr"
"github.com/quic-go/quic-go"
Expand Down Expand Up @@ -88,7 +89,8 @@ func (c *Client) Run(ctx context.Context) error {

c.logger.Debug("start quic listener")
transport := &quic.Transport{
Conn: udpConn,
Conn: udpConn,
ConnContext: quicc.RTTContext,
// TODO review other options
}
defer transport.Close()
Expand Down Expand Up @@ -170,7 +172,8 @@ func (c *Client) connect(ctx context.Context, transport *quic.Transport, retoken
RootCAs: c.controlCAs,
NextProtos: []string{"connet"},
}, &quic.Config{
KeepAlivePeriod: 25 * time.Second,
MaxIdleTimeout: 20 * time.Second,
KeepAlivePeriod: 10 * time.Second,
})
if err != nil {
return retConnect(err)
Expand Down
5 changes: 4 additions & 1 deletion client/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/connet-dev/connet/certc"
"github.com/connet-dev/connet/pb"
"github.com/connet-dev/connet/quicc"
"github.com/klev-dev/kleverr"
"github.com/quic-go/quic-go"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -139,7 +140,9 @@ func (s *DirectServer) runServer(ctx context.Context) error {
}

l, err := s.transport.Listen(tlsConf, &quic.Config{
KeepAlivePeriod: 25 * time.Second,
MaxIdleTimeout: 20 * time.Second,
KeepAlivePeriod: 10 * time.Second,
Tracer: quicc.RTTTracer,
})
if err != nil {
return err
Expand Down
13 changes: 11 additions & 2 deletions client/peer_direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/connet-dev/connet/pb"
"github.com/connet-dev/connet/pbc"
"github.com/connet-dev/connet/pbs"
"github.com/connet-dev/connet/quicc"
"github.com/klev-dev/kleverr"
"github.com/quic-go/quic-go"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -227,6 +228,9 @@ func (p *directPeerIncoming) keepalive(ctx context.Context, conn quic.Connection
if err := p.heartbeat(stream); err != nil {
return err
}
if rttStats := quicc.RTTStats(conn); rttStats != nil {
p.parent.logger.Debug("rtt-in", "last", rttStats.LatestRTT(), "smoothed", rttStats.SmoothedRTT())
}
}
})

Expand Down Expand Up @@ -307,13 +311,15 @@ func (p *directPeerOutgoing) connect(ctx context.Context) (quic.Connection, quic
addr := net.UDPAddrFromAddrPort(paddr)

p.parent.logger.Debug("dialing direct", "addr", addr, "server", p.serverConf.name, "cert", p.serverConf.key)
conn, err := p.parent.local.direct.transport.Dial(ctx, addr, &tls.Config{
conn, err := p.parent.local.direct.transport.Dial(quicc.RTTContext(ctx), addr, &tls.Config{
Certificates: []tls.Certificate{p.parent.local.clientCert},
RootCAs: p.serverConf.cas,
ServerName: p.serverConf.name,
NextProtos: []string{"connet-direct"},
}, &quic.Config{
KeepAlivePeriod: 25 * time.Second,
MaxIdleTimeout: 20 * time.Second,
KeepAlivePeriod: 10 * time.Second,
Tracer: quicc.RTTTracer,
})
if err != nil {
errs = append(errs, err)
Expand Down Expand Up @@ -352,6 +358,9 @@ func (p *directPeerOutgoing) keepalive(ctx context.Context, conn quic.Connection
if err := p.heartbeat(ctx, stream); err != nil {
return err
}
if rttStats := quicc.RTTStats(conn); rttStats != nil {
p.parent.logger.Debug("rtt-out", "last", rttStats.LatestRTT(), "smoothed", rttStats.SmoothedRTT())
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion client/peer_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ func (r *relayPeer) connect(ctx context.Context) (quic.Connection, error) {
ServerName: cfg.name,
NextProtos: []string{"connet-relay"},
}, &quic.Config{
KeepAlivePeriod: 25 * time.Second,
MaxIdleTimeout: 20 * time.Second,
KeepAlivePeriod: 10 * time.Second,
})
}

Expand Down
3 changes: 2 additions & 1 deletion control/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ func (s *Server) runListener(ctx context.Context) error {
defer transport.Close()

l, err := transport.Listen(s.tlsConf, &quic.Config{
KeepAlivePeriod: 25 * time.Second,
MaxIdleTimeout: 20 * time.Second,
KeepAlivePeriod: 10 * time.Second,
})
if err != nil {
return kleverr.Ret(err)
Expand Down
43 changes: 43 additions & 0 deletions quicc/rtt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package quicc

import (
"context"
"sync/atomic"

"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/logging"
)

type rttContextKeyType struct{}

var rttContextKey rttContextKeyType

type rttStats struct {
stats atomic.Pointer[logging.RTTStats]
}

func RTTContext(ctx context.Context) context.Context {
return context.WithValue(ctx, rttContextKey, &rttStats{})
}

func RTTTracer(ctx context.Context, pers logging.Perspective, ci quic.ConnectionID) *logging.ConnectionTracer {
v, ok := ctx.Value(rttContextKey).(*rttStats)
if !ok {
return nil
}
return &logging.ConnectionTracer{
UpdatedMetrics: func(rttStats *logging.RTTStats, cwnd, bytesInFlight logging.ByteCount, packetsInFlight int) {
// make a copy of the stats at this point of time
stats := *rttStats
v.stats.Store(&stats)
},
}
}

func RTTStats(conn quic.Connection) *logging.RTTStats {
v, ok := conn.Context().Value(rttContextKey).(*rttStats)
if !ok {
return nil
}
return v.stats.Load()
}
3 changes: 2 additions & 1 deletion relay/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ func (s *clientsServer) removeSource(fcs *forwardClients, conn *clientConn) {

func (s *clientsServer) run(ctx context.Context, transport *quic.Transport) error {
l, err := transport.Listen(s.tlsConf, &quic.Config{
KeepAlivePeriod: 25 * time.Second,
MaxIdleTimeout: 20 * time.Second,
KeepAlivePeriod: 10 * time.Second,
})
if err != nil {
return kleverr.Ret(err)
Expand Down
3 changes: 2 additions & 1 deletion relay/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ func (s *controlClient) connect(ctx context.Context, transport *quic.Transport)
}

conn, err := transport.Dial(ctx, s.controlAddr, s.controlTLSConf, &quic.Config{
KeepAlivePeriod: 25 * time.Second,
MaxIdleTimeout: 20 * time.Second,
KeepAlivePeriod: 10 * time.Second,
})
if err != nil {
return retConnect(err)
Expand Down

0 comments on commit dbe804f

Please sign in to comment.