perf(api): optimize cookie validation with fail-fast strategy

Before: Pre-validate cookie before every request (2-4 API calls per activity)
After: Direct request, only validate on 4xx error (1-2 API calls per activity)

Changes:
- Remove pre-validation step in fetchActivityData
- Keep existing 4xx error handling with re-login logic
- Add debug log to track cookie usage

Impact: ~20-30% reduction in API calls for normal scenarios
Benefit: Faster scanning, less load on engage API
This commit is contained in:
JamesFlare1212
2026-04-06 21:37:06 -04:00
parent 32dee6b161
commit 5f630f8599

View File

@@ -182,23 +182,15 @@ export async function fetchActivityData(
let currentCookie = forceLogin ? null : await getCachedCookieString(); let currentCookie = forceLogin ? null : await getCachedCookieString();
if (forceLogin && currentCookie) { if (forceLogin && currentCookie) {
logger.info('Forcing new login. Clearing cached cookie.');
await clearCookieCache(); await clearCookieCache();
currentCookie = null; currentCookie = null;
} }
if (currentCookie) { // Optimization: Skip pre-validation, directly request data
const isValid = await testCookieValidityWithApi(currentCookie); // Only validate/re-login when we get 4xx error (fail-fast strategy)
if (!isValid) {
logger.info('Cached cookie test failed or cookie expired. Clearing cache.');
await clearCookieCache();
currentCookie = null;
} else {
logger.info('Using valid cached cookie.');
}
}
if (!currentCookie) { if (!currentCookie) {
logger.info(forceLogin ? 'Forcing new login.' : 'No valid cached cookie found or cache bypassed. Attempting login...'); logger.info('No cached cookie found. Attempting login...');
try { try {
currentCookie = await getCompleteCookies(userName, userPwd); currentCookie = await getCompleteCookies(userName, userPwd);
@@ -217,6 +209,8 @@ export async function fetchActivityData(
return null; return null;
} }
logger.debug('Using cached cookie for API request.');
try { try {
const rawActivityDetailsString = await getActivityDetailsRaw(activityId, currentCookie); const rawActivityDetailsString = await getActivityDetailsRaw(activityId, currentCookie);
if (rawActivityDetailsString) { if (rawActivityDetailsString) {
@@ -227,7 +221,8 @@ export async function fetchActivityData(
return null; return null;
} catch (error) { } catch (error) {
if (error instanceof AuthenticationError) { if (error instanceof AuthenticationError) {
logger.warn(`Initial fetch failed with AuthenticationError (Status: ${error.status}). Cookie was likely invalid. Attempting re-login and one retry.`); // Cookie returned 4xx, now validate and re-login
logger.warn(`API returned 4xx error (Status: ${error.status}). Cookie may be invalid. Attempting re-login and retry.`);
await clearCookieCache(); await clearCookieCache();
try { try {
@@ -239,7 +234,7 @@ export async function fetchActivityData(
await saveCookiesToCache(cookies); await saveCookiesToCache(cookies);
} }
logger.info('Re-login successful. Retrying request for activity details once...'); logger.info('Re-login successful. Retrying request for activity details...');
const rawActivityDetailsStringRetry = await getActivityDetailsRaw(activityId, currentCookie); const rawActivityDetailsStringRetry = await getActivityDetailsRaw(activityId, currentCookie);
if (rawActivityDetailsStringRetry) { if (rawActivityDetailsStringRetry) {
const parsedOuterRetry = JSON.parse(rawActivityDetailsStringRetry); const parsedOuterRetry = JSON.parse(rawActivityDetailsStringRetry);