1f2e74acca
- 完善 stream/bulk DataID 分配、预留和双向命名空间,修复并发打开及 dedicated/shared 回退时的 ID 冲突 - 将收发、回复、恢复任务和 sidecar 绑定原始会话与物理连接,防止重连后的旧消息误操作新连接 - 加强 close/reset 身份校验及实例移除检查,修复 dedicated attach 失败、通道引用和资源回收竞态 - 收紧批量发送器停止准入,确保在途入队完成后统一清理请求、缓冲区和等待者 - 修复 record 满队列死锁、取消时序号消耗及关闭竞态,确保关闭有界并返回真实错误 - 增加协商式 record 逻辑半关闭,保留反向 ACK;通过 reset 传递 RecordFailure,避免背压掩盖原始失败原因 - 补齐帧长度、批次数量、序号溢出和未确认窗口校验,提前拒绝超限数据并按字节预算拆批 - 为入站分发增加全局及单连接的条数、字节预算和阻塞背压,关闭时唤醒等待者,消除正常断连日志噪音 - 完善 bulk 窗口释放失败处理与传输诊断,补充并发、重连、背压、协议边界及真实 TCP 回归覆盖
239 lines
7.6 KiB
Go
239 lines
7.6 KiB
Go
package notify
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
const streamDispatchRejectTimeout = 300 * time.Millisecond
|
|
|
|
func (c *ClientCommon) dispatchStreamEnvelope(env Envelope) {
|
|
route := c.clientSessionRouteSnapshot()
|
|
if route.epoch == 0 {
|
|
route.epoch = c.currentClientSessionEpoch()
|
|
}
|
|
c.dispatchStreamEnvelopeAtRoute(route, env)
|
|
}
|
|
|
|
func (c *ClientCommon) dispatchStreamEnvelopeAtRoute(route clientSessionRoute, env Envelope) {
|
|
streamID := env.Stream.StreamID
|
|
if streamID == "" {
|
|
return
|
|
}
|
|
if route.binding != nil && !c.clientSessionRouteCurrent(route) {
|
|
return
|
|
}
|
|
runtime := c.getStreamRuntime()
|
|
if runtime == nil {
|
|
return
|
|
}
|
|
stream, ok := runtime.lookup(clientFileScope(), streamID)
|
|
if !ok {
|
|
if c.showError || c.debugMode {
|
|
fmt.Println("client stream data for unknown stream", streamID)
|
|
}
|
|
c.bestEffortRejectInboundStreamDataAtRoute(route, streamID, 0, errStreamNotFound.Error())
|
|
return
|
|
}
|
|
if !stream.acceptsClientSessionRoute(route) {
|
|
if c.showError || c.debugMode {
|
|
fmt.Println("client stream data rejected by stale session epoch", streamID)
|
|
}
|
|
detachErr := transportDetachedSessionEpochError()
|
|
stream.markReset(detachErr)
|
|
c.bestEffortRejectInboundStreamDataAtRoute(route, streamID, stream.dataIDSnapshot(), detachErr.Error())
|
|
return
|
|
}
|
|
if err := stream.pushChunk(env.Stream.Chunk); err != nil {
|
|
if c.showError || c.debugMode {
|
|
fmt.Println("client stream push chunk error", err)
|
|
}
|
|
if !errors.Is(err, io.EOF) {
|
|
c.bestEffortRejectInboundStreamDataAtRoute(route, streamID, stream.dataIDSnapshot(), err.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *ServerCommon) dispatchStreamEnvelope(logical *LogicalConn, transport *TransportConn, conn net.Conn, env Envelope) {
|
|
streamID := env.Stream.StreamID
|
|
if streamID == "" || logical == nil {
|
|
return
|
|
}
|
|
runtime := s.getStreamRuntime()
|
|
if runtime == nil {
|
|
return
|
|
}
|
|
stream, ok := runtime.lookup(serverFileScope(logical), streamID)
|
|
if !ok {
|
|
if s.showError || s.debugMode {
|
|
fmt.Println("server stream data for unknown stream", streamID)
|
|
}
|
|
s.bestEffortRejectInboundStreamData(logical, transport, conn, streamID, 0, errStreamNotFound.Error())
|
|
return
|
|
}
|
|
if !stream.acceptsTransportGeneration(transport) {
|
|
if s.showError || s.debugMode {
|
|
fmt.Println("server stream data rejected by transport generation mismatch", streamID)
|
|
}
|
|
detachErr := transportDetachedGenerationMismatchError(stream.TransportGeneration(), transport)
|
|
s.bestEffortRejectInboundStreamData(logical, transport, conn, streamID, stream.dataIDSnapshot(), detachErr.Error())
|
|
return
|
|
}
|
|
if err := stream.pushChunk(env.Stream.Chunk); err != nil {
|
|
if s.showError || s.debugMode {
|
|
fmt.Println("server stream push chunk error", err)
|
|
}
|
|
if !errors.Is(err, io.EOF) {
|
|
s.bestEffortRejectInboundStreamData(logical, transport, conn, streamID, stream.dataIDSnapshot(), err.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *ClientCommon) dispatchFastStreamData(frame streamFastDataFrame) {
|
|
route := c.clientSessionRouteSnapshot()
|
|
if route.epoch == 0 {
|
|
route.epoch = c.currentClientSessionEpoch()
|
|
}
|
|
c.dispatchFastStreamDataWithOwnerAtRoute(route, frame, nil)
|
|
}
|
|
|
|
func (c *ClientCommon) dispatchFastStreamDataWithOwner(frame streamFastDataFrame, owner *streamReadPayloadOwner) {
|
|
route := c.clientSessionRouteSnapshot()
|
|
if route.epoch == 0 {
|
|
route.epoch = c.currentClientSessionEpoch()
|
|
}
|
|
c.dispatchFastStreamDataWithOwnerAtRoute(route, frame, owner)
|
|
}
|
|
|
|
func (c *ClientCommon) dispatchFastStreamDataWithOwnerAtRoute(route clientSessionRoute, frame streamFastDataFrame, owner *streamReadPayloadOwner) {
|
|
if frame.DataID == 0 {
|
|
return
|
|
}
|
|
if route.binding != nil && !c.clientSessionRouteCurrent(route) {
|
|
return
|
|
}
|
|
runtime := c.getStreamRuntime()
|
|
if runtime == nil {
|
|
return
|
|
}
|
|
stream, ok := runtime.lookupByDataID(clientFileScope(), frame.DataID)
|
|
if !ok {
|
|
if c.showError || c.debugMode {
|
|
fmt.Println("client stream data for unknown data id", frame.DataID)
|
|
}
|
|
c.bestEffortRejectInboundStreamDataAtRoute(route, "", frame.DataID, errStreamNotFound.Error())
|
|
return
|
|
}
|
|
if !stream.acceptsClientSessionRoute(route) {
|
|
if c.showError || c.debugMode {
|
|
fmt.Println("client stream data rejected by stale session epoch", frame.DataID)
|
|
}
|
|
detachErr := transportDetachedSessionEpochError()
|
|
stream.markReset(detachErr)
|
|
c.bestEffortRejectInboundStreamDataAtRoute(route, stream.ID(), frame.DataID, detachErr.Error())
|
|
return
|
|
}
|
|
var err error
|
|
if owner != nil {
|
|
err = stream.pushOwnedChunkWithRelease(frame.Payload, owner.retainChunk())
|
|
} else {
|
|
err = stream.pushOwnedChunk(frame.Payload)
|
|
}
|
|
if err != nil {
|
|
if c.showError || c.debugMode {
|
|
fmt.Println("client stream push chunk error", err)
|
|
}
|
|
if !errors.Is(err, io.EOF) {
|
|
c.bestEffortRejectInboundStreamDataAtRoute(route, stream.ID(), frame.DataID, err.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *ServerCommon) dispatchFastStreamData(logical *LogicalConn, transport *TransportConn, conn net.Conn, frame streamFastDataFrame) {
|
|
s.dispatchFastStreamDataWithOwner(logical, transport, conn, frame, nil)
|
|
}
|
|
|
|
func (s *ServerCommon) dispatchFastStreamDataWithOwner(logical *LogicalConn, transport *TransportConn, conn net.Conn, frame streamFastDataFrame, owner *streamReadPayloadOwner) {
|
|
if logical == nil || frame.DataID == 0 {
|
|
return
|
|
}
|
|
runtime := s.getStreamRuntime()
|
|
if runtime == nil {
|
|
return
|
|
}
|
|
stream, ok := runtime.lookupByDataID(serverFileScope(logical), frame.DataID)
|
|
if !ok {
|
|
if s.showError || s.debugMode {
|
|
fmt.Println("server stream data for unknown data id", frame.DataID)
|
|
}
|
|
s.bestEffortRejectInboundStreamData(logical, transport, conn, "", frame.DataID, errStreamNotFound.Error())
|
|
return
|
|
}
|
|
if !stream.acceptsTransportGeneration(transport) {
|
|
if s.showError || s.debugMode {
|
|
fmt.Println("server stream data rejected by transport generation mismatch", frame.DataID)
|
|
}
|
|
detachErr := transportDetachedGenerationMismatchError(stream.TransportGeneration(), transport)
|
|
s.bestEffortRejectInboundStreamData(logical, transport, conn, stream.ID(), frame.DataID, detachErr.Error())
|
|
return
|
|
}
|
|
var err error
|
|
if owner != nil {
|
|
err = stream.pushOwnedChunkWithRelease(frame.Payload, owner.retainChunk())
|
|
} else {
|
|
err = stream.pushOwnedChunk(frame.Payload)
|
|
}
|
|
if err != nil {
|
|
if s.showError || s.debugMode {
|
|
fmt.Println("server stream push chunk error", err)
|
|
}
|
|
if !errors.Is(err, io.EOF) {
|
|
s.bestEffortRejectInboundStreamData(logical, transport, conn, stream.ID(), frame.DataID, err.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *ClientCommon) bestEffortRejectInboundStreamData(streamID string, dataID uint64, message string) {
|
|
c.bestEffortRejectInboundStreamDataAtRoute(c.clientSessionRouteSnapshot(), streamID, dataID, message)
|
|
}
|
|
|
|
func (c *ClientCommon) bestEffortRejectInboundStreamDataAtRoute(route clientSessionRoute, streamID string, dataID uint64, message string) {
|
|
if c == nil || (streamID == "" && dataID == 0) {
|
|
return
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), streamDispatchRejectTimeout)
|
|
defer cancel()
|
|
_, _ = sendStreamResetClientAtRoute(ctx, c, route, StreamResetRequest{
|
|
StreamID: streamID,
|
|
DataID: dataID,
|
|
Error: message,
|
|
})
|
|
}
|
|
|
|
func (s *ServerCommon) bestEffortRejectInboundStreamData(logical *LogicalConn, transport *TransportConn, conn net.Conn, streamID string, dataID uint64, message string) {
|
|
if s == nil || logical == nil || (streamID == "" && dataID == 0) {
|
|
return
|
|
}
|
|
payload, err := encode(StreamResetRequest{
|
|
StreamID: streamID,
|
|
DataID: dataID,
|
|
Error: message,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
env, err := wrapTransferMsgEnvelope(TransferMsg{
|
|
Key: StreamResetSignalKey,
|
|
Value: payload,
|
|
Type: MSG_ASYNC,
|
|
}, s.sequenceEn)
|
|
if err != nil {
|
|
return
|
|
}
|
|
_ = s.sendEnvelopeInboundTransport(logical, transport, conn, env)
|
|
}
|