fix: close stream adaptive gaps and switch notify to stario v0.1.1
- make stream fast path honor adaptive soft payload limits end-to-end - split oversized fast-stream payloads into sequential frames before batching - use adaptive soft cap when encoding stream batch payloads - move timeout-like error detection into production code for adaptive tx - tune notify FrameReader read size explicitly to avoid throughput regression - drop local stario replace and depend on released b612.me/stario v0.1.1
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
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 encodeStreamFastBatchPlain(frames []streamFastDataFrame) ([]byte, error) {
|
||||
if len(frames) == 0 {
|
||||
return nil, errStreamFastPayloadInvalid
|
||||
}
|
||||
buf := make([]byte, streamFastBatchPlainLen(frames))
|
||||
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 := streamFastBatchPlainLen(frames)
|
||||
return encode(secretKey, plainLen, func(dst []byte) error {
|
||||
return writeStreamFastBatchPlain(dst, frames)
|
||||
})
|
||||
}
|
||||
|
||||
func writeStreamFastBatchPlain(dst []byte, frames []streamFastDataFrame) error {
|
||||
if len(frames) == 0 || len(dst) != streamFastBatchPlainLen(frames) {
|
||||
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 decodeStreamFastBatchPlain(payload []byte) ([]streamFastDataFrame, bool, error) {
|
||||
if len(payload) < 4 || string(payload[:4]) != streamFastBatchMagic {
|
||||
return nil, false, nil
|
||||
}
|
||||
if len(payload) < streamFastBatchHeaderLen {
|
||||
return nil, true, errStreamFastPayloadInvalid
|
||||
}
|
||||
if payload[4] != streamFastBatchVersion {
|
||||
return nil, true, errStreamFastPayloadInvalid
|
||||
}
|
||||
count := int(binary.BigEndian.Uint32(payload[8:12]))
|
||||
if count <= 0 {
|
||||
return nil, true, errStreamFastPayloadInvalid
|
||||
}
|
||||
frames := make([]streamFastDataFrame, 0, count)
|
||||
offset := streamFastBatchHeaderLen
|
||||
for index := 0; index < count; index++ {
|
||||
if len(payload)-offset < streamFastBatchItemHeaderLen {
|
||||
return nil, true, errStreamFastPayloadInvalid
|
||||
}
|
||||
flags := payload[offset]
|
||||
dataID := binary.BigEndian.Uint64(payload[offset+4 : offset+12])
|
||||
seq := binary.BigEndian.Uint64(payload[offset+12 : offset+20])
|
||||
payloadLen := int(binary.BigEndian.Uint32(payload[offset+20 : offset+24]))
|
||||
offset += streamFastBatchItemHeaderLen
|
||||
if dataID == 0 || payloadLen < 0 || len(payload)-offset < payloadLen {
|
||||
return nil, true, errStreamFastPayloadInvalid
|
||||
}
|
||||
frames = append(frames, streamFastDataFrame{
|
||||
Flags: flags,
|
||||
DataID: dataID,
|
||||
Seq: seq,
|
||||
Payload: payload[offset : offset+payloadLen],
|
||||
})
|
||||
offset += payloadLen
|
||||
}
|
||||
if offset != len(payload) {
|
||||
return nil, true, errStreamFastPayloadInvalid
|
||||
}
|
||||
return frames, true, nil
|
||||
}
|
||||
|
||||
func decodeStreamFastDataFrames(payload []byte) ([]streamFastDataFrame, bool, error) {
|
||||
if frames, matched, err := decodeStreamFastBatchPlain(payload); matched {
|
||||
return frames, true, err
|
||||
}
|
||||
frame, matched, err := decodeStreamFastDataFrame(payload)
|
||||
if !matched || err != nil {
|
||||
return nil, matched, err
|
||||
}
|
||||
return []streamFastDataFrame{frame}, true, nil
|
||||
}
|
||||
Reference in New Issue
Block a user