-
Notifications
You must be signed in to change notification settings - Fork 11.1k
feat: status code auto-disable configuration #2647
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
Merged
Calcium-Ion
merged 3 commits into
QuantumNous:main
from
seefs001:feature/status-code-auto-disable
Jan 12, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| package operation_setting | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sort" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| type StatusCodeRange struct { | ||
| Start int | ||
| End int | ||
| } | ||
|
|
||
| var AutomaticDisableStatusCodeRanges = []StatusCodeRange{{Start: 401, End: 401}} | ||
|
|
||
| func AutomaticDisableStatusCodesToString() string { | ||
| if len(AutomaticDisableStatusCodeRanges) == 0 { | ||
| return "" | ||
| } | ||
| parts := make([]string, 0, len(AutomaticDisableStatusCodeRanges)) | ||
| for _, r := range AutomaticDisableStatusCodeRanges { | ||
| if r.Start == r.End { | ||
| parts = append(parts, strconv.Itoa(r.Start)) | ||
| continue | ||
| } | ||
| parts = append(parts, fmt.Sprintf("%d-%d", r.Start, r.End)) | ||
| } | ||
| return strings.Join(parts, ",") | ||
| } | ||
|
|
||
| func AutomaticDisableStatusCodesFromString(s string) error { | ||
| ranges, err := ParseHTTPStatusCodeRanges(s) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| AutomaticDisableStatusCodeRanges = ranges | ||
| return nil | ||
| } | ||
|
|
||
| func ShouldDisableByStatusCode(code int) bool { | ||
| if code < 100 || code > 599 { | ||
| return false | ||
| } | ||
| for _, r := range AutomaticDisableStatusCodeRanges { | ||
| if code < r.Start { | ||
| return false | ||
| } | ||
| if code <= r.End { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func ParseHTTPStatusCodeRanges(input string) ([]StatusCodeRange, error) { | ||
| input = strings.TrimSpace(input) | ||
| if input == "" { | ||
| return nil, nil | ||
| } | ||
|
|
||
| input = strings.NewReplacer(",", ",").Replace(input) | ||
| segments := strings.Split(input, ",") | ||
|
|
||
| var ranges []StatusCodeRange | ||
| var invalid []string | ||
|
|
||
| for _, seg := range segments { | ||
| seg = strings.TrimSpace(seg) | ||
| if seg == "" { | ||
| continue | ||
| } | ||
| r, err := parseHTTPStatusCodeToken(seg) | ||
| if err != nil { | ||
| invalid = append(invalid, seg) | ||
| continue | ||
| } | ||
| ranges = append(ranges, r) | ||
| } | ||
|
|
||
| if len(invalid) > 0 { | ||
| return nil, fmt.Errorf("invalid http status code rules: %s", strings.Join(invalid, ", ")) | ||
| } | ||
| if len(ranges) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| sort.Slice(ranges, func(i, j int) bool { | ||
| if ranges[i].Start == ranges[j].Start { | ||
| return ranges[i].End < ranges[j].End | ||
| } | ||
| return ranges[i].Start < ranges[j].Start | ||
| }) | ||
|
|
||
| merged := []StatusCodeRange{ranges[0]} | ||
| for _, r := range ranges[1:] { | ||
| last := &merged[len(merged)-1] | ||
| if r.Start <= last.End+1 { | ||
| if r.End > last.End { | ||
| last.End = r.End | ||
| } | ||
| continue | ||
| } | ||
| merged = append(merged, r) | ||
| } | ||
|
|
||
| return merged, nil | ||
| } | ||
|
|
||
| func parseHTTPStatusCodeToken(token string) (StatusCodeRange, error) { | ||
| token = strings.TrimSpace(token) | ||
| token = strings.ReplaceAll(token, " ", "") | ||
| if token == "" { | ||
| return StatusCodeRange{}, fmt.Errorf("empty token") | ||
| } | ||
|
|
||
| if strings.Contains(token, "-") { | ||
| parts := strings.Split(token, "-") | ||
| if len(parts) != 2 || parts[0] == "" || parts[1] == "" { | ||
| return StatusCodeRange{}, fmt.Errorf("invalid range token: %s", token) | ||
| } | ||
| start, err := strconv.Atoi(parts[0]) | ||
| if err != nil { | ||
| return StatusCodeRange{}, fmt.Errorf("invalid range start: %s", token) | ||
| } | ||
| end, err := strconv.Atoi(parts[1]) | ||
| if err != nil { | ||
| return StatusCodeRange{}, fmt.Errorf("invalid range end: %s", token) | ||
| } | ||
| if start > end { | ||
| return StatusCodeRange{}, fmt.Errorf("range start > end: %s", token) | ||
| } | ||
| if start < 100 || end > 599 { | ||
| return StatusCodeRange{}, fmt.Errorf("range out of bounds: %s", token) | ||
| } | ||
| return StatusCodeRange{Start: start, End: end}, nil | ||
| } | ||
|
|
||
| code, err := strconv.Atoi(token) | ||
| if err != nil { | ||
| return StatusCodeRange{}, fmt.Errorf("invalid status code: %s", token) | ||
| } | ||
| if code < 100 || code > 599 { | ||
| return StatusCodeRange{}, fmt.Errorf("status code out of bounds: %s", token) | ||
| } | ||
| return StatusCodeRange{Start: code, End: code}, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package operation_setting | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestParseHTTPStatusCodeRanges_CommaSeparated(t *testing.T) { | ||
| ranges, err := ParseHTTPStatusCodeRanges("401,403,500-599") | ||
| require.NoError(t, err) | ||
| require.Equal(t, []StatusCodeRange{ | ||
| {Start: 401, End: 401}, | ||
| {Start: 403, End: 403}, | ||
| {Start: 500, End: 599}, | ||
| }, ranges) | ||
| } | ||
|
|
||
| func TestParseHTTPStatusCodeRanges_MergeAndNormalize(t *testing.T) { | ||
| ranges, err := ParseHTTPStatusCodeRanges("500-505,504,401,403,402") | ||
| require.NoError(t, err) | ||
| require.Equal(t, []StatusCodeRange{ | ||
| {Start: 401, End: 403}, | ||
| {Start: 500, End: 505}, | ||
| }, ranges) | ||
| } | ||
|
|
||
| func TestParseHTTPStatusCodeRanges_Invalid(t *testing.T) { | ||
| _, err := ParseHTTPStatusCodeRanges("99,600,foo,500-400,500-") | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func TestParseHTTPStatusCodeRanges_NoComma_IsInvalid(t *testing.T) { | ||
| _, err := ParseHTTPStatusCodeRanges("401 403") | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func TestShouldDisableByStatusCode(t *testing.T) { | ||
| orig := AutomaticDisableStatusCodeRanges | ||
| t.Cleanup(func() { AutomaticDisableStatusCodeRanges = orig }) | ||
|
|
||
| AutomaticDisableStatusCodeRanges = []StatusCodeRange{ | ||
| {Start: 401, End: 403}, | ||
| {Start: 500, End: 599}, | ||
| } | ||
|
|
||
| require.True(t, ShouldDisableByStatusCode(401)) | ||
| require.True(t, ShouldDisableByStatusCode(403)) | ||
| require.False(t, ShouldDisableByStatusCode(404)) | ||
| require.True(t, ShouldDisableByStatusCode(500)) | ||
| require.False(t, ShouldDisableByStatusCode(200)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
Repository: QuantumNous/new-api
Length of output: 5806
Global variable lacks thread-safety protection.
AutomaticDisableStatusCodeRangesis a mutable global that can be read byShouldDisableByStatusCode(line 45) while being written byAutomaticDisableStatusCodesFromString(line 37). This pattern violates the synchronization pattern used throughout the codebase for similar globals (e.g.,userUsableGroupsMutex,ModelRequestRateLimitMutex,modelPriceMapMutex). Protect concurrent access withsync.RWMutex.🤖 Prompt for AI Agents