-
Notifications
You must be signed in to change notification settings - Fork 21
Rate-limit circuit breaker for GitHub MCP backend tool calls #3799
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f5a1f23
Initial plan
Copilot 99dea91
Add rate-limit circuit breaker for GitHub MCP backend tool calls
Copilot 4b0ba32
Address code review: fix transport error handling, header casing, imp…
Copilot fea1da4
review: enforce single HALF-OPEN probe, injectable clock, preserve ba…
lpcox 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestServerConfig_RateLimitFields(t *testing.T) { | ||
| t.Parallel() | ||
| toml := ` | ||
| [servers.github] | ||
| command = "docker" | ||
| args = ["run", "--rm", "-i", "ghcr.io/github/github-mcp-server:latest"] | ||
| rate_limit_threshold = 5 | ||
| rate_limit_cooldown = 120 | ||
| ` | ||
| path := writeTempTOML(t, toml) | ||
| cfg, err := LoadFromFile(path) | ||
| require.NoError(t, err) | ||
| srv := cfg.Servers["github"] | ||
| assert.Equal(t, 5, srv.RateLimitThreshold) | ||
| assert.Equal(t, 120, srv.RateLimitCooldown) | ||
| } | ||
|
|
||
| func TestServerConfig_RateLimitFieldsDefaultToZero(t *testing.T) { | ||
| t.Parallel() | ||
| toml := validDockerServerTOML | ||
| path := writeTempTOML(t, toml) | ||
| cfg, err := LoadFromFile(path) | ||
| require.NoError(t, err) | ||
| srv := cfg.Servers["github"] | ||
| assert.Equal(t, 0, srv.RateLimitThreshold) | ||
| assert.Equal(t, 0, srv.RateLimitCooldown) | ||
| } |
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,139 @@ | ||
| package proxy | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strconv" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // TestInjectRetryAfterIfRateLimited verifies Retry-After injection and logging for | ||
| // rate-limited upstream responses. | ||
| func TestInjectRetryAfterIfRateLimited(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("HTTP 429 injects Retry-After", func(t *testing.T) { | ||
| t.Parallel() | ||
| w := httptest.NewRecorder() | ||
| future := time.Now().Add(30 * time.Second) | ||
| resp := &http.Response{ | ||
| StatusCode: http.StatusTooManyRequests, | ||
| Header: http.Header{ | ||
| "X-Ratelimit-Reset": []string{strconv.FormatInt(future.Unix(), 10)}, | ||
| }, | ||
| } | ||
| injectRetryAfterIfRateLimited(w, resp) | ||
| retryAfter := w.Header().Get("Retry-After") | ||
| assert.NotEmpty(t, retryAfter, "Retry-After should be set on 429") | ||
| secs, err := strconv.Atoi(retryAfter) | ||
| assert.NoError(t, err) | ||
| assert.Greater(t, secs, 0, "Retry-After should be positive") | ||
| }) | ||
|
|
||
| t.Run("X-Ratelimit-Remaining 0 injects Retry-After", func(t *testing.T) { | ||
| t.Parallel() | ||
| w := httptest.NewRecorder() | ||
| future := time.Now().Add(60 * time.Second) | ||
| resp := &http.Response{ | ||
| StatusCode: http.StatusOK, | ||
| Header: http.Header{ | ||
| "X-Ratelimit-Remaining": []string{"0"}, | ||
| "X-Ratelimit-Reset": []string{strconv.FormatInt(future.Unix(), 10)}, | ||
| }, | ||
| } | ||
| injectRetryAfterIfRateLimited(w, resp) | ||
| assert.NotEmpty(t, w.Header().Get("Retry-After"), "Retry-After should be set when remaining=0") | ||
| }) | ||
|
|
||
| t.Run("non-zero remaining does not inject Retry-After", func(t *testing.T) { | ||
| t.Parallel() | ||
| w := httptest.NewRecorder() | ||
| resp := &http.Response{ | ||
| StatusCode: http.StatusOK, | ||
| Header: http.Header{ | ||
| "X-Ratelimit-Remaining": []string{"100"}, | ||
| }, | ||
| } | ||
| injectRetryAfterIfRateLimited(w, resp) | ||
| assert.Empty(t, w.Header().Get("Retry-After")) | ||
| }) | ||
|
|
||
| t.Run("200 with no rate-limit headers does not inject Retry-After", func(t *testing.T) { | ||
| t.Parallel() | ||
| w := httptest.NewRecorder() | ||
| resp := &http.Response{ | ||
| StatusCode: http.StatusOK, | ||
| Header: make(http.Header), | ||
| } | ||
| injectRetryAfterIfRateLimited(w, resp) | ||
| assert.Empty(t, w.Header().Get("Retry-After")) | ||
| }) | ||
|
|
||
| t.Run("429 without reset header uses default delay", func(t *testing.T) { | ||
| t.Parallel() | ||
| w := httptest.NewRecorder() | ||
| resp := &http.Response{ | ||
| StatusCode: http.StatusTooManyRequests, | ||
| Header: make(http.Header), | ||
| } | ||
| injectRetryAfterIfRateLimited(w, resp) | ||
| retryAfter := w.Header().Get("Retry-After") | ||
| assert.Equal(t, "60", retryAfter, "default delay should be 60 seconds") | ||
| }) | ||
| } | ||
|
|
||
| // TestParseRateLimitReset verifies the Unix-timestamp header parser. | ||
| func TestParseRateLimitReset(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("empty string returns zero", func(t *testing.T) { | ||
| t.Parallel() | ||
| assert.True(t, parseRateLimitReset("").IsZero()) | ||
| }) | ||
|
|
||
| t.Run("invalid string returns zero", func(t *testing.T) { | ||
| t.Parallel() | ||
| assert.True(t, parseRateLimitReset("not-a-number").IsZero()) | ||
| }) | ||
|
|
||
| t.Run("valid unix timestamp parses correctly", func(t *testing.T) { | ||
| t.Parallel() | ||
| ts := time.Now().Add(60 * time.Second) | ||
| got := parseRateLimitReset(strconv.FormatInt(ts.Unix(), 10)) | ||
| assert.False(t, got.IsZero()) | ||
| assert.Equal(t, ts.Unix(), got.Unix()) | ||
| }) | ||
| } | ||
|
|
||
| // TestComputeRetryAfter verifies the retry-after calculation. | ||
| func TestComputeRetryAfter(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("zero time returns default", func(t *testing.T) { | ||
| t.Parallel() | ||
| assert.Equal(t, 60, computeRetryAfter(time.Time{})) | ||
| }) | ||
|
|
||
| t.Run("past time returns default", func(t *testing.T) { | ||
| t.Parallel() | ||
| assert.Equal(t, 60, computeRetryAfter(time.Now().Add(-time.Minute))) | ||
| }) | ||
|
|
||
| t.Run("future time returns seconds until reset", func(t *testing.T) { | ||
| t.Parallel() | ||
| future := time.Now().Add(30 * time.Second) | ||
| secs := computeRetryAfter(future) | ||
| // Allow ±2s for timing jitter. | ||
| assert.GreaterOrEqual(t, secs, 29) | ||
| assert.LessOrEqual(t, secs, 32) | ||
| }) | ||
|
|
||
| t.Run("very far future is clamped to max", func(t *testing.T) { | ||
| t.Parallel() | ||
| farFuture := time.Now().Add(24 * time.Hour) | ||
| assert.Equal(t, 3600, computeRetryAfter(farFuture)) | ||
| }) | ||
| } |
Oops, something went wrong.
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.
These new per-server rate-limit fields are added to the file config (TOML/JSON via ServerConfig), but stdin JSON configs are validated against an embedded upstream schema where stdioServerConfig/httpServerConfig have additionalProperties=false. Since fixSchemaBytes/stripExtensionFieldsForValidation currently do not add/strip rate_limit_threshold or rate_limit_cooldown, providing these fields via stdin JSON will fail schema validation (and convertStdinServerConfig has no wiring for them). If the intent is to support JSON stdin config too, update schema fixing + StdinServerConfig/convertStdinServerConfig accordingly; otherwise the doc comments/defaults should clarify the fields are TOML-only.