Skip to content

Api rate limiting with per tenant#95

Merged
elizabetheonoja-art merged 3 commits into
Utility-Protocol:mainfrom
gloskull:API-Rate-Limiting-with-Per-Tenant
Jul 18, 2026
Merged

Api rate limiting with per tenant#95
elizabetheonoja-art merged 3 commits into
Utility-Protocol:mainfrom
gloskull:API-Rate-Limiting-with-Per-Tenant

Conversation

@gloskull

Copy link
Copy Markdown
Contributor

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

@elizabetheonoja-art
elizabetheonoja-art merged commit 67db8c2 into Utility-Protocol:main Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API Rate Limiting with Per-Tenant Token Buckets

2 participants