feat: 添加定时系统
- 实现定时任务
This commit is contained in:
@@ -115,3 +115,44 @@ func (m *SessionManager) KeepAlive(ctx context.Context, id string, wk *WK) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *SessionManager) ClearAll() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
for sessionID, item := range m.sessions {
|
||||
// 停止 KeepAlive
|
||||
if item.cancel != nil {
|
||||
item.cancel()
|
||||
}
|
||||
|
||||
userKey := item.Instance.Host + ":" + item.Instance.Username
|
||||
delete(m.userToSession, userKey)
|
||||
delete(m.sessions, sessionID)
|
||||
|
||||
log.Info("清理 Session", zap.String("id", sessionID))
|
||||
}
|
||||
|
||||
log.Info("所有 Session 已清空")
|
||||
}
|
||||
|
||||
func (m *SessionManager) ClearExpired(d time.Duration) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
now := time.Now()
|
||||
|
||||
for sessionID, item := range m.sessions {
|
||||
if now.Sub(item.LastValue) > d {
|
||||
if item.cancel != nil {
|
||||
item.cancel()
|
||||
}
|
||||
|
||||
userKey := item.Instance.Host + ":" + item.Instance.Username
|
||||
delete(m.userToSession, userKey)
|
||||
delete(m.sessions, sessionID)
|
||||
|
||||
log.Info("清理过期 Session", zap.String("id", sessionID))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user