fix: refine grok quota pause behavior

This commit is contained in:
Heatherm Huang
2026-06-26 10:37:37 +08:00
parent 0d28642181
commit 939905b8c7
3 changed files with 29 additions and 2 deletions
@@ -155,6 +155,14 @@ func TestShouldAutoPauseGrokAccountByQuota(t *testing.T) {
},
want: true,
},
{
name: "retry after expired",
snapshot: xai.QuotaSnapshot{
RetryAfterSeconds: &retryAfter,
UpdatedAt: time.Now().Add(-time.Duration(retryAfter+1) * time.Second).UTC().Format(time.RFC3339),
},
want: false,
},
{
name: "stale snapshot ignored",
snapshot: xai.QuotaSnapshot{
@@ -1418,7 +1418,7 @@ func shouldAutoPauseGrokAccountByQuota(account *Account) (bool, openAIQuotaAutoP
if grokQuotaSnapshotStaleForPause(snapshot, now) {
return false, openAIQuotaAutoPauseDecision{}
}
if snapshot.RetryAfterSeconds != nil && *snapshot.RetryAfterSeconds > 0 {
if grokQuotaRetryAfterActive(snapshot, now) {
return true, openAIQuotaAutoPauseDecision{window: "retry_after", threshold: 1, utilization: 1}
}
if paused, decision := shouldAutoPauseGrokQuotaWindow("requests", snapshot.Requests, now); paused {
@@ -1430,6 +1430,21 @@ func shouldAutoPauseGrokAccountByQuota(account *Account) (bool, openAIQuotaAutoP
return false, openAIQuotaAutoPauseDecision{}
}
func grokQuotaRetryAfterActive(snapshot *xai.QuotaSnapshot, now time.Time) bool {
if snapshot == nil || snapshot.RetryAfterSeconds == nil || *snapshot.RetryAfterSeconds <= 0 {
return false
}
if strings.TrimSpace(snapshot.UpdatedAt) == "" {
return true
}
updatedAt, err := parseTime(snapshot.UpdatedAt)
if err != nil {
return true
}
retryAfterUntil := updatedAt.Add(time.Duration(*snapshot.RetryAfterSeconds) * time.Second)
return now.Before(retryAfterUntil)
}
func shouldAutoPauseGrokQuotaWindow(name string, window *xai.QuotaWindow, now time.Time) (bool, openAIQuotaAutoPauseDecision) {
if window == nil || window.Limit == nil || window.Remaining == nil || *window.Limit <= 0 {
return false, openAIQuotaAutoPauseDecision{}
@@ -102,7 +102,11 @@
</div>
<!-- No data yet -->
<div v-else class="text-xs text-gray-400">-</div>
<div v-else class="space-y-1">
<div class="text-xs text-gray-400">-</div>
<!-- Always allow on-demand upstream quota probe, even before passive headers exist. -->
<GrokQuotaProbeCell :account="account" />
</div>
</template>
<!-- OpenAI OAuth accounts: single source from /usage API -->