fix(notify): 根治低带宽控制面阻塞与传输写入卡死

- 为控制消息增加优先级、公平调度、队列字节预算和自适应批处理
- 支持可取消的写门等待,收紧 shared/dedicated bulk、stream 和 Reply 写入边界
- 修复 bulk reset/close、连接 handoff 和安全 profile 切换时序
- 保留旧取消与超时错误契约,新增阶段化 TransportSendError
- 增加 ReplyCtx、ReplyObjCtx、写超时配置及黑洞连接和竞态回归测试
This commit is contained in:
2026-08-14 10:16:37 +08:00
parent 98ef9e7fcc
commit 0826e17063
45 changed files with 4231 additions and 293 deletions
+129 -1
View File
@@ -1,6 +1,7 @@
package notify
import (
"b612.me/stario"
"net"
"testing"
"time"
@@ -10,6 +11,13 @@ func TestClientPeerAttachRenamesAcceptedPeer(t *testing.T) {
secret := []byte("0123456789abcdef0123456789abcdef")
server := newRunningPeerAttachServerForTest(t, func(server *ServerCommon) {
server.SetSecretKey(secret)
if err := UseSignalReliabilityServer(server, &SignalReliabilityOptions{
Enabled: true,
AckTimeout: 200 * time.Millisecond,
SendRetry: 2,
}); err != nil {
t.Fatal(err)
}
})
client := NewClient().(*ClientCommon)
client.SetSecretKey(secret)
@@ -113,7 +121,6 @@ func TestReplyPeerAttachUsesInboundConnWithoutWaitingSignalAck(t *testing.T) {
t.Fatalf("UseSignalReliabilityServer failed: %v", err)
}
})
clientConn, serverConn := net.Pipe()
defer clientConn.Close()
defer serverConn.Close()
@@ -176,3 +183,124 @@ func TestReplyPeerAttachUsesInboundConnWithoutWaitingSignalAck(t *testing.T) {
t.Fatalf("reply key = %q, want %q", transfer.Key, systemPeerAttachKey)
}
}
func TestReplyPeerAttachUsesCapturedProfileWithoutInboundConn(t *testing.T) {
secret := []byte("0123456789abcdef0123456789abcdef")
server := newRunningPeerAttachServerForTest(t, func(server *ServerCommon) {
server.SetSecretKey(secret)
if err := UseSignalReliabilityServer(server, &SignalReliabilityOptions{
Enabled: true,
AckTimeout: 2 * time.Second,
SendRetry: 2,
}); err != nil {
t.Fatal(err)
}
})
clientConn, serverConn := net.Pipe()
defer clientConn.Close()
defer serverConn.Close()
logical := bootstrapPeerAttachLogicalForTest(t, server, serverConn)
originalProfile := logical.transportProtectionProfileSnapshot()
message := Message{
NetType: NET_SERVER,
LogicalConn: logical,
TransportConn: logical.CurrentTransportConn(),
TransferMsg: TransferMsg{
ID: 43,
Key: systemPeerAttachKey,
Type: MSG_SYS_WAIT,
},
Time: time.Now(),
inboundTransportProfile: &originalProfile,
}
alternate, err := deriveModernPSKProtectionProfile([]byte("notify-peer-attach-no-inbound-conn"), testModernPSKOptions(), ProtectionManaged)
if err != nil {
t.Fatalf("deriveModernPSKProtectionProfile failed: %v", err)
}
logical.applyTransportProtectionProfile(alternate)
transition := logical.installInboundTransitionProfile(originalProfile)
defer logical.clearInboundTransitionProfile(transition)
done := make(chan error, 1)
go func() {
done <- server.replyPeerAttach(logical, message, peerAttachResponse{
PeerID: "peer-test",
Accepted: true,
})
}()
env := readServerEnvelopeFromConnWithProfile(t, server, originalProfile, clientConn, time.Second)
if env.Kind != EnvelopeSignal {
t.Fatalf("reply envelope kind = %v, want %v", env.Kind, EnvelopeSignal)
}
ackPlain, err := server.encodeEnvelopePlain(newSignalAckEnvelope(env.ID))
if err != nil {
t.Fatal(err)
}
ackPayload, err := encryptTransportPayloadCodec(originalProfile.mode, originalProfile.runtime, originalProfile.msgEn, originalProfile.secretKey, ackPlain)
if err != nil {
t.Fatal(err)
}
if err := writeFullToConn(clientConn, stario.NewQueue().BuildMessage(ackPayload)); err != nil {
t.Fatalf("write bootstrap signal ack: %v", err)
}
select {
case err := <-done:
if err != nil {
t.Fatalf("replyPeerAttach failed: %v", err)
}
case <-time.After(time.Second):
t.Fatal("replyPeerAttach should finish without an inbound stream conn")
}
}
func TestPeerAttachTransitionProfileDecryptsBootstrapFramesAndClears(t *testing.T) {
bootstrap, err := deriveModernPSKProtectionProfile([]byte("notify-peer-transition-bootstrap"), testModernPSKOptions(), ProtectionManaged)
if err != nil {
t.Fatal(err)
}
steady, err := deriveModernPSKProtectionProfile([]byte("notify-peer-transition-steady"), testModernPSKOptions(), ProtectionManaged)
if err != nil {
t.Fatal(err)
}
payload, err := encryptTransportPayloadCodec(bootstrap.mode, bootstrap.runtime, bootstrap.msgEn, bootstrap.secretKey, []byte("bootstrap-frame"))
if err != nil {
t.Fatal(err)
}
t.Run("client", func(t *testing.T) {
client := NewClient().(*ClientCommon)
client.setClientTransportProtectionProfile(steady)
transition := client.installInboundTransitionProfile(bootstrap)
plain, release, err := client.decryptTransportPayloadPooled(append([]byte(nil), payload...), nil)
if release != nil {
release()
}
if err != nil || string(plain) != "bootstrap-frame" {
t.Fatalf("client transition decrypt plain=%q err=%v", plain, err)
}
client.clearInboundTransitionProfile(transition)
if _, _, err := client.decryptTransportPayloadPooled(append([]byte(nil), payload...), nil); err == nil {
t.Fatal("client accepted bootstrap frame after transition fallback cleared")
}
})
t.Run("server", func(t *testing.T) {
logical := newServerLogicalConn(nil, "transition-server", nil)
logical.applyTransportProtectionProfile(steady)
transition := logical.installInboundTransitionProfile(bootstrap)
plain, release, err := (&ServerCommon{}).decryptTransportPayloadLogicalPooled(logical, append([]byte(nil), payload...), nil)
if release != nil {
release()
}
if err != nil || string(plain) != "bootstrap-frame" {
t.Fatalf("server transition decrypt plain=%q err=%v", plain, err)
}
logical.clearInboundTransitionProfile(transition)
if _, _, err := (&ServerCommon{}).decryptTransportPayloadLogicalPooled(logical, append([]byte(nil), payload...), nil); err == nil {
t.Fatal("server accepted bootstrap frame after transition fallback cleared")
}
})
}