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