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:
+68
-28
@@ -21,44 +21,72 @@ func (c *ClientCommon) OpenStream(ctx context.Context, opt StreamOpenOptions) (S
|
||||
if runtime == nil {
|
||||
return nil, errStreamRuntimeNil
|
||||
}
|
||||
route := c.clientSessionRouteSnapshot()
|
||||
if err := c.ensureClientSessionRouteSendReady(route); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
scope := clientFileScope()
|
||||
req := clientStreamRequest(runtime, opt)
|
||||
if req.StreamID == "" {
|
||||
return nil, errStreamIDEmpty
|
||||
}
|
||||
if _, exists := runtime.lookup(clientFileScope(), req.StreamID); exists {
|
||||
return nil, errStreamAlreadyExists
|
||||
if existing, exists := runtime.lookup(scope, req.StreamID); exists {
|
||||
if existing.acceptsClientSessionRoute(route) {
|
||||
return nil, errStreamAlreadyExists
|
||||
}
|
||||
existing.markReset(transportDetachedSessionEpochError())
|
||||
}
|
||||
resp, err := sendStreamOpenClient(ctx, c, req)
|
||||
dataID, err := runtime.reserveDataID(scope)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.DataID != 0 {
|
||||
req.DataID = resp.DataID
|
||||
req.DataID = dataID
|
||||
parent := clientSessionRouteContext(route)
|
||||
if parent == nil {
|
||||
parent = c.clientStopContextSnapshot()
|
||||
}
|
||||
if resp.FastPathVersion != 0 {
|
||||
req.FastPathVersion = resp.FastPathVersion
|
||||
} else {
|
||||
req.FastPathVersion = streamFastPathVersionV1
|
||||
}
|
||||
req.Metadata = mergeStreamMetadata(req.Metadata, resp.Metadata)
|
||||
stream := newStreamHandle(c.clientStopContextSnapshot(), runtime, clientFileScope(), req, c.currentClientSessionEpoch(), nil, nil, resp.TransportGeneration, clientStreamCloseSender(c), clientStreamResetSender(c), clientStreamDataSender(c, c.currentClientSessionEpoch()), runtime.configSnapshot())
|
||||
stream := newStreamHandle(parent, runtime, scope, req, route.epoch, nil, nil, 0, clientStreamCloseSender(c), clientStreamResetSender(c), clientStreamDataSender(c, route), runtime.configSnapshot())
|
||||
stream.setClientSnapshotOwner(c)
|
||||
stream.setAddrSnapshot(c.clientStreamAddrSnapshot())
|
||||
if err := runtime.register(clientFileScope(), stream); err != nil {
|
||||
_, _ = sendStreamResetClient(context.Background(), c, StreamResetRequest{
|
||||
StreamID: req.StreamID,
|
||||
Error: err.Error(),
|
||||
})
|
||||
stream.setClientSessionRoute(route)
|
||||
stream.setAddrSnapshot(c.clientStreamAddrSnapshotAtRoute(route))
|
||||
if err := runtime.adoptReserved(scope, stream); err != nil {
|
||||
runtime.releaseDataID(scope, req.DataID)
|
||||
return nil, err
|
||||
}
|
||||
resp, err := sendStreamOpenClientAtRoute(ctx, c, route, req)
|
||||
if err != nil {
|
||||
c.bestEffortStreamResetAtRoute(route, StreamResetRequest{StreamID: req.StreamID, DataID: req.DataID, Error: err.Error()})
|
||||
stream.markReset(err)
|
||||
return nil, err
|
||||
}
|
||||
if resp.DataID != 0 && resp.DataID != req.DataID {
|
||||
err = errStreamAlreadyExists
|
||||
c.bestEffortStreamResetAtRoute(route, StreamResetRequest{StreamID: req.StreamID, Error: "stream data id mismatch"})
|
||||
stream.markReset(err)
|
||||
return nil, err
|
||||
}
|
||||
if resp.FastPathVersion != 0 {
|
||||
stream.setFastPathVersion(resp.FastPathVersion)
|
||||
} else {
|
||||
stream.setFastPathVersion(streamFastPathVersionV1)
|
||||
}
|
||||
stream.metadata = mergeStreamMetadata(req.Metadata, resp.Metadata)
|
||||
stream.setTransportGeneration(resp.TransportGeneration)
|
||||
return stream, nil
|
||||
}
|
||||
|
||||
func (c *ClientCommon) clientStreamAddrSnapshot() (net.Addr, net.Addr) {
|
||||
return c.clientStreamAddrSnapshotAtRoute(c.clientSessionRouteSnapshot())
|
||||
}
|
||||
|
||||
func (c *ClientCommon) clientStreamAddrSnapshotAtRoute(route clientSessionRoute) (net.Addr, net.Addr) {
|
||||
if c == nil {
|
||||
return nil, nil
|
||||
}
|
||||
conn := c.clientTransportConnSnapshot()
|
||||
var conn net.Conn
|
||||
if route.binding != nil {
|
||||
conn = route.binding.connSnapshot()
|
||||
}
|
||||
if conn == nil {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -82,8 +110,9 @@ func clientStreamRequest(runtime *streamRuntime, opt StreamOpenOptions) StreamOp
|
||||
|
||||
func clientStreamCloseSender(c *ClientCommon) streamCloseSender {
|
||||
return func(ctx context.Context, stream *streamHandle, full bool) error {
|
||||
_, err := sendStreamCloseClient(ctx, c, StreamCloseRequest{
|
||||
_, err := sendStreamCloseClientAtRoute(ctx, c, stream.clientSessionRouteSnapshot(), StreamCloseRequest{
|
||||
StreamID: stream.ID(),
|
||||
DataID: stream.dataIDSnapshot(),
|
||||
Full: full,
|
||||
})
|
||||
return err
|
||||
@@ -92,21 +121,23 @@ func clientStreamCloseSender(c *ClientCommon) streamCloseSender {
|
||||
|
||||
func clientStreamResetSender(c *ClientCommon) streamResetSender {
|
||||
return func(ctx context.Context, stream *streamHandle, message string) error {
|
||||
_, err := sendStreamResetClient(ctx, c, StreamResetRequest{
|
||||
StreamID: stream.ID(),
|
||||
Error: message,
|
||||
_, err := sendStreamResetClientAtRoute(ctx, c, stream.clientSessionRouteSnapshot(), StreamResetRequest{
|
||||
StreamID: stream.ID(),
|
||||
DataID: stream.dataIDSnapshot(),
|
||||
Error: message,
|
||||
RecordFailure: stream.recordResetFailure(),
|
||||
})
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func clientStreamDataSender(c *ClientCommon, epoch uint64) streamDataSender {
|
||||
func clientStreamDataSender(c *ClientCommon, route clientSessionRoute) streamDataSender {
|
||||
return func(ctx context.Context, stream *streamHandle, chunk []byte) error {
|
||||
if c == nil {
|
||||
return errStreamClientNil
|
||||
}
|
||||
if epoch != 0 && !c.isClientSessionEpochCurrent(epoch) {
|
||||
return errTransportDetached
|
||||
if err := c.ensureClientSessionRouteSendReady(route); err != nil {
|
||||
return err
|
||||
}
|
||||
if ctx != nil {
|
||||
select {
|
||||
@@ -116,8 +147,17 @@ func clientStreamDataSender(c *ClientCommon, epoch uint64) streamDataSender {
|
||||
}
|
||||
}
|
||||
if dataID := stream.dataIDSnapshot(); dataID != 0 {
|
||||
return c.sendFastStreamData(ctx, stream, chunk)
|
||||
return c.sendFastStreamDataAtRoute(ctx, route, stream, chunk)
|
||||
}
|
||||
return c.sendEnvelope(newStreamDataEnvelope(stream.ID(), chunk))
|
||||
return c.sendEnvelopeAtRoute(route, newStreamDataEnvelope(stream.ID(), chunk))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ClientCommon) bestEffortStreamResetAtRoute(route clientSessionRoute, req StreamResetRequest) {
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), streamDispatchRejectTimeout)
|
||||
defer cancel()
|
||||
_, _ = sendStreamResetClientAtRoute(ctx, c, route, req)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user