Skip to content

perf(limiter): cache plans in memory instead of $lookup per workspace#556

Open
Kuchizu wants to merge 2 commits into
masterfrom
perf/limiter-cache-plans
Open

perf(limiter): cache plans in memory instead of $lookup per workspace#556
Kuchizu wants to merge 2 commits into
masterfrom
perf/limiter-cache-plans

Conversation

@Kuchizu
Copy link
Copy Markdown
Member

@Kuchizu Kuchizu commented May 22, 2026

Cache plans once at limiter startup (like paymaster) and resolve tariffPlan from memory instead of $lookup per workspace.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the limiter worker by caching tariff plans in memory at startup and resolving workspace plans without a MongoDB $lookup per workspace.

Changes:

  • Adds plan collection wiring and startup cache loading in the limiter worker.
  • Adds tariff plan cache helpers in DbHelper.
  • Replaces workspace aggregation lookups with projected workspace reads plus in-memory plan resolution.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
workers/limiter/src/index.ts Initializes the plans collection, passes it to DbHelper, and loads plans during startup.
workers/limiter/src/dbHelper.ts Adds plan caching and replaces $lookup aggregation with workspace projection plus cached plan resolution.
workers/limiter/tests/dbHelper.test.ts Updates test setup to pass the plans collection and preload the plan cache.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread workers/limiter/src/dbHelper.ts
Comment thread workers/limiter/src/dbHelper.ts
@Kuchizu Kuchizu requested review from e11sy and neSpecc May 26, 2026 20:27
Comment on lines +221 to +223
if (!plan) {
this.knownMissingPlanIds.add(planIdStr);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we need to refresh cache when there is no plan found in the cache

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

add tests for plans caching

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.

3 participants