AIStudio2API icon indicating copy to clipboard operation
AIStudio2API copied to clipboard

[Bug] messageRetryDelay fails to parse "Please retry in ..." when followed by a newline, causing daily quota to miscalculate reset time

Open photobrand opened this issue 1 week ago • 0 comments

发生了什么 / What happened

Summary

In internal/aistudio/client.go, messageRetryDelay() attempts to extract the retry duration using strings.Cut(rest, " ") followed by strings.TrimRight(value, "."). When Google returns an error where Please retry in <duration>. appears on its own line or at the end of a block with a trailing newline (\n or \r\n), messageRetryDelay fails to parse the duration, returning 0s.

Observed Upstream Error

* Quota exceeded for metric: generativelanguage.googleapis.com/generate_content_tokens_per_model_per_user, limit: 10000000, model: gemini-4-hydrogen, description: Generate content tokens per user per day limit exceeded. See https://aistudio.google.com/docs/rate-limits.
Please retry in 20h16m22.721618714s.
Trace & Consequences
strings.Cut(rest, " ") looks for a space after "retry in ". Because 20h16m22.721618714s. is followed by \r\n (no space), value becomes "20h16m22.721618714s.\r\n".
strings.TrimRight(value, ".") does nothing because the trailing characters are \r and \n, not ..
time.ParseDuration("20h16m22.721618714s.\r\n") returns 0s and an error (invalid duration).
In internal/aistudio/quota.go (QuotaCooldownForError), rpcError.RetryDelay remains 0.
The code falls back to dailyQuotaEvidence and sets until = nextQuotaDay(now) (Pacific midnight).
If the error occurs during the day in Pacific time (e.g. 10:00 AM PST), Pacific midnight is ~14 hours away, whereas Google explicitly specified 20h16m22s. The account is prematurely marked ready 6 hours too early, immediately hitting 429 again when scheduled.
Minimal Go Repro
```go


func messageRetryDelay(message string) time.Duration {
    _, rest, _ := strings.Cut(strings.ToLower(message), "retry in ")
    value, _, _ := strings.Cut(rest, " ")
    delay, _ := time.ParseDuration(strings.TrimRight(value, "."))
    return delay
}

// messageRetryDelay("Please retry in 20h16m22.721618714s.") -> 20h16m22.721618714s
// messageRetryDelay("Please retry in 20h16m22.721618714s.\n") -> 0s (FAILS)
// messageRetryDelay("Please retry in 20h16m22.721618714s.\r\n") -> 0s (FAILS)

Suggested Fix

Use strings.Fields and trim whitespace, punctuation, and newlines:



func messageRetryDelay(message string) time.Duration {
    _, rest, found := strings.Cut(strings.ToLower(message), "retry in ")
    if !found {
        return 0
    }
    fields := strings.Fields(rest)
    if len(fields) == 0 {
        return 0
    }
    value := strings.TrimRight(fields[0], ".,\r\n ")
    delay, _ := time.ParseDuration(value)
    return delay
}

版本 / Version

v0.3.13

photobrand avatar Nov 21 '26 19:11 photobrand