🐛 fix(ssh): 为共享端口转发增加引用计数与失败回滚

This commit is contained in:
Syngnat
2026-07-27 00:07:42 +08:00
parent 546b71945d
commit 76249c00b9
28 changed files with 795 additions and 198 deletions

View File

@@ -144,13 +144,20 @@ func (s *SqlServerDB) getDSN(config connection.ConnectionConfig) string {
return u.String()
}
func (s *SqlServerDB) Connect(config connection.ConnectionConfig) error {
func (s *SqlServerDB) Connect(config connection.ConnectionConfig) (err error) {
_ = s.Close()
defer func() {
if err != nil {
_ = s.Close()
}
}()
var dsn string
if config.UseSSH {
logger.Infof("SQL Server 使用 SSH 连接:地址=%s:%d 用户=%s", config.Host, config.Port, config.User)
forwarder, err := ssh.GetOrCreateLocalForwarder(config.SSH, config.Host, config.Port)
forwarder, err := ssh.AcquireLocalForwarder(config.SSH, config.Host, config.Port)
if err != nil {
return fmt.Errorf("创建 SSH 隧道失败:%w", err)
}
@@ -195,7 +202,7 @@ func (s *SqlServerDB) Connect(config connection.ConnectionConfig) error {
func (s *SqlServerDB) Close() error {
if s.forwarder != nil {
if err := s.forwarder.Close(); err != nil {
if err := s.forwarder.Release(); err != nil {
logger.Warnf("关闭 SQL Server SSH 端口转发失败:%v", err)
}
s.forwarder = nil