Commit 51fda65
Add rate limiting CRD types and token-bucket package (#4577)
* Add rate limiting types to MCPServerSpec
Add RateLimitConfig, RateLimitBucket, and ToolRateLimitConfig CRD types
to support configurable token-bucket rate limiting on MCPServer. The
rateLimiting field accepts shared and per-tool bucket configs with
maxTokens and refillPeriod (metav1.Duration) parameters.
"Shared" denotes a bucket shared across all users, distinguishing it
from future per-user buckets (#4550).
CEL validation ensures:
- At least one of shared or tools is configured
- Redis session storage is required when rateLimiting is set
ToolRateLimitConfig.Shared is required since a tool entry without a
bucket is meaningless. Tools uses +listType=map with +listMapKey=name
to reject duplicate tool names at admission.
Part of #4551
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Add token-bucket rate limiter package with tests
Introduce pkg/ratelimit/ with a public Limiter interface and internal
token bucket implementation backed by a Redis Lua script.
Public API surface is minimal:
- Limiter interface with Allow(ctx, toolName, userID)
- Decision result type with Allowed and RetryAfter
- NewLimiter constructor accepting CRD types directly
Internal (pkg/ratelimit/internal/bucket/):
- Atomic multi-key Lua script that checks all buckets before consuming
from any, preventing rejected per-tool calls from draining the
server-level budget
- Uses Redis TIME for clock consistency and miniredis testability
- Guards against negative elapsed time from clock drift
- Key format and derivation fully encapsulated in the internal package
Tests cover all acceptance criteria for global rate limits:
- AC3: requests exceeding maxTokens are rejected
- AC4: Retry-After computed from the bucket refill rate
- AC5: per-tool limit on one tool does not affect other tools
- AC6: request must pass both server-level and per-tool limits
- Atomic multi-bucket: tool rejection does not drain server budget
- Redis unavailability returns error (fail-open is caller's job)
- No-op limiter when config is nil
Part of #4551
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent bd02232 commit 51fda65
File tree
10 files changed
+1083
-1
lines changed- cmd/thv-operator/api/v1alpha1
- deploy/charts/operator-crds
- files/crds
- templates
- docs/operator
- pkg/ratelimit
- internal/bucket
10 files changed
+1083
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
| 157 | + | |
157 | 158 | | |
158 | 159 | | |
159 | 160 | | |
| |||
331 | 332 | | |
332 | 333 | | |
333 | 334 | | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
334 | 340 | | |
335 | 341 | | |
336 | 342 | | |
| |||
481 | 487 | | |
482 | 488 | | |
483 | 489 | | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
484 | 538 | | |
485 | 539 | | |
486 | 540 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
| |||
65 | 67 | | |
66 | 68 | | |
67 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
68 | 119 | | |
69 | 120 | | |
70 | 121 | | |
| |||
80 | 131 | | |
81 | 132 | | |
82 | 133 | | |
83 | | - | |
| 134 | + | |
84 | 135 | | |
85 | 136 | | |
86 | 137 | | |
| |||
102 | 153 | | |
103 | 154 | | |
104 | 155 | | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
105 | 166 | | |
106 | 167 | | |
107 | 168 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 79 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
497 | 497 | | |
498 | 498 | | |
499 | 499 | | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
500 | 576 | | |
501 | 577 | | |
502 | 578 | | |
| |||
886 | 962 | | |
887 | 963 | | |
888 | 964 | | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
889 | 968 | | |
890 | 969 | | |
891 | 970 | | |
| |||
0 commit comments