Files
mayfly-go/server/internal/machine/mcm/client_cache.go

87 lines
2.3 KiB
Go
Raw Normal View History

2023-10-30 17:34:56 +08:00
package mcm
import (
"mayfly-go/internal/common/consts"
"mayfly-go/pkg/cache"
2024-02-23 22:53:17 +08:00
"mayfly-go/pkg/logx"
2023-10-30 17:34:56 +08:00
"time"
)
// 机器客户端连接缓存,指定时间内没有访问则会被关闭
var cliCache = cache.NewTimedCache(consts.MachineConnExpireTime, 5*time.Second).
WithUpdateAccessTime(true).
OnEvicted(func(_, value any) {
value.(*Cli).Close()
})
func init() {
AddCheckSshTunnelMachineUseFunc(func(machineId int) bool {
// 遍历所有机器连接实例若存在机器连接实例使用该ssh隧道机器则返回true表示还在使用中...
items := cliCache.Items()
for _, v := range items {
sshTunnelMachine := v.Value.(*Cli).Info.SshTunnelMachine
if sshTunnelMachine != nil && int(sshTunnelMachine.Id) == machineId {
return true
}
}
return false
})
2024-02-23 22:53:17 +08:00
go checkClientAvailability(3 * time.Minute)
2023-10-30 17:34:56 +08:00
}
// 从缓存中获取客户端信息,不存在则回调获取机器信息函数,并新建
func GetMachineCli(machineId uint64, getMachine func(uint64) (*MachineInfo, error)) (*Cli, error) {
if load, ok := cliCache.Get(machineId); ok {
return load.(*Cli), nil
}
me, err := getMachine(machineId)
if err != nil {
return nil, err
}
c, err := me.Conn()
if err != nil {
return nil, err
}
cliCache.Put(machineId, c)
return c, nil
}
2024-02-23 22:53:17 +08:00
// 删除指定机器缓存客户端,并关闭客户端连接
2023-10-30 17:34:56 +08:00
func DeleteCli(id uint64) {
cliCache.Delete(id)
}
2024-02-23 22:53:17 +08:00
// 检查缓存中的客户端是否可用,不可用则关闭客户端连接
func checkClientAvailability(interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for range ticker.C {
// 遍历所有机器连接实例若存在机器连接实例使用该ssh隧道机器则返回true表示还在使用中...
items := cliCache.Items()
for _, v := range items {
if v == nil {
continue
}
2024-02-23 22:53:17 +08:00
cli := v.Value.(*Cli)
if cli.Info == nil {
continue
}
2024-03-02 19:08:19 +08:00
if cli.sshClient == nil {
continue
}
if cli.sshClient.Conn == nil {
continue
}
2024-02-23 22:53:17 +08:00
if _, _, err := cli.sshClient.Conn.SendRequest("ping", true, nil); err != nil {
logx.Errorf("machine[%s] cache client is not available: %s", cli.Info.Name, err.Error())
DeleteCli(cli.Info.Id)
}
logx.Debugf("machine[%s] cache client is available", cli.Info.Name)
}
}
}