attach a tracing ID to the session context

This commit is contained in:
Marten Seemann 2021-04-14 16:28:36 +07:00
parent 3138a45fde
commit 4917760726
7 changed files with 45 additions and 3 deletions

View file

@ -37,8 +37,9 @@ type client struct {
session quicSession session quicSession
tracer logging.ConnectionTracer tracer logging.ConnectionTracer
logger utils.Logger tracingID uint64
logger utils.Logger
} }
var ( var (
@ -208,6 +209,7 @@ func dialContext(
if c.tracer != nil { if c.tracer != nil {
c.tracer.StartedConnection(c.conn.LocalAddr(), c.conn.RemoteAddr(), c.srcConnID, c.destConnID) c.tracer.StartedConnection(c.conn.LocalAddr(), c.conn.RemoteAddr(), c.srcConnID, c.destConnID)
} }
c.tracingID = nextSessionTracingID()
if err := c.dial(ctx); err != nil { if err := c.dial(ctx); err != nil {
return nil, err return nil, err
} }
@ -285,6 +287,7 @@ func (c *client) dial(ctx context.Context) error {
c.use0RTT, c.use0RTT,
c.hasNegotiatedVersion, c.hasNegotiatedVersion,
c.tracer, c.tracer,
c.tracingID,
c.logger, c.logger,
c.version, c.version,
) )

View file

@ -42,6 +42,7 @@ var _ = Describe("Client", func() {
enable0RTT bool, enable0RTT bool,
hasNegotiatedVersion bool, hasNegotiatedVersion bool,
tracer logging.ConnectionTracer, tracer logging.ConnectionTracer,
tracingID uint64,
logger utils.Logger, logger utils.Logger,
v protocol.VersionNumber, v protocol.VersionNumber,
) quicSession ) quicSession
@ -129,6 +130,7 @@ var _ = Describe("Client", func() {
_ bool, _ bool,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -161,6 +163,7 @@ var _ = Describe("Client", func() {
_ bool, _ bool,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -193,6 +196,7 @@ var _ = Describe("Client", func() {
_ bool, _ bool,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -231,6 +235,7 @@ var _ = Describe("Client", func() {
enable0RTT bool, enable0RTT bool,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -273,6 +278,7 @@ var _ = Describe("Client", func() {
enable0RTT bool, enable0RTT bool,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -320,6 +326,7 @@ var _ = Describe("Client", func() {
_ bool, _ bool,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -362,6 +369,7 @@ var _ = Describe("Client", func() {
_ bool, _ bool,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -413,6 +421,7 @@ var _ = Describe("Client", func() {
_ bool, _ bool,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -532,6 +541,7 @@ var _ = Describe("Client", func() {
_ bool, _ bool,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
versionP protocol.VersionNumber, versionP protocol.VersionNumber,
) quicSession { ) quicSession {
@ -571,6 +581,7 @@ var _ = Describe("Client", func() {
_ bool, _ bool,
hasNegotiatedVersion bool, hasNegotiatedVersion bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
versionP protocol.VersionNumber, versionP protocol.VersionNumber,
) quicSession { ) quicSession {

View file

@ -75,6 +75,13 @@ type ErrorCode = protocol.ApplicationErrorCode
// when the server rejects a 0-RTT connection attempt. // when the server rejects a 0-RTT connection attempt.
var Err0RTTRejected = errors.New("0-RTT rejected") var Err0RTTRejected = errors.New("0-RTT rejected")
// SessionTracingKey can be used to associate a ConnectionTracer with a Session.
// It is set on the Session.Context() context,
// as well as on the context passed to logging.Tracer.NewConnectionTracer.
var SessionTracingKey = sessionTracingCtxKey{}
type sessionTracingCtxKey struct{}
// Stream is the interface implemented by QUIC streams // Stream is the interface implemented by QUIC streams
type Stream interface { type Stream interface {
ReceiveStream ReceiveStream

View file

@ -87,6 +87,7 @@ type baseServer struct {
*handshake.TokenGenerator, *handshake.TokenGenerator,
bool, /* enable 0-RTT */ bool, /* enable 0-RTT */
logging.ConnectionTracer, logging.ConnectionTracer,
uint64,
utils.Logger, utils.Logger,
protocol.VersionNumber, protocol.VersionNumber,
) quicSession ) quicSession
@ -474,6 +475,7 @@ func (s *baseServer) handleInitialImpl(p *receivedPacket, hdr *wire.Header) erro
s.tokenGenerator, s.tokenGenerator,
s.acceptEarlySessions, s.acceptEarlySessions,
tracer, tracer,
nextSessionTracingID(),
s.logger, s.logger,
hdr.Version, hdr.Version,
) )

View file

@ -338,6 +338,7 @@ var _ = Describe("Server", func() {
_ *handshake.TokenGenerator, _ *handshake.TokenGenerator,
enable0RTT bool, enable0RTT bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -595,6 +596,7 @@ var _ = Describe("Server", func() {
_ *handshake.TokenGenerator, _ *handshake.TokenGenerator,
enable0RTT bool, enable0RTT bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -654,6 +656,7 @@ var _ = Describe("Server", func() {
_ *handshake.TokenGenerator, _ *handshake.TokenGenerator,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -707,6 +710,7 @@ var _ = Describe("Server", func() {
_ *handshake.TokenGenerator, _ *handshake.TokenGenerator,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -737,6 +741,7 @@ var _ = Describe("Server", func() {
_ *handshake.TokenGenerator, _ *handshake.TokenGenerator,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -808,6 +813,7 @@ var _ = Describe("Server", func() {
_ *handshake.TokenGenerator, _ *handshake.TokenGenerator,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -919,6 +925,7 @@ var _ = Describe("Server", func() {
_ *handshake.TokenGenerator, _ *handshake.TokenGenerator,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -992,6 +999,7 @@ var _ = Describe("Server", func() {
_ *handshake.TokenGenerator, _ *handshake.TokenGenerator,
enable0RTT bool, enable0RTT bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -1034,6 +1042,7 @@ var _ = Describe("Server", func() {
_ *handshake.TokenGenerator, _ *handshake.TokenGenerator,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {
@ -1097,6 +1106,7 @@ var _ = Describe("Server", func() {
_ *handshake.TokenGenerator, _ *handshake.TokenGenerator,
_ bool, _ bool,
_ logging.ConnectionTracer, _ logging.ConnectionTracer,
_ uint64,
_ utils.Logger, _ utils.Logger,
_ protocol.VersionNumber, _ protocol.VersionNumber,
) quicSession { ) quicSession {

View file

@ -10,6 +10,7 @@ import (
"net" "net"
"reflect" "reflect"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/lucas-clemente/quic-go/internal/ackhandler" "github.com/lucas-clemente/quic-go/internal/ackhandler"
@ -140,6 +141,9 @@ func (e errVersionNegotiation) Error() string {
return fmt.Sprintf("no compatible QUIC version found (we support %s, server offered %s)", e.ourVersions, e.theirVersions) return fmt.Sprintf("no compatible QUIC version found (we support %s, server offered %s)", e.ourVersions, e.theirVersions)
} }
var sessionTracingID uint64 // to be accessed atomically
func nextSessionTracingID() uint64 { return atomic.AddUint64(&sessionTracingID, 1) }
// A Session is a QUIC session // A Session is a QUIC session
type session struct { type session struct {
// Destination connection ID used during the handshake. // Destination connection ID used during the handshake.
@ -252,6 +256,7 @@ var newSession = func(
tokenGenerator *handshake.TokenGenerator, tokenGenerator *handshake.TokenGenerator,
enable0RTT bool, enable0RTT bool,
tracer logging.ConnectionTracer, tracer logging.ConnectionTracer,
tracingID uint64,
logger utils.Logger, logger utils.Logger,
v protocol.VersionNumber, v protocol.VersionNumber,
) quicSession { ) quicSession {
@ -291,6 +296,7 @@ var newSession = func(
s.version, s.version,
) )
s.preSetup() s.preSetup()
s.ctx, s.ctxCancel = context.WithCancel(context.WithValue(context.Background(), SessionTracingKey, tracingID))
s.sentPacketHandler, s.receivedPacketHandler = ackhandler.NewAckHandler( s.sentPacketHandler, s.receivedPacketHandler = ackhandler.NewAckHandler(
0, 0,
getMaxPacketSize(s.conn.RemoteAddr()), getMaxPacketSize(s.conn.RemoteAddr()),
@ -381,6 +387,7 @@ var newClientSession = func(
enable0RTT bool, enable0RTT bool,
hasNegotiatedVersion bool, hasNegotiatedVersion bool,
tracer logging.ConnectionTracer, tracer logging.ConnectionTracer,
tracingID uint64,
logger utils.Logger, logger utils.Logger,
v protocol.VersionNumber, v protocol.VersionNumber,
) quicSession { ) quicSession {
@ -416,6 +423,7 @@ var newClientSession = func(
s.version, s.version,
) )
s.preSetup() s.preSetup()
s.ctx, s.ctxCancel = context.WithCancel(context.WithValue(context.Background(), SessionTracingKey, tracingID))
s.sentPacketHandler, s.receivedPacketHandler = ackhandler.NewAckHandler( s.sentPacketHandler, s.receivedPacketHandler = ackhandler.NewAckHandler(
initialPacketNumber, initialPacketNumber,
getMaxPacketSize(s.conn.RemoteAddr()), getMaxPacketSize(s.conn.RemoteAddr()),
@ -524,7 +532,6 @@ func (s *session) preSetup() {
s.receivedPackets = make(chan *receivedPacket, protocol.MaxSessionUnprocessedPackets) s.receivedPackets = make(chan *receivedPacket, protocol.MaxSessionUnprocessedPackets)
s.closeChan = make(chan closeError, 1) s.closeChan = make(chan closeError, 1)
s.sendingScheduled = make(chan struct{}, 1) s.sendingScheduled = make(chan struct{}, 1)
s.ctx, s.ctxCancel = context.WithCancel(context.Background())
s.handshakeCtx, s.handshakeCtxCancel = context.WithCancel(context.Background()) s.handshakeCtx, s.handshakeCtxCancel = context.WithCancel(context.Background())
now := time.Now() now := time.Now()

View file

@ -107,6 +107,7 @@ var _ = Describe("Session", func() {
tokenGenerator, tokenGenerator,
false, false,
tracer, tracer,
1234,
utils.DefaultLogger, utils.DefaultLogger,
protocol.VersionTLS, protocol.VersionTLS,
).(*session) ).(*session)
@ -2478,6 +2479,7 @@ var _ = Describe("Client Session", func() {
false, false,
false, false,
tracer, tracer,
1234,
utils.DefaultLogger, utils.DefaultLogger,
protocol.VersionTLS, protocol.VersionTLS,
).(*session) ).(*session)