From 72a009e1aef65dce9f9278bea3baae32b429bea0 Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Fri, 24 Oct 2025 17:09:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20session=20=E6=B7=BB=E5=8A=A0=E8=8A=82?= =?UTF-8?q?=E6=B5=81=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/contexts/AuthContext.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/contexts/AuthContext.js b/src/contexts/AuthContext.js index c4deae2b..d7bbdb8a 100755 --- a/src/contexts/AuthContext.js +++ b/src/contexts/AuthContext.js @@ -29,10 +29,32 @@ export const AuthProvider = ({ children }) => { // ⚡ 使用 ref 保存最新的 isAuthenticated 值,避免事件监听器重复注册 const isAuthenticatedRef = React.useRef(isAuthenticated); + // ⚡ 请求节流:记录上次请求时间,防止短时间内重复请求 + const lastCheckTimeRef = React.useRef(0); + const MIN_CHECK_INTERVAL = 1000; // 最少间隔1秒 + // 检查Session状态 const checkSession = async () => { + // 节流检查 + const now = Date.now(); + const timeSinceLastCheck = now - lastCheckTimeRef.current; + + if (timeSinceLastCheck < MIN_CHECK_INTERVAL) { + logger.warn('AuthContext', 'checkSession 请求被节流(防止频繁请求)', { + timeSinceLastCheck: `${timeSinceLastCheck}ms`, + minInterval: `${MIN_CHECK_INTERVAL}ms`, + reason: '距离上次请求间隔太短' + }); + return; + } + + lastCheckTimeRef.current = now; + try { - logger.debug('AuthContext', '检查Session状态'); + logger.debug('AuthContext', '开始检查Session状态', { + timestamp: new Date().toISOString(), + timeSinceLastCheck: timeSinceLastCheck > 0 ? `${timeSinceLastCheck}ms` : '首次请求' + }); // 创建超时控制器 const controller = new AbortController();