Skip to content

fix(ratelimits): enterprise and team checks should be pooled limit#1255

Merged
icecrasher321 merged 4 commits intostagingfrom
fix/rate-limits
Sep 5, 2025
Merged

fix(ratelimits): enterprise and team checks should be pooled limit#1255
icecrasher321 merged 4 commits intostagingfrom
fix/rate-limits

Conversation

@icecrasher321
Copy link
Collaborator

Summary

Ratelimits must be pooled by org/team.

Type of Change

  • Bug fix

Testing

Manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link

vercel bot commented Sep 5, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
sim (staging) Building Building Sep 5, 2025 4:44am
sim Ready Ready Preview Comment Sep 5, 2025 4:44am
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
docs Skipped Skipped Sep 5, 2025 4:44am

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This PR implements organization-aware rate limiting for team and enterprise subscription plans, fixing a critical bug where users in these plans were getting individual rate limits instead of shared organization-level limits. The changes affect five core files in the API layer and rate limiting service.

The core issue was that team and enterprise users were being rate limited individually, meaning each user in an organization had their own separate rate limit pool. For example, if an enterprise plan had a 1000 requests/minute limit, each team member would effectively get their own 1000 requests, multiplying the organization's actual capacity by the number of users.

The solution introduces organization-aware rate limiting by:

  1. Replacing direct subscription queries with calls to getHighestPrioritySubscription() which properly resolves both personal and organizational subscriptions
  2. Switching to new subscription-aware methods like checkRateLimitWithSubscription() and getRateLimitStatusWithSubscription() that accept the full subscription object
  3. Using organization ID as the rate limit key for team/enterprise plans through the subscription's referenceId, while maintaining individual user-based limits for free/pro plans

The implementation spans multiple API endpoints including workflow execution (/workflows/[id]/execute), webhook triggers (/webhooks/trigger/[path]), scheduled executions (/schedules/execute), and rate limit status checks (/users/me/rate-limit). The RateLimiter service was enhanced with a new getRateLimitKey() method that determines whether to use the user ID or organization ID based on the subscription plan, and new subscription-aware public methods while maintaining backward compatibility through deprecated legacy methods.

This change ensures that organizations can effectively control their API usage as a whole, with all team members sharing the same rate limit pool identified by the organization's reference ID, which is the expected behavior for team-based subscription tiers.

Confidence score: 4/5

  • This PR addresses a well-defined business logic issue with clear implementation patterns across multiple files
  • The backward compatibility approach with deprecated methods ensures existing code continues to work during transition
  • Pay close attention to the RateLimiter service changes and verify the organization ID resolution logic works correctly

5 files reviewed, 3 comments

Edit Code Review Bot Settings | Greptile

.where(eq(subscription.referenceId, authenticatedUserId))
.limit(1)
// Get user subscription (checks both personal and org subscriptions)
const { getHighestPrioritySubscription } = await import('@/lib/billing/core/subscription')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Using dynamic import here adds unnecessary overhead on every request. Consider importing this function at module level since it's always needed.

@vercel vercel bot temporarily deployed to Preview – docs September 5, 2025 04:32 Inactive
@vercel vercel bot temporarily deployed to Preview – docs September 5, 2025 04:35 Inactive
@vercel vercel bot temporarily deployed to Preview – docs September 5, 2025 04:39 Inactive
@icecrasher321 icecrasher321 merged commit 864622c into staging Sep 5, 2025
6 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/rate-limits branch September 5, 2025 05:50
arenadeveloper02 pushed a commit to arenadeveloper02/p2-sim that referenced this pull request Sep 19, 2025
…imstudioai#1255)

* fix(ratelimits): enterprise and team checks should be pooled limit"

* fix

* fix dynamic imports

* fix tests"
;
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.

1 participant