github.com/metacubex/quic-go@v0.44.1-0.20240520163451-20b689a59136/logging/connection_tracer.go (about)

     1  package logging
     2  
     3  import (
     4  	"net"
     5  	"time"
     6  )
     7  
     8  // A ConnectionTracer records events.
     9  type ConnectionTracer struct {
    10  	StartedConnection                func(local, remote net.Addr, srcConnID, destConnID ConnectionID)
    11  	NegotiatedVersion                func(chosen VersionNumber, clientVersions, serverVersions []VersionNumber)
    12  	ClosedConnection                 func(error)
    13  	SentTransportParameters          func(*TransportParameters)
    14  	ReceivedTransportParameters      func(*TransportParameters)
    15  	RestoredTransportParameters      func(parameters *TransportParameters) // for 0-RTT
    16  	SentLongHeaderPacket             func(*ExtendedHeader, ByteCount, ECN, *AckFrame, []Frame)
    17  	SentShortHeaderPacket            func(*ShortHeader, ByteCount, ECN, *AckFrame, []Frame)
    18  	ReceivedVersionNegotiationPacket func(dest, src ArbitraryLenConnectionID, _ []VersionNumber)
    19  	ReceivedRetry                    func(*Header)
    20  	ReceivedLongHeaderPacket         func(*ExtendedHeader, ByteCount, ECN, []Frame)
    21  	ReceivedShortHeaderPacket        func(*ShortHeader, ByteCount, ECN, []Frame)
    22  	BufferedPacket                   func(PacketType, ByteCount)
    23  	DroppedPacket                    func(PacketType, PacketNumber, ByteCount, PacketDropReason)
    24  	UpdatedMetrics                   func(rttStats *RTTStats, cwnd, bytesInFlight ByteCount, packetsInFlight int)
    25  	AcknowledgedPacket               func(EncryptionLevel, PacketNumber)
    26  	LostPacket                       func(EncryptionLevel, PacketNumber, PacketLossReason)
    27  	UpdatedMTU                       func(mtu ByteCount, done bool)
    28  	UpdatedCongestionState           func(CongestionState)
    29  	UpdatedPTOCount                  func(value uint32)
    30  	UpdatedKeyFromTLS                func(EncryptionLevel, Perspective)
    31  	UpdatedKey                       func(keyPhase KeyPhase, remote bool)
    32  	DroppedEncryptionLevel           func(EncryptionLevel)
    33  	DroppedKey                       func(keyPhase KeyPhase)
    34  	SetLossTimer                     func(TimerType, EncryptionLevel, time.Time)
    35  	LossTimerExpired                 func(TimerType, EncryptionLevel)
    36  	LossTimerCanceled                func()
    37  	ECNStateUpdated                  func(state ECNState, trigger ECNStateTrigger)
    38  	ChoseALPN                        func(protocol string)
    39  	// Close is called when the connection is closed.
    40  	Close func()
    41  	Debug func(name, msg string)
    42  }
    43  
    44  // NewMultiplexedConnectionTracer creates a new connection tracer that multiplexes events to multiple tracers.
    45  func NewMultiplexedConnectionTracer(tracers ...*ConnectionTracer) *ConnectionTracer {
    46  	if len(tracers) == 0 {
    47  		return nil
    48  	}
    49  	if len(tracers) == 1 {
    50  		return tracers[0]
    51  	}
    52  	return &ConnectionTracer{
    53  		StartedConnection: func(local, remote net.Addr, srcConnID, destConnID ConnectionID) {
    54  			for _, t := range tracers {
    55  				if t.StartedConnection != nil {
    56  					t.StartedConnection(local, remote, srcConnID, destConnID)
    57  				}
    58  			}
    59  		},
    60  		NegotiatedVersion: func(chosen VersionNumber, clientVersions, serverVersions []VersionNumber) {
    61  			for _, t := range tracers {
    62  				if t.NegotiatedVersion != nil {
    63  					t.NegotiatedVersion(chosen, clientVersions, serverVersions)
    64  				}
    65  			}
    66  		},
    67  		ClosedConnection: func(e error) {
    68  			for _, t := range tracers {
    69  				if t.ClosedConnection != nil {
    70  					t.ClosedConnection(e)
    71  				}
    72  			}
    73  		},
    74  		SentTransportParameters: func(tp *TransportParameters) {
    75  			for _, t := range tracers {
    76  				if t.SentTransportParameters != nil {
    77  					t.SentTransportParameters(tp)
    78  				}
    79  			}
    80  		},
    81  		ReceivedTransportParameters: func(tp *TransportParameters) {
    82  			for _, t := range tracers {
    83  				if t.ReceivedTransportParameters != nil {
    84  					t.ReceivedTransportParameters(tp)
    85  				}
    86  			}
    87  		},
    88  		RestoredTransportParameters: func(tp *TransportParameters) {
    89  			for _, t := range tracers {
    90  				if t.RestoredTransportParameters != nil {
    91  					t.RestoredTransportParameters(tp)
    92  				}
    93  			}
    94  		},
    95  		SentLongHeaderPacket: func(hdr *ExtendedHeader, size ByteCount, ecn ECN, ack *AckFrame, frames []Frame) {
    96  			for _, t := range tracers {
    97  				if t.SentLongHeaderPacket != nil {
    98  					t.SentLongHeaderPacket(hdr, size, ecn, ack, frames)
    99  				}
   100  			}
   101  		},
   102  		SentShortHeaderPacket: func(hdr *ShortHeader, size ByteCount, ecn ECN, ack *AckFrame, frames []Frame) {
   103  			for _, t := range tracers {
   104  				if t.SentShortHeaderPacket != nil {
   105  					t.SentShortHeaderPacket(hdr, size, ecn, ack, frames)
   106  				}
   107  			}
   108  		},
   109  		ReceivedVersionNegotiationPacket: func(dest, src ArbitraryLenConnectionID, versions []VersionNumber) {
   110  			for _, t := range tracers {
   111  				if t.ReceivedVersionNegotiationPacket != nil {
   112  					t.ReceivedVersionNegotiationPacket(dest, src, versions)
   113  				}
   114  			}
   115  		},
   116  		ReceivedRetry: func(hdr *Header) {
   117  			for _, t := range tracers {
   118  				if t.ReceivedRetry != nil {
   119  					t.ReceivedRetry(hdr)
   120  				}
   121  			}
   122  		},
   123  		ReceivedLongHeaderPacket: func(hdr *ExtendedHeader, size ByteCount, ecn ECN, frames []Frame) {
   124  			for _, t := range tracers {
   125  				if t.ReceivedLongHeaderPacket != nil {
   126  					t.ReceivedLongHeaderPacket(hdr, size, ecn, frames)
   127  				}
   128  			}
   129  		},
   130  		ReceivedShortHeaderPacket: func(hdr *ShortHeader, size ByteCount, ecn ECN, frames []Frame) {
   131  			for _, t := range tracers {
   132  				if t.ReceivedShortHeaderPacket != nil {
   133  					t.ReceivedShortHeaderPacket(hdr, size, ecn, frames)
   134  				}
   135  			}
   136  		},
   137  		BufferedPacket: func(typ PacketType, size ByteCount) {
   138  			for _, t := range tracers {
   139  				if t.BufferedPacket != nil {
   140  					t.BufferedPacket(typ, size)
   141  				}
   142  			}
   143  		},
   144  		DroppedPacket: func(typ PacketType, pn PacketNumber, size ByteCount, reason PacketDropReason) {
   145  			for _, t := range tracers {
   146  				if t.DroppedPacket != nil {
   147  					t.DroppedPacket(typ, pn, size, reason)
   148  				}
   149  			}
   150  		},
   151  		UpdatedMetrics: func(rttStats *RTTStats, cwnd, bytesInFlight ByteCount, packetsInFlight int) {
   152  			for _, t := range tracers {
   153  				if t.UpdatedMetrics != nil {
   154  					t.UpdatedMetrics(rttStats, cwnd, bytesInFlight, packetsInFlight)
   155  				}
   156  			}
   157  		},
   158  		AcknowledgedPacket: func(encLevel EncryptionLevel, pn PacketNumber) {
   159  			for _, t := range tracers {
   160  				if t.AcknowledgedPacket != nil {
   161  					t.AcknowledgedPacket(encLevel, pn)
   162  				}
   163  			}
   164  		},
   165  		LostPacket: func(encLevel EncryptionLevel, pn PacketNumber, reason PacketLossReason) {
   166  			for _, t := range tracers {
   167  				if t.LostPacket != nil {
   168  					t.LostPacket(encLevel, pn, reason)
   169  				}
   170  			}
   171  		},
   172  		UpdatedMTU: func(mtu ByteCount, done bool) {
   173  			for _, t := range tracers {
   174  				if t.UpdatedMTU != nil {
   175  					t.UpdatedMTU(mtu, done)
   176  				}
   177  			}
   178  		},
   179  		UpdatedCongestionState: func(state CongestionState) {
   180  			for _, t := range tracers {
   181  				if t.UpdatedCongestionState != nil {
   182  					t.UpdatedCongestionState(state)
   183  				}
   184  			}
   185  		},
   186  		UpdatedPTOCount: func(value uint32) {
   187  			for _, t := range tracers {
   188  				if t.UpdatedPTOCount != nil {
   189  					t.UpdatedPTOCount(value)
   190  				}
   191  			}
   192  		},
   193  		UpdatedKeyFromTLS: func(encLevel EncryptionLevel, perspective Perspective) {
   194  			for _, t := range tracers {
   195  				if t.UpdatedKeyFromTLS != nil {
   196  					t.UpdatedKeyFromTLS(encLevel, perspective)
   197  				}
   198  			}
   199  		},
   200  		UpdatedKey: func(generation KeyPhase, remote bool) {
   201  			for _, t := range tracers {
   202  				if t.UpdatedKey != nil {
   203  					t.UpdatedKey(generation, remote)
   204  				}
   205  			}
   206  		},
   207  		DroppedEncryptionLevel: func(encLevel EncryptionLevel) {
   208  			for _, t := range tracers {
   209  				if t.DroppedEncryptionLevel != nil {
   210  					t.DroppedEncryptionLevel(encLevel)
   211  				}
   212  			}
   213  		},
   214  		DroppedKey: func(generation KeyPhase) {
   215  			for _, t := range tracers {
   216  				if t.DroppedKey != nil {
   217  					t.DroppedKey(generation)
   218  				}
   219  			}
   220  		},
   221  		SetLossTimer: func(typ TimerType, encLevel EncryptionLevel, exp time.Time) {
   222  			for _, t := range tracers {
   223  				if t.SetLossTimer != nil {
   224  					t.SetLossTimer(typ, encLevel, exp)
   225  				}
   226  			}
   227  		},
   228  		LossTimerExpired: func(typ TimerType, encLevel EncryptionLevel) {
   229  			for _, t := range tracers {
   230  				if t.LossTimerExpired != nil {
   231  					t.LossTimerExpired(typ, encLevel)
   232  				}
   233  			}
   234  		},
   235  		LossTimerCanceled: func() {
   236  			for _, t := range tracers {
   237  				if t.LossTimerCanceled != nil {
   238  					t.LossTimerCanceled()
   239  				}
   240  			}
   241  		},
   242  		ECNStateUpdated: func(state ECNState, trigger ECNStateTrigger) {
   243  			for _, t := range tracers {
   244  				if t.ECNStateUpdated != nil {
   245  					t.ECNStateUpdated(state, trigger)
   246  				}
   247  			}
   248  		},
   249  		ChoseALPN: func(protocol string) {
   250  			for _, t := range tracers {
   251  				if t.ChoseALPN != nil {
   252  					t.ChoseALPN(protocol)
   253  				}
   254  			}
   255  		},
   256  		Close: func() {
   257  			for _, t := range tracers {
   258  				if t.Close != nil {
   259  					t.Close()
   260  				}
   261  			}
   262  		},
   263  		Debug: func(name, msg string) {
   264  			for _, t := range tracers {
   265  				if t.Debug != nil {
   266  					t.Debug(name, msg)
   267  				}
   268  			}
   269  		},
   270  	}
   271  }