fix: clarify effective limit debug output

This commit is contained in:
kim
2026-04-01 14:10:59 +08:00
parent 730e444a76
commit f36c560bb0
+66 -14
View File
@@ -301,11 +301,9 @@ def get_usage_key(clean_model, api_base=None):
return f"daily_usage:{date_str}:{clean_model}{suffix}"
def get_static_limit(clean_model, api_base):
"""Check static configuration in HARD_DAILY_LIMITS."""
current_hash = generate_api_base_hash(api_base)
# 1. Exact Match with Hash
def get_static_limit_for_hash(clean_model, current_hash=None):
"""Check static configuration in HARD_DAILY_LIMITS using an optional endpoint hash."""
if current_hash:
specific_key = f"{clean_model}:{current_hash}"
if specific_key in HARD_DAILY_LIMITS:
return HARD_DAILY_LIMITS[specific_key]
@@ -328,6 +326,59 @@ def get_static_limit(clean_model, api_base):
return None
def get_static_limit(clean_model, api_base):
"""Check static configuration in HARD_DAILY_LIMITS."""
current_hash = generate_api_base_hash(api_base) if api_base else None
return get_static_limit_for_hash(clean_model, current_hash)
def get_monitored_static_limit(clean_model, model_hashes=None):
"""
Resolve the static limit for monitored-model debug output.
If multiple endpoint hashes exist, show the strictest applicable static cap.
"""
specific_limits = []
for current_hash in model_hashes or []:
limit_val = get_static_limit_for_hash(clean_model, current_hash)
if limit_val is not None:
specific_limits.append(float(limit_val))
if specific_limits:
return min(specific_limits)
return get_static_limit_for_hash(clean_model, None)
def calculate_effective_limit(static_limit, dynamic_limit):
"""Return the effective cap after combining static and dynamic limits."""
try:
if dynamic_limit is None:
return float(static_limit) if static_limit is not None else None
dynamic_limit = float(dynamic_limit)
if static_limit is None:
return dynamic_limit
return min(float(static_limit), dynamic_limit)
except (ValueError, TypeError):
return float(static_limit) if static_limit is not None else None
def format_limit_debug_fields(static_limit=None, stable_limit=None, effective_limit=None):
"""Render explicit limit labels for debug logs to avoid confusing stable and effective caps."""
fields = []
if static_limit is not None:
fields.append(f"StaticLimit={int(float(static_limit)):,}")
if stable_limit is not None:
fields.append(f"StableLimit={int(float(stable_limit)):,}")
if effective_limit is not None:
fields.append(f"EffectiveLimit={int(float(effective_limit)):,}")
return " | ".join(fields) if fields else "EffectiveLimit=unknown"
def get_effective_limit(clean_model, api_base, current_usage=None):
"""
Calculate effective limit using the Peak-Buffer strategy.
@@ -350,19 +401,14 @@ def get_effective_limit(clean_model, api_base, current_usage=None):
if stable_limit_val is not None:
try:
dynamic_limit = float(stable_limit_val)
# Apply Static Limit override if stricter
if static_limit:
effective_limit = min(float(static_limit), dynamic_limit)
else:
effective_limit = dynamic_limit
effective_limit = calculate_effective_limit(static_limit, dynamic_limit)
except (ValueError, TypeError):
effective_limit = static_limit
effective_limit = calculate_effective_limit(static_limit, None)
else:
# Fallback: If daemon hasn't calculated stable limit yet, use old logic?
# Or just use static. Safer to wait for daemon.
effective_limit = static_limit
effective_limit = calculate_effective_limit(static_limit, None)
# Legacy fallback (Optional: Enable if you want immediate loose limits before first sync)
# dynamic_remaining = redis_client.get(f"limit:dynamic:volcengine:remaining:{clean_model}")
@@ -1210,8 +1256,14 @@ def active_monitor_loop():
redis_client.setex(stable_limit_key, 86400, int(stable_limit))
if DEBUG_MODE:
static_limit = get_monitored_static_limit(
target, target_hashes
)
effective_limit = calculate_effective_limit(
static_limit, stable_limit
)
print(
f"DEBUG: {target} PeakPool={int(current_peak):,} | Limit={int(stable_limit):,} | Usage={int(current_usage):,}",
f"DEBUG: {target} PeakPool={int(current_peak):,} | Usage={int(current_usage):,} | {format_limit_debug_fields(static_limit=static_limit, stable_limit=stable_limit, effective_limit=effective_limit)}",
flush=True,
)