Repository navigation
feat: Consolidate rate limiting and usage tracking into usagePlugin - #178
Merged
Merged
Conversation
…177) Creates a unified usagePlugin that auto-applies hooks to all collections, eliminating manual hook registration across 17 collections. Key changes: - Create src/lib/usage/ module with plugin, hooks, tasks, and types - Rename usageStats field group to usage in Clients collection - Rename maxDailyRequests to peakDailyRequests - Remove unused totalRequests field - Task slugs changed: trackClientUsage → trackUsage, resetClientUsage → resetUsage - Remove manual hook registrations from all 17 content/resource/tag collections - Delete old src/lib/rateLimiting.ts and src/jobs/tasks/TrackUsage.ts - Create database migration for field renames - Update tests and documentation Co-Authored-By: Claude Opus 4.5 <[email protected]>
- Move consumer collections to CONSUMER_COLLECTIONS constant - Add STATS_FIELD_PATH and HIGH_USAGE_THRESHOLD constants - Remove createInitStatsHook (rely on field defaults) - Simplify UsagePluginOptions to just enabled + exclude - Streamline checkRateLimit function - Export tasks as configs instead of factory functions - Remove consumerCollection from TrackUsageInput Co-Authored-By: Claude Opus 4.5 <[email protected]>
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.
Summary
Closes #177
This PR consolidates all rate limiting and usage tracking code into a single
usagePlugin, following the establishedaccessPluginpattern. The plugin auto-applies hooks to collections, eliminating manual hook registration across 17 collections.Key Changes
New usagePlugin (
src/lib/usage/) - Plugin auto-applies hooks to collections:beforeOperationhook for rate limiting (Cloudflare Workers binding)afterReadhook for usage tracking (queues job)beforeChangehook for consumer collections (initializes usage stats)Field Renames (with migration):
usageStats→usage(field group)maxDailyRequests→peakDailyRequeststotalRequestsfieldTask Renames:
trackClientUsage→trackUsageresetClientUsage→resetUsageRemoved Manual Hooks from 17 collections:
Deleted Files:
src/jobs/tasks/TrackUsage.ts(moved to plugin)src/lib/rateLimiting.ts(integrated into hooks.ts)Plugin Configuration
Test plan
buildRateLimitKey,validateUserId) exported for testingpnpm payload migrateTest Results
🤖 Generated with Claude Code