-
Notifications
You must be signed in to change notification settings - Fork 11.3k
feat: customizable automatic retry status codes #2663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,3 +50,30 @@ func TestShouldDisableByStatusCode(t *testing.T) { | |
| require.True(t, ShouldDisableByStatusCode(500)) | ||
| require.False(t, ShouldDisableByStatusCode(200)) | ||
| } | ||
|
|
||
| func TestShouldRetryByStatusCode(t *testing.T) { | ||
| orig := AutomaticRetryStatusCodeRanges | ||
| t.Cleanup(func() { AutomaticRetryStatusCodeRanges = orig }) | ||
|
|
||
| AutomaticRetryStatusCodeRanges = []StatusCodeRange{ | ||
| {Start: 429, End: 429}, | ||
| {Start: 500, End: 599}, | ||
| } | ||
|
|
||
| require.True(t, ShouldRetryByStatusCode(429)) | ||
| require.True(t, ShouldRetryByStatusCode(500)) | ||
| require.False(t, ShouldRetryByStatusCode(400)) | ||
| require.False(t, ShouldRetryByStatusCode(200)) | ||
| } | ||
|
|
||
| func TestShouldRetryByStatusCode_DefaultMatchesLegacyBehavior(t *testing.T) { | ||
| require.False(t, ShouldRetryByStatusCode(200)) | ||
| require.False(t, ShouldRetryByStatusCode(400)) | ||
| require.True(t, ShouldRetryByStatusCode(401)) | ||
| require.False(t, ShouldRetryByStatusCode(408)) | ||
| require.True(t, ShouldRetryByStatusCode(429)) | ||
| require.True(t, ShouldRetryByStatusCode(500)) | ||
| require.False(t, ShouldRetryByStatusCode(504)) | ||
| require.False(t, ShouldRetryByStatusCode(524)) | ||
| require.True(t, ShouldRetryByStatusCode(599)) | ||
| } | ||
|
Comment on lines
+54
to
+79
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Global mutation in tests can become flaky if package tests are (now or later) run in parallel. Consider either:
🤖 Prompt for AI Agents |
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,71 @@ | ||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||
| Copyright (C) 2025 QuantumNous | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| This program is free software: you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||
| it under the terms of the GNU Affero General Public License as | ||||||||||||||||||||||||||||||||||
| published by the Free Software Foundation, either version 3 of the | ||||||||||||||||||||||||||||||||||
| License, or (at your option) any later version. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||||||||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||||||||
| GNU Affero General Public License for more details. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| You should have received a copy of the GNU Affero General Public License | ||||||||||||||||||||||||||||||||||
| along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| For commercial licensing, please contact support@quantumnous.com | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import React from 'react'; | ||||||||||||||||||||||||||||||||||
| import { Form, Tag, Typography } from '@douyinfe/semi-ui'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export default function HttpStatusCodeRulesInput(props) { | ||||||||||||||||||||||||||||||||||
| const { Text } = Typography; | ||||||||||||||||||||||||||||||||||
| const { | ||||||||||||||||||||||||||||||||||
| label, | ||||||||||||||||||||||||||||||||||
| field, | ||||||||||||||||||||||||||||||||||
| placeholder, | ||||||||||||||||||||||||||||||||||
| extraText, | ||||||||||||||||||||||||||||||||||
| onChange, | ||||||||||||||||||||||||||||||||||
| parsed, | ||||||||||||||||||||||||||||||||||
| invalidText, | ||||||||||||||||||||||||||||||||||
| } = props; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||
| <> | ||||||||||||||||||||||||||||||||||
| <Form.Input | ||||||||||||||||||||||||||||||||||
| label={label} | ||||||||||||||||||||||||||||||||||
| placeholder={placeholder} | ||||||||||||||||||||||||||||||||||
| extraText={extraText} | ||||||||||||||||||||||||||||||||||
| field={field} | ||||||||||||||||||||||||||||||||||
| onChange={onChange} | ||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||
| {parsed?.ok && parsed.tokens?.length > 0 && ( | ||||||||||||||||||||||||||||||||||
| <div | ||||||||||||||||||||||||||||||||||
| style={{ | ||||||||||||||||||||||||||||||||||
| display: 'flex', | ||||||||||||||||||||||||||||||||||
| flexWrap: 'wrap', | ||||||||||||||||||||||||||||||||||
| gap: 8, | ||||||||||||||||||||||||||||||||||
| marginTop: 8, | ||||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||
| {parsed.tokens.map((token) => ( | ||||||||||||||||||||||||||||||||||
| <Tag key={token} size='small'> | ||||||||||||||||||||||||||||||||||
| {token} | ||||||||||||||||||||||||||||||||||
| </Tag> | ||||||||||||||||||||||||||||||||||
| ))} | ||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||
| {!parsed?.ok && ( | ||||||||||||||||||||||||||||||||||
| <Text type='danger' style={{ display: 'block', marginTop: 8 }}> | ||||||||||||||||||||||||||||||||||
| {invalidText} | ||||||||||||||||||||||||||||||||||
| {parsed?.invalidTokens && parsed.invalidTokens.length > 0 | ||||||||||||||||||||||||||||||||||
| ? `: ${parsed.invalidTokens.join(', ')}` | ||||||||||||||||||||||||||||||||||
| : ''} | ||||||||||||||||||||||||||||||||||
| </Text> | ||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+60
to
+67
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard against undefined When 🐛 Proposed fix- {!parsed?.ok && (
+ {parsed && !parsed.ok && (
<Text type='danger' style={{ display: 'block', marginTop: 8 }}>
{invalidText}
{parsed?.invalidTokens && parsed.invalidTokens.length > 0
? `: ${parsed.invalidTokens.join(', ')}`
: ''}
</Text>
)}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| </> | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
# First, locate and read the file in question fd -type f status_code_ranges.goRepository: QuantumNous/new-api
Length of output: 233
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 4279
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 2793
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 1870
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 2242
🏁 Script executed:
# Read the complete status_code_ranges.go file cat setting/operation_setting/status_code_ranges.goRepository: QuantumNous/new-api
Length of output: 4573
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 45
Major: data race and sortedness invariant in global mutable status code ranges.
Data race:
AutomaticRetryStatusCodeRangesandAutomaticDisableStatusCodeRangesare unprotected exported globals. They are read in request paths (e.g.,service/channel.gocallsShouldDisableByStatusCode) and written viaSetAutomaticRetryStatusCodes/SetAutomaticDisableStatusCodes. Concurrent reads/writes are a Go data race. Other operation settings modules (e.g.,setting/ratio_setting/model_ratio.go,setting/user_usable_group.go) consistently protect mutable globals withsync.RWMutex.Sortedness invariant:
shouldMatchStatusCodeRangesrelies on sorted ranges—it returnsfalsewhencode < r.Start, skipping later ranges.ParseHTTPStatusCodeRangesenforces sorting, but the globals can be assigned directly (as tests do), bypassing this guarantee and silently breaking the matching logic.Fix: Add
sync.RWMutexaround both globals (consistent with other settings), and makeshouldMatchStatusCodeRangesorder-independent:Proposed changes
Also applies to: 35-52 (update functions), 74-84 (shouldMatchStatusCodeRanges)
🤖 Prompt for AI Agents