- 引入 LogicalConn/TransportConn 分层,ClientConn 保留兼容适配层 - 新增 Stream、Bulk、RecordStream 三条数据面能力及对应控制路径 - 完成 transfer/file 传输内核与状态快照、诊断能力 - 补齐 reconnect、inbound dispatcher、modern psk 等基础模块 - 增加大规模回归、并发与基准测试覆盖 - 更新依赖库
117 lines
2.3 KiB
Go
117 lines
2.3 KiB
Go
package notify
|
|
|
|
import (
|
|
"b612.me/stario"
|
|
"net"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// transportBinding models the currently attached physical transport for a
|
|
// logical session. The binding can be swapped later without forcing callers to
|
|
// reach into raw conn fields directly.
|
|
type transportBinding struct {
|
|
conn net.Conn
|
|
queue *stario.StarQueue
|
|
writeMu sync.Mutex
|
|
|
|
controlMu sync.Mutex
|
|
controlSender *controlBatchSender
|
|
|
|
bulkMu sync.Mutex
|
|
bulkSender *bulkBatchSender
|
|
}
|
|
|
|
func newTransportBinding(conn net.Conn, queue *stario.StarQueue) *transportBinding {
|
|
if conn == nil && queue == nil {
|
|
return nil
|
|
}
|
|
return &transportBinding{
|
|
conn: conn,
|
|
queue: queue,
|
|
}
|
|
}
|
|
|
|
func (b *transportBinding) connSnapshot() net.Conn {
|
|
if b == nil {
|
|
return nil
|
|
}
|
|
return b.conn
|
|
}
|
|
|
|
func (b *transportBinding) queueSnapshot() *stario.StarQueue {
|
|
if b == nil {
|
|
return nil
|
|
}
|
|
return b.queue
|
|
}
|
|
|
|
func (b *transportBinding) withConnWriteLock(fn func(net.Conn) error) error {
|
|
return b.withConnWriteLockDeadline(time.Time{}, fn)
|
|
}
|
|
|
|
func (b *transportBinding) withConnWriteLockDeadline(deadline time.Time, fn func(net.Conn) error) error {
|
|
if b == nil {
|
|
return net.ErrClosed
|
|
}
|
|
b.writeMu.Lock()
|
|
defer b.writeMu.Unlock()
|
|
conn := b.connSnapshot()
|
|
if conn == nil {
|
|
return net.ErrClosed
|
|
}
|
|
if !deadline.IsZero() {
|
|
if err := conn.SetWriteDeadline(deadline); err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
_ = conn.SetWriteDeadline(time.Time{})
|
|
}()
|
|
}
|
|
return fn(conn)
|
|
}
|
|
|
|
func (b *transportBinding) bulkBatchSenderSnapshot() *bulkBatchSender {
|
|
if b == nil {
|
|
return nil
|
|
}
|
|
b.bulkMu.Lock()
|
|
defer b.bulkMu.Unlock()
|
|
if b.bulkSender != nil {
|
|
return b.bulkSender
|
|
}
|
|
b.bulkSender = newBulkBatchSender(b)
|
|
return b.bulkSender
|
|
}
|
|
|
|
func (b *transportBinding) controlBatchSenderSnapshot() *controlBatchSender {
|
|
if b == nil {
|
|
return nil
|
|
}
|
|
b.controlMu.Lock()
|
|
defer b.controlMu.Unlock()
|
|
if b.controlSender != nil {
|
|
return b.controlSender
|
|
}
|
|
b.controlSender = newControlBatchSender(b)
|
|
return b.controlSender
|
|
}
|
|
|
|
func (b *transportBinding) stopBackgroundWorkers() {
|
|
if b == nil {
|
|
return
|
|
}
|
|
b.controlMu.Lock()
|
|
controlSender := b.controlSender
|
|
b.controlMu.Unlock()
|
|
b.bulkMu.Lock()
|
|
bulkSender := b.bulkSender
|
|
b.bulkMu.Unlock()
|
|
if controlSender != nil {
|
|
controlSender.stop()
|
|
}
|
|
if bulkSender != nil {
|
|
bulkSender.stop()
|
|
}
|
|
}
|