Parallelize the /v1/models and /api/oauth/usage calls using asyncio.gather in _do_fetch_provider_usage
This commit is contained in:
parent
d406beec56
commit
6bd6390b47
|
|
@ -1022,22 +1022,38 @@ async def _do_fetch_provider_usage(
|
||||||
checked_at=utcnow(), reachable=False,
|
checked_at=utcnow(), reachable=False,
|
||||||
error="No API key configured.",
|
error="No API key configured.",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
elif api_key and effective_session_key:
|
||||||
|
# Fire both calls concurrently — halves latency and eliminates the
|
||||||
|
# sequential back-to-back requests to api.anthropic.com that caused
|
||||||
|
# the subscription endpoint to 429 on every initial load.
|
||||||
|
subscription_attempted = True
|
||||||
|
result, sub_windows = await asyncio.gather(
|
||||||
|
_fetch_anthropic(api_key, base_url),
|
||||||
|
_fetch_anthropic_subscription(effective_session_key),
|
||||||
|
)
|
||||||
|
if result.reachable is not False:
|
||||||
|
if sub_windows:
|
||||||
|
result.subscription_windows = sub_windows
|
||||||
|
result.reachable = True
|
||||||
|
else:
|
||||||
|
result.error = result.error or "No subscription data returned."
|
||||||
|
|
||||||
elif api_key:
|
elif api_key:
|
||||||
result = await _fetch_anthropic(api_key, base_url)
|
result = await _fetch_anthropic(api_key, base_url)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
# OAuth token only — no API key configured
|
||||||
|
subscription_attempted = True
|
||||||
result = ProviderUsageLive(
|
result = ProviderUsageLive(
|
||||||
provider=provider, account_key=account_key,
|
provider=provider, account_key=account_key,
|
||||||
checked_at=utcnow(), reachable=True,
|
checked_at=utcnow(), reachable=True,
|
||||||
)
|
)
|
||||||
# Overlay subscription windows from OAuth token (auto or explicit)
|
|
||||||
if effective_session_key and result.reachable is not False:
|
|
||||||
subscription_attempted = True
|
|
||||||
sub_windows = await _fetch_anthropic_subscription(effective_session_key)
|
sub_windows = await _fetch_anthropic_subscription(effective_session_key)
|
||||||
if sub_windows:
|
if sub_windows:
|
||||||
result.subscription_windows = sub_windows
|
result.subscription_windows = sub_windows
|
||||||
result.reachable = True
|
|
||||||
else:
|
else:
|
||||||
result.error = result.error or "No subscription data returned."
|
result.error = "No subscription data returned."
|
||||||
|
|
||||||
elif provider in ("openai", "codex"):
|
elif provider in ("openai", "codex"):
|
||||||
if not api_key and not session_key:
|
if not api_key and not session_key:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue