mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50:52 +08:00
Merge pull request #449 from sususu98/fix/gemini-cli-429-retry-delay-parsing
fix(gemini-cli): enhance 429 retry delay parsing
This commit is contained in:
@@ -11,6 +11,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -784,20 +786,45 @@ func parseRetryDelay(errorBody []byte) (*time.Duration, error) {
|
|||||||
// Try to parse the retryDelay from the error response
|
// Try to parse the retryDelay from the error response
|
||||||
// Format: error.details[].retryDelay where @type == "type.googleapis.com/google.rpc.RetryInfo"
|
// Format: error.details[].retryDelay where @type == "type.googleapis.com/google.rpc.RetryInfo"
|
||||||
details := gjson.GetBytes(errorBody, "error.details")
|
details := gjson.GetBytes(errorBody, "error.details")
|
||||||
if !details.Exists() || !details.IsArray() {
|
if details.Exists() && details.IsArray() {
|
||||||
return nil, fmt.Errorf("no error.details found")
|
for _, detail := range details.Array() {
|
||||||
|
typeVal := detail.Get("@type").String()
|
||||||
|
if typeVal == "type.googleapis.com/google.rpc.RetryInfo" {
|
||||||
|
retryDelay := detail.Get("retryDelay").String()
|
||||||
|
if retryDelay != "" {
|
||||||
|
// Parse duration string like "0.847655010s"
|
||||||
|
duration, err := time.ParseDuration(retryDelay)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse duration")
|
||||||
|
}
|
||||||
|
return &duration, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: try ErrorInfo.metadata.quotaResetDelay (e.g., "373.801628ms")
|
||||||
|
for _, detail := range details.Array() {
|
||||||
|
typeVal := detail.Get("@type").String()
|
||||||
|
if typeVal == "type.googleapis.com/google.rpc.ErrorInfo" {
|
||||||
|
quotaResetDelay := detail.Get("metadata.quotaResetDelay").String()
|
||||||
|
if quotaResetDelay != "" {
|
||||||
|
duration, err := time.ParseDuration(quotaResetDelay)
|
||||||
|
if err == nil {
|
||||||
|
return &duration, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, detail := range details.Array() {
|
// Fallback: parse from error.message "Your quota will reset after Xs."
|
||||||
typeVal := detail.Get("@type").String()
|
message := gjson.GetBytes(errorBody, "error.message").String()
|
||||||
if typeVal == "type.googleapis.com/google.rpc.RetryInfo" {
|
if message != "" {
|
||||||
retryDelay := detail.Get("retryDelay").String()
|
re := regexp.MustCompile(`after\s+(\d+)s\.?`)
|
||||||
if retryDelay != "" {
|
if matches := re.FindStringSubmatch(message); len(matches) > 1 {
|
||||||
// Parse duration string like "0.847655010s"
|
seconds, err := strconv.Atoi(matches[1])
|
||||||
duration, err := time.ParseDuration(retryDelay)
|
if err == nil {
|
||||||
if err != nil {
|
duration := time.Duration(seconds) * time.Second
|
||||||
return nil, fmt.Errorf("failed to parse duration")
|
|
||||||
}
|
|
||||||
return &duration, nil
|
return &duration, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user