Feat/rate limit violation tracking claude opus#74
Open
kudroma404 wants to merge 4 commits into
Open
Conversation
…feature, and violation_flush_interval_sec config Made-with: Cursor
… implementations Made-with: Cursor
…r startup Made-with: Cursor
Made-with: Cursor
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Rate Limit Violation Tracking
Summary
Adds batched rate-limit violation tracking that accumulates per-client violations in memory and periodically flushes them to a dedicated PostgreSQL table. This avoids spamming the database with individual violation records on hot paths.
Motivation
Previously, rate limit violations were only observable through HTTP 429 responses. There was no persistent record of how often limits were hit, by which clients, or through which limiters. This makes it difficult to tune rate limits, detect abuse patterns, or understand capacity pressure across gateway instances.
Design
The implementation follows the existing
EventRecorder/EventSinkpattern already used for activity and worker events:RateLimitViolationTracker) -- usesscc::HashMapwith per-bucket locking for contention-freerecord()calls on hot request paths. A background task drains the map on a configurable interval (default 60s).RateLimitViolationSink) -- abstracts persistence so tests can useInMemoryViolationSinkorNoopwithout a database.rate_limit_violations) -- each flush produces one row per gateway instance withgateway_name,total_count, and adetailsJSONB column mapping"client_id:limiter_name"to count.Integration points
Violations are captured at both rate-limiting layers:
limiter.handle()call, the response status is checked; if 429, the violation is recorded with the source IP and limiter name (basic,add_result,load,leader,metric,status,update_key,unauthorized_only,read).check_and_incrreturnsfalseincheck_subject_limit, the violation is recorded with the subject type and ID before returningTooManyRequests.Changes
dev-env/init-scripts/init-schema.sql-- newrate_limit_violationstable with index on(gateway_name, created_at DESC)Cargo.toml-- addedwith-serde_json-1feature totokio-postgresfor JSONB parameter supportsrc/config.rs-- addedviolation_flush_interval_secfield toDbConfig(default 60s)src/db/mod.rs--RateLimitViolationSinktrait,InMemoryViolationSink,ViolationSinkHandleenum,ViolationRowsrc/db/repository.rs--Database::record_violations_batchINSERT querysrc/db/violation_tracker.rs-- new file:RateLimitViolationTrackerstruct with accumulator + periodic flusher + 5 unit testssrc/http3/rate_limits.rs--record_ip_violation_if_limitedhelper; hooks in all 9 per-IP handlers andcheck_subject_limitsrc/http3/state.rs--violation_trackerfield + accessor onHttpStatesrc/http3/server.rs-- pass tracker throughHttp3Server::runsrc/raft/mod.rs-- create tracker at startup alongsideEventRecordersrc/test_support.rs-- acceptViolationSinkHandleinbuild_shared_harness_coretests/client_http_api/support.rs--ViolationTestHarness+build_harness_with_violation_trackingtests/client_http_api/rate_limit_violations.rs-- new file: 4 integration teststests/event_tracker/support.rs-- passViolationSinkHandle::Noopto updated harness builderdev-env/config/*.toml-- addedviolation_flush_interval_sec = 60to all dev configsDatabase migration
Run manually on existing deployments before deploying: