1f2e74acca
- 完善 stream/bulk DataID 分配、预留和双向命名空间,修复并发打开及 dedicated/shared 回退时的 ID 冲突 - 将收发、回复、恢复任务和 sidecar 绑定原始会话与物理连接,防止重连后的旧消息误操作新连接 - 加强 close/reset 身份校验及实例移除检查,修复 dedicated attach 失败、通道引用和资源回收竞态 - 收紧批量发送器停止准入,确保在途入队完成后统一清理请求、缓冲区和等待者 - 修复 record 满队列死锁、取消时序号消耗及关闭竞态,确保关闭有界并返回真实错误 - 增加协商式 record 逻辑半关闭,保留反向 ACK;通过 reset 传递 RecordFailure,避免背压掩盖原始失败原因 - 补齐帧长度、批次数量、序号溢出和未确认窗口校验,提前拒绝超限数据并按字节预算拆批 - 为入站分发增加全局及单连接的条数、字节预算和阻塞背压,关闭时唤醒等待者,消除正常断连日志噪音 - 完善 bulk 窗口释放失败处理与传输诊断,补充并发、重连、背压、协议边界及真实 TCP 回归覆盖
206 lines
5.9 KiB
Go
206 lines
5.9 KiB
Go
package notify
|
|
|
|
import "encoding/binary"
|
|
|
|
const (
|
|
streamFastPathVersionV1 = 1
|
|
streamFastPathVersionV2 = 2
|
|
streamFastPathVersionCurrent = streamFastPathVersionV2
|
|
)
|
|
|
|
const (
|
|
streamFastBatchMagic = "NSB1"
|
|
streamFastBatchVersion = 1
|
|
streamFastBatchHeaderLen = 12
|
|
streamFastBatchItemHeaderLen = 24
|
|
streamFastBatchMaxItems = 64
|
|
streamFastBatchMaxPlainBytes = 8 * 1024 * 1024
|
|
)
|
|
|
|
func normalizeStreamFastPathVersion(version uint8) uint8 {
|
|
if version < streamFastPathVersionV1 {
|
|
return streamFastPathVersionV1
|
|
}
|
|
if version > streamFastPathVersionCurrent {
|
|
return streamFastPathVersionCurrent
|
|
}
|
|
return version
|
|
}
|
|
|
|
func negotiateStreamFastPathVersion(version uint8) uint8 {
|
|
return normalizeStreamFastPathVersion(version)
|
|
}
|
|
|
|
func streamFastPathSupportsBatch(version uint8) bool {
|
|
return normalizeStreamFastPathVersion(version) >= streamFastPathVersionV2
|
|
}
|
|
|
|
func streamFastBatchFrameLen(frame streamFastDataFrame) int {
|
|
return streamFastBatchItemHeaderLen + len(frame.Payload)
|
|
}
|
|
|
|
func streamFastBatchPlainLen(frames []streamFastDataFrame) int {
|
|
total := streamFastBatchHeaderLen
|
|
for _, frame := range frames {
|
|
total += streamFastBatchFrameLen(frame)
|
|
}
|
|
return total
|
|
}
|
|
|
|
func streamFastBatchPlainLenChecked(frames []streamFastDataFrame) (int, error) {
|
|
if len(frames) == 0 || len(frames) > streamFastBatchMaxItems {
|
|
return 0, errStreamFastPayloadInvalid
|
|
}
|
|
total := streamFastBatchHeaderLen
|
|
for _, frame := range frames {
|
|
itemLen := streamFastBatchFrameLen(frame)
|
|
if itemLen < streamFastBatchItemHeaderLen || itemLen > streamFastBatchMaxPlainBytes {
|
|
return 0, errStreamFastPayloadInvalid
|
|
}
|
|
if total > streamFastBatchMaxPlainBytes-itemLen {
|
|
return 0, errStreamFastPayloadInvalid
|
|
}
|
|
total += itemLen
|
|
}
|
|
return total, nil
|
|
}
|
|
|
|
func encodeStreamFastBatchPlain(frames []streamFastDataFrame) ([]byte, error) {
|
|
plainLen, err := streamFastBatchPlainLenChecked(frames)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
buf := make([]byte, plainLen)
|
|
if err := writeStreamFastBatchPlain(buf, frames); err != nil {
|
|
return nil, err
|
|
}
|
|
return buf, nil
|
|
}
|
|
|
|
func encodeStreamFastBatchPayloadFast(encode transportFastPlainEncoder, secretKey []byte, frames []streamFastDataFrame) ([]byte, error) {
|
|
if encode == nil {
|
|
return nil, errTransportPayloadEncryptFailed
|
|
}
|
|
plainLen, err := streamFastBatchPlainLenChecked(frames)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return encode(secretKey, plainLen, func(dst []byte) error {
|
|
return writeStreamFastBatchPlain(dst, frames)
|
|
})
|
|
}
|
|
|
|
func writeStreamFastBatchPlain(dst []byte, frames []streamFastDataFrame) error {
|
|
plainLen, err := streamFastBatchPlainLenChecked(frames)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(dst) != plainLen {
|
|
return errStreamFastPayloadInvalid
|
|
}
|
|
copy(dst[:4], streamFastBatchMagic)
|
|
dst[4] = streamFastBatchVersion
|
|
binary.BigEndian.PutUint32(dst[8:12], uint32(len(frames)))
|
|
offset := streamFastBatchHeaderLen
|
|
for _, frame := range frames {
|
|
if frame.DataID == 0 {
|
|
return errStreamFastPayloadInvalid
|
|
}
|
|
dst[offset] = frame.Flags
|
|
binary.BigEndian.PutUint64(dst[offset+4:offset+12], frame.DataID)
|
|
binary.BigEndian.PutUint64(dst[offset+12:offset+20], frame.Seq)
|
|
binary.BigEndian.PutUint32(dst[offset+20:offset+24], uint32(len(frame.Payload)))
|
|
offset += streamFastBatchItemHeaderLen
|
|
copy(dst[offset:offset+len(frame.Payload)], frame.Payload)
|
|
offset += len(frame.Payload)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func walkStreamFastBatchPlain(payload []byte, fn func(streamFastDataFrame) error) (bool, error) {
|
|
if len(payload) < 4 || string(payload[:4]) != streamFastBatchMagic {
|
|
return false, nil
|
|
}
|
|
if len(payload) < streamFastBatchHeaderLen {
|
|
return true, errStreamFastPayloadInvalid
|
|
}
|
|
if payload[4] != streamFastBatchVersion {
|
|
return true, errStreamFastPayloadInvalid
|
|
}
|
|
wireCount := binary.BigEndian.Uint32(payload[8:12])
|
|
if wireCount == 0 || wireCount > streamFastBatchMaxItems {
|
|
return true, errStreamFastPayloadInvalid
|
|
}
|
|
count := int(wireCount)
|
|
offset := streamFastBatchHeaderLen
|
|
for index := 0; index < count; index++ {
|
|
if len(payload)-offset < streamFastBatchItemHeaderLen {
|
|
return true, errStreamFastPayloadInvalid
|
|
}
|
|
flags := payload[offset]
|
|
dataID := binary.BigEndian.Uint64(payload[offset+4 : offset+12])
|
|
seq := binary.BigEndian.Uint64(payload[offset+12 : offset+20])
|
|
wirePayloadLen := binary.BigEndian.Uint32(payload[offset+20 : offset+24])
|
|
offset += streamFastBatchItemHeaderLen
|
|
if dataID == 0 || uint64(wirePayloadLen) > uint64(len(payload)-offset) {
|
|
return true, errStreamFastPayloadInvalid
|
|
}
|
|
payloadLen := int(wirePayloadLen)
|
|
if fn != nil {
|
|
if err := fn(streamFastDataFrame{
|
|
Flags: flags,
|
|
DataID: dataID,
|
|
Seq: seq,
|
|
Payload: payload[offset : offset+payloadLen],
|
|
}); err != nil {
|
|
return true, err
|
|
}
|
|
}
|
|
offset += payloadLen
|
|
}
|
|
if offset != len(payload) {
|
|
return true, errStreamFastPayloadInvalid
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func decodeStreamFastBatchPlain(payload []byte) ([]streamFastDataFrame, bool, error) {
|
|
frames := make([]streamFastDataFrame, 0, 1)
|
|
matched, err := walkStreamFastBatchPlain(payload, func(frame streamFastDataFrame) error {
|
|
frames = append(frames, frame)
|
|
return nil
|
|
})
|
|
if !matched || err != nil {
|
|
return nil, matched, err
|
|
}
|
|
return frames, true, nil
|
|
}
|
|
|
|
func walkStreamFastFrames(payload []byte, fn func(streamFastDataFrame) error) (bool, error) {
|
|
if matched, err := walkStreamFastBatchPlain(payload, fn); matched {
|
|
return true, err
|
|
}
|
|
frame, matched, err := decodeStreamFastDataFrame(payload)
|
|
if !matched || err != nil {
|
|
return matched, err
|
|
}
|
|
if fn != nil {
|
|
if err := fn(frame); err != nil {
|
|
return true, err
|
|
}
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func decodeStreamFastDataFrames(payload []byte) ([]streamFastDataFrame, bool, error) {
|
|
frames := make([]streamFastDataFrame, 0, 1)
|
|
matched, err := walkStreamFastFrames(payload, func(frame streamFastDataFrame) error {
|
|
frames = append(frames, frame)
|
|
return nil
|
|
})
|
|
if !matched || err != nil {
|
|
return nil, matched, err
|
|
}
|
|
return frames, true, nil
|
|
}
|