Documentation
¶
Overview ¶
Package chanconn 为 XOps v2 的 ssh.Channel 虚拟连接提供受 core/conc.Nursery 监督的 取消/截止时间看门狗与多路复用操作原语(ADR-009 / ADR-015)。
本包隶属 core/internal/sshnet/dial 体系,供 sshnet/dial 及后续 sshnet/{sftp,forward,mux} 复用:
- 全包 0 裸 go 语句,所有后台看门狗、带外排空与脱离读写均受 core/conc.Nursery 托管;
- WatchedConn 将无原生 SetDeadline 的 ssh.Channel 封装为标准 net.Conn,在半开黑洞或 截止到期时立即解阻并零协程泄漏收敛;
- RunWithAbortWatchdog 基于 core/conc.RunScoped 保护单次阻塞操作(如 SSH 握手)。
Index ¶
- Constants
- func RunMultiplexedOp[T any](ctx context.Context, lifeNursery *conc.Nursery, op func() (T, error), ...) (T, error)
- func RunWithAbortWatchdog(ctx context.Context, onAbort func(), op func() error) error
- func WaitBounded(done <-chan struct{}, grace time.Duration)
- type Addr
- type WatchedConn
- func (c *WatchedConn) Close() error
- func (c *WatchedConn) CloseWrite() error
- func (c *WatchedConn) LocalAddr() net.Addr
- func (c *WatchedConn) Read(p []byte) (int, error)
- func (c *WatchedConn) RemoteAddr() net.Addr
- func (c *WatchedConn) SetDeadline(t time.Time) error
- func (c *WatchedConn) SetReadDeadline(t time.Time) error
- func (c *WatchedConn) SetWriteDeadline(t time.Time) error
- func (c *WatchedConn) Write(p []byte) (int, error)
Constants ¶
const ( // MaxCopyChunk 定义通道分块读写与拷贝的默认单次缓冲上限(32 KiB)。 MaxCopyChunk = 32 * 1024 // CloseGraceTimeout 定义关闭通道或会话时等待后台排空协程收敛的有界宽限时长。 CloseGraceTimeout = 5 * time.Millisecond )
Variables ¶
This section is empty.
Functions ¶
func RunMultiplexedOp ¶
func RunMultiplexedOp[T any]( ctx context.Context, lifeNursery *conc.Nursery, op func() (T, error), cleanup func(T), ) (T, error)
RunMultiplexedOp 在连接生命周期 Nursery 监督下执行单次多路复用控制操作: 单一调用方取消 ctx 时绝不关闭共享父连接(避免误杀同连接上其他活跃会话或子跳), 并在互斥锁线性化下保证迟到完成的孤立资源恰好被 cleanup 释放一次且随父连接关闭零泄漏收敛。
func RunWithAbortWatchdog ¶
RunWithAbortWatchdog 在 core/conc.RunScoped 监督下执行阻塞操作 op: 一旦 ctx 取消或超时且 op 尚未完成,看门狗协程立即调用 onAbort(通常关闭底层连接或 ssh.Channel) 解除阻塞;op 正常完成时先原子置位 completed 再关闭 doneCh,杜绝 select 随机竞态误伤健康连接。
func WaitBounded ¶
WaitBounded 在最多 grace 时长内等待 done 关闭,避免半开黑洞对端在关闭路径造成永久阻塞。
Types ¶
type Addr ¶
type Addr struct {
// contains filtered or unexported fields
}
Addr 为基于 ssh.Channel 的虚拟 net.Conn 提供轻量级 net.Addr 实现。
type WatchedConn ¶
type WatchedConn struct {
// contains filtered or unexported fields
}
WatchedConn 将不原生支持 SetDeadline 的 ssh.Channel 封装为带强制取消/截止时间看门狗的 net.Conn(ADR-015)。
生命周期与半开黑洞防挂死不变量:
- 所有带外排空、O(1) 截止看门狗与脱离的 Read/Write 均受上游 lifeNursery 统一托管,零裸 go;
- 仅当具体方向存在活跃 I/O 且该方向当前截止时间确已到期时才中止通道,清除/延长截止或空闲态永不误杀;
- 脱离的 Read 读入内部独立缓冲、Write 拷贝内部切片,通道中止返回后绝不触碰调用方切片(-race 安全);
- 对端进入半开黑洞(不回传 SSH_MSG_CHANNEL_CLOSE)时,abortCh 立即唤醒 Read/Write 与上层握手, Close() 经有界宽限返回且不伤健康父连接,父连接 Close() 关底层 TCP 时 100% 回收全部协程(零泄漏)。
func WrapChannel ¶
func WrapChannel( lifeNursery *conc.Nursery, ch ssh.Channel, reqs <-chan *ssh.Request, localAddr, remoteAddr net.Addr, clk ports.Clock, ) (*WatchedConn, error)
WrapChannel 创建受 lifeNursery 监督的 ssh.Channel 包装器并启动带外排空与 O(1) 截止看门狗。
func (*WatchedConn) Close ¶
func (c *WatchedConn) Close() error
Close 幂等关闭底层 ssh.Channel、立即唤醒阻塞读写与 O(1) 看门狗,并在有界宽限内收敛带外排空。
func (*WatchedConn) CloseWrite ¶
func (c *WatchedConn) CloseWrite() error
CloseWrite 半关闭底层 ssh.Channel 的写方向(发送 SSH_MSG_CHANNEL_EOF),供 SFTP/透传子系统使用。
func (*WatchedConn) Read ¶
func (c *WatchedConn) Read(p []byte) (int, error)
Read 从底层 ssh.Channel 读取字节;受 O(1) 读截止看门狗与半开黑洞即时解阻保护。
func (*WatchedConn) SetDeadline ¶
func (c *WatchedConn) SetDeadline(t time.Time) error
SetDeadline 原子更新读写截止时间并通知 O(1) 看门狗;无活跃 I/O 时绝不提前销毁通道。
func (*WatchedConn) SetReadDeadline ¶
func (c *WatchedConn) SetReadDeadline(t time.Time) error
SetReadDeadline 原子更新读截止时间并通知 O(1) 看门狗;清除或延长截止时立即取消旧到期判断。
func (*WatchedConn) SetWriteDeadline ¶
func (c *WatchedConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline 原子更新写截止时间并通知 O(1) 看门狗;清除或延长截止时立即取消旧到期判断。