package com.ttstd.signaling.service;

import com.ttstd.signaling.security.RateLimiter;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 安全相关内存结构的周期性清理，防止长期运行造成内存增长。
 */
@Component
public class SecurityMaintenanceTask {

    private final AccountService accountService;
    private final DeviceIdentityService deviceIdentityService;
    private final RateLimiter rateLimiter;

    public SecurityMaintenanceTask(AccountService accountService,
                                   DeviceIdentityService deviceIdentityService,
                                   RateLimiter rateLimiter) {
        this.accountService = accountService;
        this.deviceIdentityService = deviceIdentityService;
        this.rateLimiter = rateLimiter;
    }

    /** 每 5 分钟清理一次过期会话与限流窗口（nonce 由 Redis 自动过期）。 */
    @Scheduled(fixedDelay = 300_000L, initialDelay = 300_000L)
    public void cleanup() {
        accountService.evictExpiredSessions();
        rateLimiter.evictExpired();
    }
}
