fix(notify): 修复传输生命周期竞态,完善背压与协议边界
- 完善 stream/bulk DataID 分配、预留和双向命名空间,修复并发打开及 dedicated/shared 回退时的 ID 冲突 - 将收发、回复、恢复任务和 sidecar 绑定原始会话与物理连接,防止重连后的旧消息误操作新连接 - 加强 close/reset 身份校验及实例移除检查,修复 dedicated attach 失败、通道引用和资源回收竞态 - 收紧批量发送器停止准入,确保在途入队完成后统一清理请求、缓冲区和等待者 - 修复 record 满队列死锁、取消时序号消耗及关闭竞态,确保关闭有界并返回真实错误 - 增加协商式 record 逻辑半关闭,保留反向 ACK;通过 reset 传递 RecordFailure,避免背压掩盖原始失败原因 - 补齐帧长度、批次数量、序号溢出和未确认窗口校验,提前拒绝超限数据并按字节预算拆批 - 为入站分发增加全局及单连接的条数、字节预算和阻塞背压,关闭时唤醒等待者,消除正常断连日志噪音 - 完善 bulk 窗口释放失败处理与传输诊断,补充并发、重连、背压、协议边界及真实 TCP 回归覆盖
This commit is contained in:
+53
-23
@@ -41,6 +41,9 @@ func encodeBulkFastFrameHeader(dst []byte, frameType uint8, flags uint8, dataID
|
||||
if dataID == 0 {
|
||||
return errBulkDataIDEmpty
|
||||
}
|
||||
if payloadLen < 0 || uint64(payloadLen) > uint64(^uint32(0)) {
|
||||
return errBulkFastPayloadInvalid
|
||||
}
|
||||
if len(dst) < bulkFastPayloadHeaderLen {
|
||||
return errBulkFastPayloadInvalid
|
||||
}
|
||||
@@ -92,8 +95,8 @@ func decodeBulkFastFrame(payload []byte) (bulkFastFrame, bool, error) {
|
||||
default:
|
||||
return bulkFastFrame{}, true, errBulkFastPayloadInvalid
|
||||
}
|
||||
dataLen := int(binary.BigEndian.Uint32(payload[24:28]))
|
||||
if dataLen < 0 || len(payload) != bulkFastPayloadHeaderLen+dataLen {
|
||||
wireDataLen := binary.BigEndian.Uint32(payload[24:28])
|
||||
if uint64(len(payload)-bulkFastPayloadHeaderLen) != uint64(wireDataLen) {
|
||||
return bulkFastFrame{}, true, errBulkFastPayloadInvalid
|
||||
}
|
||||
dataID := binary.BigEndian.Uint64(payload[8:16])
|
||||
@@ -200,10 +203,14 @@ func (c *ClientCommon) encodeBulkFastBatchPayloadPooled(frames []bulkFastFrame)
|
||||
}
|
||||
|
||||
func (c *ClientCommon) sendFastBulkData(ctx context.Context, dataID uint64, seq uint64, chunk []byte, fastPathVersion uint8) error {
|
||||
binding := c.clientTransportBindingSnapshot()
|
||||
if binding == nil {
|
||||
return net.ErrClosed
|
||||
return c.sendFastBulkDataAtRoute(ctx, c.clientSessionRouteSnapshot(), dataID, seq, chunk, fastPathVersion)
|
||||
}
|
||||
|
||||
func (c *ClientCommon) sendFastBulkDataAtRoute(ctx context.Context, route clientSessionRoute, dataID uint64, seq uint64, chunk []byte, fastPathVersion uint8) error {
|
||||
if err := c.ensureClientSessionRouteSendReady(route); err != nil {
|
||||
return err
|
||||
}
|
||||
binding := route.binding
|
||||
if sender := binding.clientBulkBatchSenderSnapshot(c); sender != nil {
|
||||
return sender.submitData(ctx, dataID, seq, fastPathVersion, chunk)
|
||||
}
|
||||
@@ -211,17 +218,21 @@ func (c *ClientCommon) sendFastBulkData(ctx context.Context, dataID uint64, seq
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.writePayloadToTransport(payload)
|
||||
return c.writePayloadToTransportBindingContextTimeout(ctx, binding, payload, 0)
|
||||
}
|
||||
|
||||
func (c *ClientCommon) sendFastBulkWrite(ctx context.Context, dataID uint64, startSeq uint64, chunkSize int, fastPathVersion uint8, payload []byte, payloadOwned bool) (int, error) {
|
||||
return c.sendFastBulkWriteAtRoute(ctx, c.clientSessionRouteSnapshot(), dataID, startSeq, chunkSize, fastPathVersion, payload, payloadOwned)
|
||||
}
|
||||
|
||||
func (c *ClientCommon) sendFastBulkWriteAtRoute(ctx context.Context, route clientSessionRoute, dataID uint64, startSeq uint64, chunkSize int, fastPathVersion uint8, payload []byte, payloadOwned bool) (int, error) {
|
||||
if len(payload) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
binding := c.clientTransportBindingSnapshot()
|
||||
if binding == nil {
|
||||
return 0, net.ErrClosed
|
||||
if err := c.ensureClientSessionRouteSendReady(route); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
binding := route.binding
|
||||
if sender := binding.clientBulkBatchSenderSnapshot(c); sender != nil {
|
||||
return sender.submitWrite(ctx, dataID, startSeq, fastPathVersion, payload, chunkSize, payloadOwned)
|
||||
}
|
||||
@@ -235,7 +246,7 @@ func (c *ClientCommon) sendFastBulkWrite(ctx context.Context, dataID uint64, sta
|
||||
if end > len(payload) {
|
||||
end = len(payload)
|
||||
}
|
||||
if err := c.sendFastBulkData(ctx, dataID, seq, payload[written:end], fastPathVersion); err != nil {
|
||||
if err := c.sendFastBulkDataAtRoute(ctx, route, dataID, seq, payload[written:end], fastPathVersion); err != nil {
|
||||
return written, err
|
||||
}
|
||||
seq++
|
||||
@@ -245,6 +256,10 @@ func (c *ClientCommon) sendFastBulkWrite(ctx context.Context, dataID uint64, sta
|
||||
}
|
||||
|
||||
func (c *ClientCommon) sendFastBulkControl(ctx context.Context, frameType uint8, flags uint8, dataID uint64, seq uint64, fastPathVersion uint8, payload []byte) error {
|
||||
return c.sendFastBulkControlAtRoute(ctx, c.clientSessionRouteSnapshot(), frameType, flags, dataID, seq, fastPathVersion, payload)
|
||||
}
|
||||
|
||||
func (c *ClientCommon) sendFastBulkControlAtRoute(ctx context.Context, route clientSessionRoute, frameType uint8, flags uint8, dataID uint64, seq uint64, fastPathVersion uint8, payload []byte) error {
|
||||
frame := bulkFastFrame{
|
||||
Type: frameType,
|
||||
Flags: flags,
|
||||
@@ -252,10 +267,10 @@ func (c *ClientCommon) sendFastBulkControl(ctx context.Context, frameType uint8,
|
||||
Seq: seq,
|
||||
Payload: payload,
|
||||
}
|
||||
binding := c.clientTransportBindingSnapshot()
|
||||
if binding == nil {
|
||||
return net.ErrClosed
|
||||
if err := c.ensureClientSessionRouteSendReady(route); err != nil {
|
||||
return err
|
||||
}
|
||||
binding := route.binding
|
||||
if sender := binding.clientBulkBatchSenderSnapshot(c); sender != nil {
|
||||
return sender.submitControl(ctx, frameType, flags, dataID, seq, fastPathVersion, payload)
|
||||
}
|
||||
@@ -263,7 +278,7 @@ func (c *ClientCommon) sendFastBulkControl(ctx context.Context, frameType uint8,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.writePayloadToTransport(encoded)
|
||||
return c.writePayloadToTransportBindingContextTimeout(ctx, binding, encoded, 0)
|
||||
}
|
||||
|
||||
func (c *ClientCommon) encodeBulkFastControlPayload(frameType uint8, flags uint8, dataID uint64, seq uint64, payload []byte) ([]byte, error) {
|
||||
@@ -359,7 +374,7 @@ func (s *ServerCommon) sendFastBulkDataTransport(ctx context.Context, logical *L
|
||||
if logical == nil {
|
||||
return errTransportDetached
|
||||
}
|
||||
if binding := logical.transportBindingSnapshot(); binding != nil {
|
||||
if binding := serverTransportBindingSnapshot(logical, transport); binding != nil {
|
||||
if binding.queueSnapshot() != nil {
|
||||
if sender := binding.serverBulkBatchSenderSnapshot(logical); sender != nil {
|
||||
return sender.submitData(ctx, dataID, seq, fastPathVersion, chunk)
|
||||
@@ -386,7 +401,7 @@ func (s *ServerCommon) sendFastBulkWriteTransport(ctx context.Context, logical *
|
||||
if logical == nil {
|
||||
return 0, errTransportDetached
|
||||
}
|
||||
if binding := logical.transportBindingSnapshot(); binding != nil {
|
||||
if binding := serverTransportBindingSnapshot(logical, transport); binding != nil {
|
||||
if binding.queueSnapshot() != nil {
|
||||
if sender := binding.serverBulkBatchSenderSnapshot(logical); sender != nil {
|
||||
return sender.submitWrite(ctx, dataID, startSeq, fastPathVersion, payload, chunkSize, payloadOwned)
|
||||
@@ -422,7 +437,7 @@ func (s *ServerCommon) sendFastBulkControlTransport(ctx context.Context, logical
|
||||
if logical == nil {
|
||||
return errTransportDetached
|
||||
}
|
||||
if binding := logical.transportBindingSnapshot(); binding != nil {
|
||||
if binding := serverTransportBindingSnapshot(logical, transport); binding != nil {
|
||||
if binding.queueSnapshot() != nil {
|
||||
if sender := binding.serverBulkBatchSenderSnapshot(logical); sender != nil {
|
||||
return sender.submitControl(ctx, frameType, flags, dataID, seq, fastPathVersion, payload)
|
||||
@@ -517,11 +532,15 @@ func decryptTransportPayloadWithFallbackPooled(primary transportProtectionProfil
|
||||
}
|
||||
|
||||
func (c *ClientCommon) tryDispatchBorrowedTransportPlain(plain []byte, release func()) bool {
|
||||
return c.tryDispatchBorrowedTransportPlainAtRoute(c.clientSessionRouteSnapshot(), plain, release)
|
||||
}
|
||||
|
||||
func (c *ClientCommon) tryDispatchBorrowedTransportPlainAtRoute(route clientSessionRoute, plain []byte, release func()) bool {
|
||||
switch transportFastPayloadMagic(plain) {
|
||||
case bulkFastPayloadMagic, bulkFastBatchMagic:
|
||||
owner := newBulkReadPayloadOwner(release)
|
||||
matched, walkErr := walkBulkFastFrames(plain, func(frame bulkFastFrame) error {
|
||||
c.dispatchFastBulkFrameWithOwner(frame, owner)
|
||||
c.dispatchFastBulkFrameWithOwnerAtRoute(route, frame, owner)
|
||||
return nil
|
||||
})
|
||||
if owner != nil {
|
||||
@@ -537,7 +556,7 @@ func (c *ClientCommon) tryDispatchBorrowedTransportPlain(plain []byte, release f
|
||||
case streamFastPayloadMagic, streamFastBatchMagic:
|
||||
owner := newStreamReadPayloadOwner(release)
|
||||
matched, walkErr := walkStreamFastFrames(plain, func(frame streamFastDataFrame) error {
|
||||
c.dispatchFastStreamDataWithOwner(frame, owner)
|
||||
c.dispatchFastStreamDataWithOwnerAtRoute(route, frame, owner)
|
||||
return nil
|
||||
})
|
||||
if owner != nil {
|
||||
@@ -595,22 +614,33 @@ func (s *ServerCommon) tryDispatchBorrowedTransportPlain(logical *LogicalConn, t
|
||||
}
|
||||
|
||||
func (c *ClientCommon) dispatchInboundTransportPayload(payload []byte, now time.Time) error {
|
||||
return c.dispatchInboundTransportPayloadAtRoute(c.clientSessionRouteSnapshot(), payload, now)
|
||||
}
|
||||
|
||||
func (c *ClientCommon) dispatchInboundTransportPayloadAtRoute(route clientSessionRoute, payload []byte, now time.Time) error {
|
||||
plain, err := c.decryptTransportPayload(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.dispatchInboundTransportPlain(plain, now)
|
||||
return c.dispatchInboundTransportPlainAtRoute(route, plain, now)
|
||||
}
|
||||
|
||||
func (c *ClientCommon) dispatchInboundTransportPlain(plain []byte, now time.Time) error {
|
||||
return c.dispatchInboundTransportPlainAtRoute(c.clientSessionRouteSnapshot(), plain, now)
|
||||
}
|
||||
|
||||
func (c *ClientCommon) dispatchInboundTransportPlainAtRoute(route clientSessionRoute, plain []byte, now time.Time) error {
|
||||
if route.bound() && !c.clientSessionRouteCurrent(route) {
|
||||
return transportDetachedSessionEpochError()
|
||||
}
|
||||
if matched, err := walkBulkFastFrames(plain, func(frame bulkFastFrame) error {
|
||||
c.dispatchFastBulkFrame(frame)
|
||||
c.dispatchFastBulkFrameAtRoute(route, frame)
|
||||
return nil
|
||||
}); matched {
|
||||
return err
|
||||
}
|
||||
if matched, err := walkStreamFastFrames(plain, func(frame streamFastDataFrame) error {
|
||||
c.dispatchFastStreamData(frame)
|
||||
c.dispatchFastStreamDataWithOwnerAtRoute(route, frame, nil)
|
||||
return nil
|
||||
}); matched {
|
||||
return err
|
||||
@@ -619,7 +649,7 @@ func (c *ClientCommon) dispatchInboundTransportPlain(plain []byte, now time.Time
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.dispatchEnvelope(env, now)
|
||||
c.dispatchEnvelopeAtRoute(route, env, now)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user