refactor: 后端代码优化 11 项(codestable/refactors/2026-04-25-backend-cleanup)

- 提取 getWKFromContext 辅助函数,消除 handler 中 5 处重复代码
- 提取 retryCode 函数,消除 Login/performStudy 中验证码重试重复
- 提取 removeSession 内部方法,消除 Del/ClearAll/ClearExpired 中 3 处重复
- 提取 WK.UserKey() 方法,消除 4 处 userKey 手动拼接
- SessionManager.Get() 改用 RLock 优化读性能
- GetRecords 递归分页改为迭代,避免栈溢出
- prepareRequestClient 添加配置缓存,仅在 debug 设置变化时重建
- 修正 schedule.go 时区为 Asia/Shanghai + cron "0 6 * * *"
- 修正 typo "以达到" → "已达到"
- 删除未使用的 QAList struct
- 修复 bufferHub.append 切片内存泄漏

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-26 13:07:45 +08:00
parent 2a6732ffe7
commit 536aa506f9
9 changed files with 488 additions and 94 deletions

View File

@@ -29,12 +29,23 @@ func NewSessionManager() *SessionManager {
}
}
// removeSession 取消 context、删除双 map 条目、记日志
func (m *SessionManager) removeSession(sessionID string, item SessionItem) {
if item.cancel != nil {
item.cancel()
}
userKey := item.Instance.UserKey()
delete(m.userToSession, userKey)
delete(m.sessions, sessionID)
log.Info("删除 Session", zap.String("id", sessionID))
}
// Store: 保存 session 并返回 session id
func (m *SessionManager) Store(wk *WK) string {
m.mu.Lock()
defer m.mu.Unlock()
userKey := wk.Host + ":" + wk.Username
userKey := wk.UserKey()
if oldID, exists := m.userToSession[userKey]; exists {
item := m.sessions[oldID]
if item.cancel != nil {
@@ -78,16 +89,20 @@ func (m *SessionManager) Store(wk *WK) string {
// Get: 获取指定 session id 的 wk
func (m *SessionManager) Get(sessionID string) (*WK, bool) {
m.mu.Lock()
defer m.mu.Unlock()
m.mu.RLock()
item, ok := m.sessions[sessionID]
if ok {
item.LastValue = time.Now()
m.sessions[sessionID] = item
return item.Instance, true
if !ok {
m.mu.RUnlock()
return nil, false
}
return nil, false
m.mu.RUnlock()
m.mu.Lock()
item.LastValue = time.Now()
m.sessions[sessionID] = item
m.mu.Unlock()
return item.Instance, true
}
func (m *SessionManager) Del(sessionID string) {
@@ -95,16 +110,7 @@ func (m *SessionManager) Del(sessionID string) {
defer m.mu.Unlock()
if item, ok := m.sessions[sessionID]; ok {
userKey := item.Instance.Host + ":" + item.Instance.Username
if item.cancel != nil {
item.cancel()
}
delete(m.userToSession, userKey)
delete(m.sessions, sessionID)
log.Info("删除 Session", zap.String("id", sessionID))
m.removeSession(sessionID, item)
}
}
@@ -141,16 +147,7 @@ func (m *SessionManager) ClearAll() {
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))
m.removeSession(sessionID, item)
}
log.Info("所有 Session 已清空")
@@ -164,15 +161,7 @@ func (m *SessionManager) ClearExpired(d time.Duration) {
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))
m.removeSession(sessionID, item)
}
}
}