refactor: 提取 service 层 best-effort 超时常量

- 新增 defaultBETimeout = 5 * time.Second
- 替换 auth/auth_runtime/password_reset/user_service/webhook 中 6 处硬编码 5*time.Second
This commit is contained in:
2026-05-08 12:44:05 +08:00
parent 9ad7b5c0df
commit 2ecd1fef1e
5 changed files with 8 additions and 7 deletions

View File

@@ -30,6 +30,7 @@ const (
defaultTOTPChallengeTTL = 5 * time.Minute
defaultPasswordMinLen = 8
refreshTokenRetryGrace = 10 * time.Second
defaultBETimeout = 5 * time.Second // best-effort 后台操作默认超时
MaxUsernameAttempts = 100 // 最大尝试次数P1性能优化减少循环查询
MaxUsernameLength = 40 // 用户名最大长度
)
@@ -553,7 +554,7 @@ func (s *AuthService) writeLoginLog(
log.Printf("auth: write login log panic recovered, user_id=%v login_type=%d err=%v", userID, loginType, r)
}
}()
bgCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
bgCtx, cancel := context.WithTimeout(context.Background(), defaultBETimeout)
defer cancel()
if err := s.loginLogRepo.Create(bgCtx, loginRecord); err != nil {
log.Printf("auth: write login log failed, user_id=%v login_type=%d err=%v", userID, loginType, err)
@@ -634,7 +635,7 @@ func (s *AuthService) bestEffortRegisterDevice(ctx context.Context, userID int64
log.Printf("auth: register device panic recovered, user_id=%d device_id=%s err=%v", userID, req.DeviceID, r)
}
}()
bgCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
bgCtx, cancel := context.WithTimeout(context.Background(), defaultBETimeout)
defer cancel()
_, _ = s.deviceService.CreateDevice(bgCtx, userID, createReq)
}()

View File

@@ -95,7 +95,7 @@ func (s *AuthService) bestEffortUpdateLastLogin(ctx context.Context, userID int6
log.Printf("auth: update last login panic recovered, source=%s user_id=%d err=%v", source, userID, r)
}
}()
bgCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
bgCtx, cancel := context.WithTimeout(context.Background(), defaultBETimeout)
defer cancel()
if err := s.userRepo.UpdateLastLogin(bgCtx, userID, ip); err != nil {
log.Printf("auth: update last login failed, source=%s user_id=%d ip=%s err=%v", source, userID, ip, err)

View File

@@ -299,7 +299,7 @@ func (s *PasswordResetService) doResetPassword(ctx context.Context, user *domain
if s.passwordHistoryRepo != nil {
// #nosec G118 - 使用带超时的独立 context防止 DB 写入无限等待
go func() { // #nosec G118
bgCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
bgCtx, cancel := context.WithTimeout(context.Background(), defaultBETimeout)
defer cancel()
_ = s.passwordHistoryRepo.Create(bgCtx, &domain.PasswordHistory{
UserID: user.ID,

View File

@@ -132,7 +132,7 @@ func (s *UserService) ChangePassword(ctx context.Context, userID int64, oldPassw
if s.passwordHistoryRepo != nil {
// #nosec G118 - 使用带超时的独立 context不能使用请求 ctx该 goroutine 在请求完成后仍可能运行)
go func(hashedPw string) { // #nosec G118
bgCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
bgCtx, cancel := context.WithTimeout(context.Background(), defaultBETimeout)
defer cancel()
_ = s.passwordHistoryRepo.Create(bgCtx, &domain.PasswordHistory{
UserID: userID,
@@ -199,7 +199,7 @@ func (s *UserService) applyNewPassword(ctx context.Context, user *domain.User, n
log.Printf("user_service: password history save panic recovered, user_id=%d err=%v", userID, r)
}
}()
bgCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
bgCtx, cancel := context.WithTimeout(context.Background(), defaultBETimeout)
defer cancel()
_ = s.passwordHistoryRepo.Create(bgCtx, &domain.PasswordHistory{
UserID: userID,

View File

@@ -295,7 +295,7 @@ func (s *WebhookService) recordDelivery(task *deliveryTask, statusCode int, body
delivery.DeliveredAt = &now
}
// 使用带超时的独立 context防止 DB 写入无限等待
writeCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
writeCtx, cancel := context.WithTimeout(context.Background(), defaultBETimeout)
defer cancel()
_ = s.repo.CreateDelivery(writeCtx, delivery)
}