mirror of
https://github.com/dtzp555-max/ocp.git
synced 2026-07-19 09:44:07 +00:00
fix: fallback to stale cache on usage API 429 + extend cache to 15min
When the /api/oauth/usage endpoint returns 429 (rate limit), fall back to the most recent cached data instead of returning an error. Also extended cache TTL from 5min to 15min to reduce API calls. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+9
-2
@@ -664,7 +664,7 @@ function completionResponse(res, id, model, content) {
|
|||||||
// Caches the result for 5 minutes to avoid excessive API calls.
|
// Caches the result for 5 minutes to avoid excessive API calls.
|
||||||
|
|
||||||
let usageCache = { data: null, fetchedAt: 0 };
|
let usageCache = { data: null, fetchedAt: 0 };
|
||||||
const USAGE_CACHE_TTL = 300000; // 5 min
|
const USAGE_CACHE_TTL = 900000; // 15 min
|
||||||
const OAUTH_CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e";
|
const OAUTH_CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e";
|
||||||
const OAUTH_TOKEN_URL = "https://platform.claude.com/v1/oauth/token";
|
const OAUTH_TOKEN_URL = "https://platform.claude.com/v1/oauth/token";
|
||||||
const OAUTH_BETA_HEADER = "oauth-2025-04-20";
|
const OAUTH_BETA_HEADER = "oauth-2025-04-20";
|
||||||
@@ -853,6 +853,9 @@ async function handleUsage(_req, res) {
|
|||||||
data = await fetchUsageFromApi();
|
data = await fetchUsageFromApi();
|
||||||
if (!data.error) {
|
if (!data.error) {
|
||||||
usageCache = { data, fetchedAt: now };
|
usageCache = { data, fetchedAt: now };
|
||||||
|
} else if (usageCache.data) {
|
||||||
|
// Fallback to stale cache on error (e.g. 429 rate limit)
|
||||||
|
data = { ...usageCache.data, _stale: true, _fetchError: data.error };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Always attach live model stats and proxy stats (not cached)
|
// Always attach live model stats and proxy stats (not cached)
|
||||||
@@ -922,7 +925,11 @@ async function handleStatus(_req, res) {
|
|||||||
usage = usageCache.data;
|
usage = usageCache.data;
|
||||||
} else {
|
} else {
|
||||||
usage = await fetchUsageFromApi();
|
usage = await fetchUsageFromApi();
|
||||||
if (!usage.error) usageCache = { data: usage, fetchedAt: now };
|
if (!usage.error) {
|
||||||
|
usageCache = { data: usage, fetchedAt: now };
|
||||||
|
} else if (usageCache.data) {
|
||||||
|
usage = { ...usageCache.data, _stale: true };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auth
|
// Auth
|
||||||
|
|||||||
Reference in New Issue
Block a user