Api rate limiting with per tenant#95
Merged
elizabetheonoja-art merged 3 commits intoJul 18, 2026
Merged
Conversation
…enant Add per-tenant token-bucket rate limiting to meter-simulator
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.
Motivation
Protect simulator API and contract submission paths from noisy or abusive tenants by enforcing local per-tenant quotas with a low-latency, dependency-free limiter.
Provide configurable defaults, observability hooks, and a deployment/runbook plan so the limiter can be tuned and monitored in canary/production rollouts.
Description
Add a dependency-free token-bucket implementation (TenantTokenBucket) and manager (PerTenantRateLimiter) in meter-simulator/src/rate-limiter.js with idle-bucket eviction and snapshotMetrics() for monitoring.
Expose configurable defaults via meter-simulator/src/config.js (rateLimit.capacity, rateLimit.refillRatePerSecond, rateLimit.idleTtlMs).
Enforce rate checks before hot paths by wiring PerTenantRateLimiter.assertAllowed() into submitUsageData and submitZkUsageData in meter-simulator/src/contract-interface.js and into publishUsageData, publishHeartbeat, and publishStatus in meter-simulator/src/mqtt-publisher.js.
Add documentation docs/API_RATE_LIMITING.md describing architecture, config, monitoring/alerts, deployment/canary steps, and a runbook, and add unit tests in meter-simulator/tests/rate-limiter.test.js.
Testing
Ran the new unit tests with npm test -- --runInBand tests/rate-limiter.test.js, and the rate-limiter tests passed.
Performed syntax checks with node -c on updated modules (src/rate-limiter.js, src/contract-interface.js, src/mqtt-publisher.js, src/config.js) and they passed.
Ran the full simulator test suite with npm test -- --runInBand, which revealed pre-existing failures in tests/meter-device.test.js unrelated to the rate-limiter changes (invalid fixture secret key size, peak-hour expectation mismatch, and signature message length expectation), so the complete suite did not pass end-to-end.
Closes #72