Implement API rate limiting middleware with event tracking and paid access integration#40
Conversation
Co-authored-by: DavidASix <3901710+DavidASix@users.noreply.github.com>
|
@copilot this has gotten a bit stale, so I want you to re-write with your updated copilot instructions and fix the merge conflict. Also merge in main to this branch. |
Co-authored-by: DavidASix <3901710+DavidASix@users.noreply.github.com>
I've successfully updated the implementation with the latest copilot instructions and merged main into the branch. The merge conflict has been resolved by properly combining the Key updates completed:
The rate limiting system is now fully compatible with the latest codebase and includes both paid access validation and event-based rate limiting functionality. |
This PR introduces a comprehensive API rate limiting system that prevents abuse and manages resource consumption through event-based tracking, now fully integrated with the existing paid access middleware.
Implementation
Core Middleware
withEventRateLimit: A chainable middleware that enforces rate limits based on database event trackingcountEventsInTimeWindow()function tracks user events within configurable time windowswithPaidAccessmiddleware for subscription-based endpointsKey Features
withAuth,withApiKey,withPaidAccess,withBody)Usage Example
Applied Rate Limiting
/api/v1/fetch-updated-data: Limited to 100fetch_reviewscalls per 24 hours, with paid access validationError Response Format
When limits are exceeded, returns structured error response:
{ "error": "Rate limit exceeded", "details": { "event": "fetch_reviews", "maxCalls": 100, "timeWindowHours": 24, "currentCount": 101 } }Integration with Existing Systems
The rate limiting middleware has been designed to work alongside the existing
withPaidAccessmiddleware, ensuring that:Testing
Comprehensive test suite covering rate limit enforcement, event recording, error handling, and middleware chaining scenarios. All tests pass with full coverage.
Fixes #34.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.