Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
a70d37a6
Commit
a70d37a6
authored
Feb 09, 2026
by
erio
Browse files
fix: Gemini error policy check should precede retry logic
parent
6892e84a
Changes
1
Hide whitespace changes
Inline
Side-by-side
backend/internal/service/gemini_messages_compat_service.go
View file @
a70d37a6
...
@@ -770,6 +770,14 @@ func (s *GeminiMessagesCompatService) Forward(ctx context.Context, c *gin.Contex
...
@@ -770,6 +770,14 @@ func (s *GeminiMessagesCompatService) Forward(ctx context.Context, c *gin.Contex
break
break
}
}
// 错误策略优先:匹配则跳过重试直接处理。
if
matched
,
rebuilt
:=
s
.
checkErrorPolicyInLoop
(
ctx
,
account
,
resp
);
matched
{
resp
=
rebuilt
break
}
else
{
resp
=
rebuilt
}
if
resp
.
StatusCode
>=
400
&&
s
.
shouldRetryGeminiUpstreamError
(
account
,
resp
.
StatusCode
)
{
if
resp
.
StatusCode
>=
400
&&
s
.
shouldRetryGeminiUpstreamError
(
account
,
resp
.
StatusCode
)
{
respBody
,
_
:=
io
.
ReadAll
(
io
.
LimitReader
(
resp
.
Body
,
2
<<
20
))
respBody
,
_
:=
io
.
ReadAll
(
io
.
LimitReader
(
resp
.
Body
,
2
<<
20
))
_
=
resp
.
Body
.
Close
()
_
=
resp
.
Body
.
Close
()
...
@@ -1176,6 +1184,14 @@ func (s *GeminiMessagesCompatService) ForwardNative(ctx context.Context, c *gin.
...
@@ -1176,6 +1184,14 @@ func (s *GeminiMessagesCompatService) ForwardNative(ctx context.Context, c *gin.
return
nil
,
s
.
writeGoogleError
(
c
,
http
.
StatusBadGateway
,
"Upstream request failed after retries: "
+
safeErr
)
return
nil
,
s
.
writeGoogleError
(
c
,
http
.
StatusBadGateway
,
"Upstream request failed after retries: "
+
safeErr
)
}
}
// 错误策略优先:匹配则跳过重试直接处理。
if
matched
,
rebuilt
:=
s
.
checkErrorPolicyInLoop
(
ctx
,
account
,
resp
);
matched
{
resp
=
rebuilt
break
}
else
{
resp
=
rebuilt
}
if
resp
.
StatusCode
>=
400
&&
s
.
shouldRetryGeminiUpstreamError
(
account
,
resp
.
StatusCode
)
{
if
resp
.
StatusCode
>=
400
&&
s
.
shouldRetryGeminiUpstreamError
(
account
,
resp
.
StatusCode
)
{
respBody
,
_
:=
io
.
ReadAll
(
io
.
LimitReader
(
resp
.
Body
,
2
<<
20
))
respBody
,
_
:=
io
.
ReadAll
(
io
.
LimitReader
(
resp
.
Body
,
2
<<
20
))
_
=
resp
.
Body
.
Close
()
_
=
resp
.
Body
.
Close
()
...
@@ -1425,6 +1441,26 @@ func (s *GeminiMessagesCompatService) ForwardNative(ctx context.Context, c *gin.
...
@@ -1425,6 +1441,26 @@ func (s *GeminiMessagesCompatService) ForwardNative(ctx context.Context, c *gin.
},
nil
},
nil
}
}
// checkErrorPolicyInLoop 在重试循环内预检查错误策略。
// 返回 true 表示策略已匹配(调用者应 break),resp 已重建可直接使用。
// 返回 false 表示 ErrorPolicyNone,resp 已重建,调用者继续走重试逻辑。
func
(
s
*
GeminiMessagesCompatService
)
checkErrorPolicyInLoop
(
ctx
context
.
Context
,
account
*
Account
,
resp
*
http
.
Response
,
)
(
matched
bool
,
rebuilt
*
http
.
Response
)
{
if
resp
.
StatusCode
<
400
||
s
.
rateLimitService
==
nil
{
return
false
,
resp
}
body
,
_
:=
io
.
ReadAll
(
io
.
LimitReader
(
resp
.
Body
,
2
<<
20
))
_
=
resp
.
Body
.
Close
()
rebuilt
=
&
http
.
Response
{
StatusCode
:
resp
.
StatusCode
,
Header
:
resp
.
Header
.
Clone
(),
Body
:
io
.
NopCloser
(
bytes
.
NewReader
(
body
)),
}
policy
:=
s
.
rateLimitService
.
CheckErrorPolicy
(
ctx
,
account
,
resp
.
StatusCode
,
body
)
return
policy
!=
ErrorPolicyNone
,
rebuilt
}
func
(
s
*
GeminiMessagesCompatService
)
shouldRetryGeminiUpstreamError
(
account
*
Account
,
statusCode
int
)
bool
{
func
(
s
*
GeminiMessagesCompatService
)
shouldRetryGeminiUpstreamError
(
account
*
Account
,
statusCode
int
)
bool
{
switch
statusCode
{
switch
statusCode
{
case
429
,
500
,
502
,
503
,
504
,
529
:
case
429
,
500
,
502
,
503
,
504
,
529
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment