chanconn

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func RunWithAbortWatchdog(ctx context.Context, onAbort func(), op func() error) error

RunWithAbortWatchdog 在 core/conc.RunScoped 监督下执行阻塞操作 op: 一旦 ctx 取消或超时且 op 尚未完成,看门狗协程立即调用 onAbort(通常关闭底层连接或 ssh.Channel) 解除阻塞;op 正常完成时先原子置位 completed 再关闭 doneCh,杜绝 select 随机竞态误伤健康连接。

func WaitBounded

func WaitBounded(done <-chan struct{}, grace time.Duration)

WaitBounded 在最多 grace 时长内等待 done 关闭,避免半开黑洞对端在关闭路径造成永久阻塞。

Types

type Addr

type Addr struct {
	// contains filtered or unexported fields
}

Addr 为基于 ssh.Channel 的虚拟 net.Conn 提供轻量级 net.Addr 实现。

func NewAddr

func NewAddr(network, address string) Addr

NewAddr 构造基于网络类型与地址文本的不可变通道地址。

func (Addr) Network

func (a Addr) Network() string

Network 返回通道地址所属网络类型(如 "tcp"、"unix"、"tun"、"subsystem")。

func (Addr) String

func (a Addr) String() string

String 返回通道地址文本表示。

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) LocalAddr

func (c *WatchedConn) LocalAddr() net.Addr

LocalAddr 返回虚拟通道本端地址。

func (*WatchedConn) Read

func (c *WatchedConn) Read(p []byte) (int, error)

Read 从底层 ssh.Channel 读取字节;受 O(1) 读截止看门狗与半开黑洞即时解阻保护。

func (*WatchedConn) RemoteAddr

func (c *WatchedConn) RemoteAddr() net.Addr

RemoteAddr 返回虚拟通道远端地址。

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) 看门狗;清除或延长截止时立即取消旧到期判断。

func (*WatchedConn) Write

func (c *WatchedConn) Write(p []byte) (int, error)

Write 向底层 ssh.Channel 分块写入字节;受 O(1) 写截止看门狗与半开黑洞即时解阻保护。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL