fix: 仅向已握手连接推送并拆分调度循环

This commit is contained in:
Nixevol
2026-09-30 16:22:43 +08:00
parent ad4f13193c
commit 83521f4b87
14 changed files with 1192 additions and 290 deletions
+21 -2
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"sync"
"time"
"git.asio.asia/nixevol/NixMsg/internal/app/port"
)
@@ -19,6 +20,8 @@ type LiveConn struct {
ConnID port.ConnID
MaxReceiveBytes int
MaxPacketSize uint32
// Ready 为 true 表示已完成 hello,可以推送。分发在线判定不看此字段。
Ready bool
}
// ConnRegistry 查询端是否有连接(由 N 线或测试假实现注入)。
@@ -82,8 +85,11 @@ func (c *MemoryConns) Snapshot() map[string]LiveConn {
type RecordingDownlink struct {
mu sync.Mutex
Published []DownPublish
FailNext int // 接下来 N 次 PublishDown 返回错误
MaxSize int // >0 时超限返回错误
FailNext int // 接下来 N 次 PublishDown 返回错误
FailErr error // FailNext 时返回的错误;空则用 errPublishFailed
MaxSize int // >0 时超限返回错误
Delay time.Duration // 每次发布前休眠
Block <-chan struct{}
}
// DownPublish 是一次下行记录。
@@ -96,6 +102,16 @@ type DownPublish struct {
// PublishDown 实现 port.Downlink。
func (d *RecordingDownlink) PublishDown(_ context.Context, endpointID string, connID port.ConnID, payload []byte, opts port.PublishOpts) error {
d.mu.Lock()
block := d.Block
delay := d.Delay
d.mu.Unlock()
if block != nil {
<-block
}
if delay > 0 {
time.Sleep(delay)
}
d.mu.Lock()
defer d.mu.Unlock()
if d.MaxSize > 0 && len(payload) > d.MaxSize {
@@ -103,6 +119,9 @@ func (d *RecordingDownlink) PublishDown(_ context.Context, endpointID string, co
}
if d.FailNext > 0 {
d.FailNext--
if d.FailErr != nil {
return d.FailErr
}
return errPublishFailed
}
d.Published = append(d.Published, DownPublish{