mirror of
https://gitee.com/dromara/mayfly-go
synced 2026-01-03 05:06:36 +08:00
fix: 移除隧道连接时检测是否正在使用
This commit is contained in:
@@ -9,6 +9,27 @@ var (
|
||||
poolGroup = pool.NewPoolGroup[*Cli]()
|
||||
)
|
||||
|
||||
func init() {
|
||||
AddCheckSshTunnelMachineUseFunc(func(machineId int) bool {
|
||||
// 遍历所有redis连接实例,若存在redis实例使用该ssh隧道机器,则返回true,表示还在使用中...
|
||||
items := poolGroup.AllPool()
|
||||
for _, v := range items {
|
||||
if v.Stats().TotalConns == 0 {
|
||||
continue // 连接池中没有连接,跳过
|
||||
}
|
||||
cli, err := v.Get(context.Background())
|
||||
if err != nil {
|
||||
continue // 获取连接失败,跳过
|
||||
}
|
||||
sshTunnelMachine := cli.Info.SshTunnelMachine
|
||||
if sshTunnelMachine != nil && sshTunnelMachine.Id == uint64(machineId) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
// 从缓存中获取客户端信息,不存在则回调获取机器信息函数,并新建。
|
||||
// @param 机器的授权凭证名
|
||||
func GetMachineCli(ctx context.Context, authCertName string, getMachine func(string) (*MachineInfo, error)) (*Cli, error) {
|
||||
@@ -39,7 +60,7 @@ func DeleteCli(id uint64) {
|
||||
continue
|
||||
}
|
||||
if conn.Info.Id == id {
|
||||
pool.Close()
|
||||
poolGroup.Close(conn.Info.AuthCertName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,15 @@ import (
|
||||
"mayfly-go/pkg/utils/netx"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
// type SshTunnelAble interface {
|
||||
// GetSshTunnelMachineId() int
|
||||
// }
|
||||
|
||||
var (
|
||||
// 所有检测ssh隧道机器是否被使用的函数
|
||||
checkSshTunnelMachineHasUseFuncs []CheckSshTunnelMachineHasUseFunc
|
||||
@@ -132,7 +137,20 @@ func GetSshTunnelMachine(ctx context.Context, machineId int, getMachine func(uin
|
||||
logx.Infof("connect to the ssh tunnel machine for the first time[%d][%s:%d]", machineId, mi.Ip, mi.Port)
|
||||
|
||||
return stm, err
|
||||
})
|
||||
}, pool.WithIdleTimeout[*SshTunnelMachine](50*time.Minute), pool.WithOnConnClose(func(conn *SshTunnelMachine) error {
|
||||
mid := int(conn.mi.Id)
|
||||
logx.Debugf("periodically check if the ssh tunnel machine [%d] is still in use...", mid)
|
||||
|
||||
for _, checkUseFunc := range checkSshTunnelMachineHasUseFuncs {
|
||||
// 如果一个在使用则返回不关闭,不继续后续检查
|
||||
if checkUseFunc(mid) {
|
||||
return fmt.Errorf("ssh tunnel machine [%s] is still in use", conn.mi.Name)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -142,18 +160,19 @@ func GetSshTunnelMachine(ctx context.Context, machineId int, getMachine func(uin
|
||||
|
||||
// 关闭ssh隧道机器的指定隧道
|
||||
func CloseSshTunnelMachine(machineId uint64, tunnelId string) {
|
||||
//sshTunnelMachine := mcIdPool[machineId]
|
||||
//if sshTunnelMachine == nil {
|
||||
// return
|
||||
//}
|
||||
//
|
||||
//sshTunnelMachine.mutex.Lock()
|
||||
//defer sshTunnelMachine.mutex.Unlock()
|
||||
//t := sshTunnelMachine.tunnels[tunnelId]
|
||||
//if t != nil {
|
||||
// t.Close()
|
||||
// delete(sshTunnelMachine.tunnels, tunnelId)
|
||||
//}
|
||||
sshTunnelMachinePool, ok := tunnelPoolGroup.Get(fmt.Sprintf("machine-tunnel-%d", machineId))
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
sshTunnelMachine, err := sshTunnelMachinePool.Get(context.Background())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
t := sshTunnelMachine.tunnels[tunnelId]
|
||||
if t != nil {
|
||||
t.Close()
|
||||
delete(sshTunnelMachine.tunnels, tunnelId)
|
||||
}
|
||||
}
|
||||
|
||||
type Tunnel struct {
|
||||
|
||||
Reference in New Issue
Block a user