Files
notify/server_inbound_source.go
T
b612 1f2e74acca fix(notify): 修复传输生命周期竞态,完善背压与协议边界
- 完善 stream/bulk DataID 分配、预留和双向命名空间,修复并发打开及 dedicated/shared 回退时的 ID 冲突
- 将收发、回复、恢复任务和 sidecar 绑定原始会话与物理连接,防止重连后的旧消息误操作新连接
- 加强 close/reset 身份校验及实例移除检查,修复 dedicated attach 失败、通道引用和资源回收竞态
- 收紧批量发送器停止准入,确保在途入队完成后统一清理请求、缓冲区和等待者
- 修复 record 满队列死锁、取消时序号消耗及关闭竞态,确保关闭有界并返回真实错误
- 增加协商式 record 逻辑半关闭,保留反向 ACK;通过 reset 传递 RecordFailure,避免背压掩盖原始失败原因
- 补齐帧长度、批次数量、序号溢出和未确认窗口校验,提前拒绝超限数据并按字节预算拆批
- 为入站分发增加全局及单连接的条数、字节预算和阻塞背压,关闭时唤醒等待者,消除正常断连日志噪音
- 完善 bulk 窗口释放失败处理与传输诊断,补充并发、重连、背压、协议边界及真实 TCP 回归覆盖
2026-09-23 15:33:17 +08:00

188 lines
5.0 KiB
Go

package notify
import (
"b612.me/stario"
"fmt"
"net"
"time"
)
type serverInboundSource struct {
Source string
Logical *LogicalConn
Conn net.Conn
RemoteAddr net.Addr
TransportGeneration uint64
HasRuntimeConn bool
}
func newServerInboundSource(logical *LogicalConn, conn net.Conn, remoteAddr net.Addr, generation uint64) serverInboundSource {
if remoteAddr == nil && conn != nil {
remoteAddr = conn.RemoteAddr()
}
source := ""
if conn != nil && conn.RemoteAddr() != nil {
source = conn.RemoteAddr().String()
}
if source == "" && logical != nil && logical.ID() != "" {
source = logical.ID()
}
if source == "" && remoteAddr != nil {
source = remoteAddr.String()
}
if source == "" && logical != nil && logical.RemoteAddr() != nil {
source = logical.RemoteAddr().String()
}
return serverInboundSource{
Source: source,
Logical: logical,
Conn: conn,
RemoteAddr: remoteAddr,
TransportGeneration: generation,
HasRuntimeConn: conn != nil,
}
}
func (s *ServerCommon) pushMessageSource(data []byte, source interface{}) {
queue := s.serverQueueSnapshot()
if queue == nil || len(data) == 0 {
return
}
if s.pushMessageSourceFast(queue, data, source) {
return
}
_ = queue.ParseMessage(data, source)
}
func (s *ServerCommon) pushMessageSourceFast(queue *stario.StarQueue, data []byte, source interface{}) bool {
dispatcher := s.serverInboundDispatcherSnapshot()
if queue == nil || dispatcher == nil || len(data) == 0 {
return false
}
if err := queue.ParseMessageView(data, source, func(frame stario.FrameView) error {
s.pushTransportPayloadSourceFast(frame.Payload, nil, frame.Conn)
return nil
}); err != nil && (s.showError || s.debugMode) {
fmt.Println("server parse inbound frame error", err)
}
return true
}
func (s *ServerCommon) pushTransportPayloadSourceFast(payload []byte, release func(), source interface{}) bool {
dispatcher := s.serverInboundDispatcherSnapshot()
if len(payload) == 0 {
if release != nil {
release()
}
return false
}
if dispatcher == nil {
queue := s.serverQueueSnapshot()
if queue == nil {
if release != nil {
release()
}
return false
}
if err := validateTransportFramePayloadLen(payload); err != nil {
if release != nil {
release()
}
if s.showError || s.debugMode {
fmt.Println("server enqueue inbound frame error", err)
}
return true
}
frame := queue.BuildMessage(payload)
if release != nil {
release()
}
if err := queue.ParseMessage(frame, source); err != nil && (s.showError || s.debugMode) {
fmt.Println("server enqueue inbound frame error", err)
}
return true
}
logical, transport := s.resolveInboundSource(source)
if logical == nil {
if release != nil {
release()
}
return true
}
plain, plainRelease, err := s.decryptTransportPayloadLogicalPooled(logical, payload, release)
if err != nil {
if s.showError || s.debugMode {
fmt.Println("server decode transport payload error", err)
}
return true
}
inboundConn := serverInboundConn(source)
if s.tryDispatchBorrowedTransportPlain(logical, transport, inboundConn, plain, plainRelease) {
return true
}
owned := plain
if plainRelease != nil {
owned = append([]byte(nil), plain...)
plainRelease()
}
s.wg.Add(1)
if !dispatcher.DispatchSized(serverInboundDispatchSource(source), len(owned), func() {
defer s.wg.Done()
now := time.Now()
if err := s.dispatchInboundTransportPlain(logical, transport, inboundConn, owned, now); err != nil && (s.showError || s.debugMode) {
fmt.Println("server decode envelope error", err)
}
}) {
s.wg.Done()
}
return true
}
func serverInboundConn(source interface{}) net.Conn {
switch data := source.(type) {
case net.Conn:
return data
case serverInboundSource:
return data.Conn
case *serverInboundSource:
if data != nil {
return data.Conn
}
}
return nil
}
func (s *ServerCommon) resolveInboundSource(source interface{}) (*LogicalConn, *TransportConn) {
switch data := source.(type) {
case serverInboundSource:
return s.resolveInboundSourceValue(data)
case *serverInboundSource:
if data == nil {
return nil, nil
}
return s.resolveInboundSourceValue(*data)
case string:
return s.resolveLogicalBySource(data), nil
default:
return nil, nil
}
}
func (s *ServerCommon) resolveInboundSourceValue(source serverInboundSource) (*LogicalConn, *TransportConn) {
logical := source.Logical
if logical == nil {
logical = s.resolveLogicalBySource(source.Source)
} else if source.HasRuntimeConn {
transport := logical.transportConnSnapshotForInbound(source.Conn, source.RemoteAddr, source.TransportGeneration, source.HasRuntimeConn)
if transport == nil || !transport.Attached() {
if rebound := s.resolveLogicalBySource(source.Source); rebound != nil {
logical = rebound
} else if !logical.Status().Alive {
return nil, nil
}
}
}
transport := logical.transportConnSnapshotForInbound(source.Conn, source.RemoteAddr, source.TransportGeneration, source.HasRuntimeConn)
return logical, transport
}