@@ -46,6 +46,7 @@ type ConsoleSession struct {
|
||||
type ConsoleService struct {
|
||||
configs *OciConfigService
|
||||
pollInterval time.Duration // 清理残留连接的轮询间隔,测试注入缩短
|
||||
sessionTTL time.Duration // 会话回收检查周期,测试注入缩短
|
||||
|
||||
mu sync.Mutex
|
||||
sessions map[string]*ConsoleSession
|
||||
@@ -55,6 +56,7 @@ func NewConsoleService(configs *OciConfigService) *ConsoleService {
|
||||
return &ConsoleService{
|
||||
configs: configs,
|
||||
pollInterval: 2 * time.Second,
|
||||
sessionTTL: consoleSessionTTL,
|
||||
sessions: map[string]*ConsoleSession{},
|
||||
}
|
||||
}
|
||||
@@ -161,11 +163,12 @@ func (s *ConsoleService) storeSession(cfgID uint, instanceID, region, typ, connI
|
||||
s.mu.Lock()
|
||||
s.sessions[sess.ID] = sess
|
||||
s.mu.Unlock()
|
||||
time.AfterFunc(consoleSessionTTL, func() { s.expire(sess.ID) })
|
||||
time.AfterFunc(s.sessionTTL, func() { s.expire(sess.ID) })
|
||||
return sess
|
||||
}
|
||||
|
||||
// expire TTL 到期回收:正在使用的会话跳过(连接断开后自然停止,无续期)。
|
||||
// expire TTL 到期回收:正在使用的会话跳过并重挂下一轮检查,
|
||||
// 连接断开后由后续轮次回收,避免会话与云端连接常驻到进程退出。
|
||||
func (s *ConsoleService) expire(id string) {
|
||||
s.mu.Lock()
|
||||
sess, ok := s.sessions[id]
|
||||
@@ -173,6 +176,7 @@ func (s *ConsoleService) expire(id string) {
|
||||
sess.mu.Lock()
|
||||
if sess.inUse {
|
||||
ok = false
|
||||
time.AfterFunc(s.sessionTTL, func() { s.expire(id) })
|
||||
} else {
|
||||
delete(s.sessions, id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user