Files
new-api/service/auth_cleanup.go
T
CaIon 14a9acd368 feat(custom): CNY native billing, daily check-in button, subscription plans page, usage docs and promo banner
Based on QuantumNous/new-api v1.0.0-rc.36 (shallow history squashed).

- CNY quota display mode: RMB-denominated top-up and crediting across Epay/Waffo channels
- New CheckinButton in both authenticated and public headers (Turnstile supported)
- New /subscribe page with public plan snapshot, Alipay/balance payment, auto group upgrade/reset
- New /docs 12-section beginner guide with anchor navigation
- New home PromoBanner linking to /docs; i18n synced (en/zh/zh-TW/fr/ja/ru/vi)
- README: document custom features, live demo https://ai.88531.cn
2026-09-23 16:28:52 +08:00

52 lines
1.5 KiB
Go

package service
import (
"fmt"
"time"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/model"
)
const authArtifactCleanupInterval = time.Hour
// StartAuthArtifactCleanup removes expired dashboard Sessions and old
// one-time authentication flows. Only the master instance performs cleanup.
func StartAuthArtifactCleanup() {
if !common.IsMasterNode {
return
}
go func() {
cleanupAuthArtifacts()
ticker := time.NewTicker(authArtifactCleanupInterval)
defer ticker.Stop()
for range ticker.C {
cleanupAuthArtifacts()
}
}()
}
func cleanupAuthArtifacts() {
now := time.Now()
count, err := model.CountUserSessionsCreatedSince(0, now.Add(-time.Hour).Unix())
if err != nil {
common.SysError("failed to count hourly user session issuance: " + err.Error())
} else if count > int64(common.UserSessionHourlyAlertThreshold) {
common.SysError(fmt.Sprintf(
"hourly user session issuance exceeded alert threshold: count=%d threshold=%d window_seconds=%d",
count,
common.UserSessionHourlyAlertThreshold,
int64(time.Hour/time.Second),
))
}
if err := model.DeleteExpiredUserSessions(now.Unix()); err != nil {
common.SysError("failed to delete expired user sessions: " + err.Error())
}
if err := model.DeleteOldRevokedUserSessions(now.Unix()); err != nil {
common.SysError("failed to delete old revoked user sessions: " + err.Error())
}
if err := model.DeleteExpiredAuthFlows(now); err != nil {
common.SysError("failed to delete expired authentication flows: " + err.Error())
}
}