fix: kcp 协议内部日志细节
This commit is contained in:
@@ -17,6 +17,7 @@ type kcpPendingPacketDebug struct {
|
||||
remoteAddr net.Addr
|
||||
packetBytes int
|
||||
kcpConv *uint32
|
||||
segments []KCPPacketDebugSegment
|
||||
timestamps map[string]int64
|
||||
}
|
||||
|
||||
@@ -75,13 +76,15 @@ func (c *platformKCPPacketConn) ReadFrom(p []byte) (int, net.Addr, error) {
|
||||
}
|
||||
|
||||
if rxTimestamp > 0 {
|
||||
kcpConv, segments := parseKCPPacketMetadata(p[:n])
|
||||
c.logKCPPacketDebugRecord(c.newKCPPacketDebugRecord(
|
||||
latencylog.EventBRXSoftware,
|
||||
addr,
|
||||
n,
|
||||
rxTimestamp,
|
||||
nil,
|
||||
parseKCPConversationID(p[:n]),
|
||||
kcpConv,
|
||||
segments,
|
||||
))
|
||||
}
|
||||
|
||||
@@ -102,7 +105,11 @@ func (c *platformKCPPacketConn) WriteTo(p []byte, addr net.Addr) (int, error) {
|
||||
return 0, fmt.Errorf("transport: kcp packet write target must be UDPAddr, got %T", addr)
|
||||
}
|
||||
|
||||
expectedTXID := c.nextExpectedTXID()
|
||||
// Reserve the local txID before the send so an immediately-arriving errqueue
|
||||
// event can still find its pending record. If the send never succeeds, roll
|
||||
// the reservation back to keep the local txID mirror aligned with the kernel.
|
||||
kcpConv, segments := parseKCPPacketMetadata(p)
|
||||
expectedTXID := c.reservePendingTX(udpAddr, len(p), kcpConv, segments)
|
||||
for {
|
||||
err := c.sendmsgRaw(p, udpAddr)
|
||||
if err != nil {
|
||||
@@ -110,10 +117,10 @@ func (c *platformKCPPacketConn) WriteTo(p []byte, addr net.Addr) (int, error) {
|
||||
time.Sleep(linuxDataPollInterval)
|
||||
continue
|
||||
}
|
||||
c.rollbackPendingTX(expectedTXID)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
c.storePendingTX(expectedTXID, udpAddr, len(p), parseKCPConversationID(p))
|
||||
return len(p), nil
|
||||
}
|
||||
}
|
||||
@@ -247,6 +254,7 @@ func (c *platformKCPPacketConn) collectTXErrqueueLoop() {
|
||||
event.TSUnixNano,
|
||||
&udpTxID,
|
||||
record.kcpConv,
|
||||
record.segments,
|
||||
))
|
||||
|
||||
if complete {
|
||||
@@ -281,25 +289,31 @@ func (c *platformKCPPacketConn) recvTXErrqueueOnce() (txTimestampEvent, error) {
|
||||
return event, nil
|
||||
}
|
||||
|
||||
func (c *platformKCPPacketConn) nextExpectedTXID() uint32 {
|
||||
func (c *platformKCPPacketConn) reservePendingTX(remoteAddr net.Addr, packetBytes int, kcpConv *uint32, segments []KCPPacketDebugSegment) uint32 {
|
||||
c.pendingMu.Lock()
|
||||
defer c.pendingMu.Unlock()
|
||||
|
||||
next := c.nextTXID
|
||||
txID := c.nextTXID
|
||||
c.nextTXID++
|
||||
return next
|
||||
}
|
||||
|
||||
func (c *platformKCPPacketConn) storePendingTX(txID uint32, remoteAddr net.Addr, packetBytes int, kcpConv *uint32) {
|
||||
c.pendingMu.Lock()
|
||||
defer c.pendingMu.Unlock()
|
||||
|
||||
c.pendingTX[txID] = kcpPendingPacketDebug{
|
||||
remoteAddr: remoteAddr,
|
||||
packetBytes: packetBytes,
|
||||
kcpConv: kcpConv,
|
||||
segments: append([]KCPPacketDebugSegment(nil), segments...),
|
||||
timestamps: make(map[string]int64, 2),
|
||||
}
|
||||
|
||||
return txID
|
||||
}
|
||||
|
||||
func (c *platformKCPPacketConn) rollbackPendingTX(txID uint32) {
|
||||
c.pendingMu.Lock()
|
||||
defer c.pendingMu.Unlock()
|
||||
|
||||
delete(c.pendingTX, txID)
|
||||
if c.nextTXID == txID+1 {
|
||||
c.nextTXID = txID
|
||||
}
|
||||
}
|
||||
|
||||
func (c *platformKCPPacketConn) recordPendingTXEvent(event txTimestampEvent) (*kcpPendingPacketDebug, bool) {
|
||||
|
||||
Reference in New Issue
Block a user