Skip to content

fix(accounts): replace plans $lookup with in-memory join#161

Open
Kuchizu wants to merge 2 commits into
masterfrom
fix/plans-read-time
Open

fix(accounts): replace plans $lookup with in-memory join#161
Kuchizu wants to merge 2 commits into
masterfrom
fix/plans-read-time

Conversation

@Kuchizu

@Kuchizu Kuchizu commented Jun 10, 2026

Copy link
Copy Markdown
Member

Replace plans $lookup with in-memory join in limits cache update.

$lookup from workspaces into plans issued one inner read of plans per workspace on every cache refresh, inflating its read-time metric. Fetch plans with a single find and join in memory instead. Resulting limits are unchanged.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates the accounts limits cache refresh logic to avoid a MongoDB $lookup from workspaces into plans (which inflated plan read-time metrics) by fetching plans once and joining them to workspaces in memory before applying workspace/project overrides.

Changes:

  • Replaces the workspaces→plans aggregation $lookup/$unwind with separate Find() calls and an in-memory join via a plansMap.
  • Updates the workspace model used by the cache refresh to store tariffPlanId instead of an embedded plan.
  • Adjusts the limits resolution logic to read plan limits from the joined plan entry while preserving existing override precedence.

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

Comment thread pkg/accounts/cache.go
Comment on lines +95 to +101
// Join plans in memory: $lookup would read plans once per workspace doc
plansCollection := client.mdb.Database(client.database).Collection(plansCollectionName)
plansCursor, err := plansCollection.Find(ctx, bson.D{})
if err != nil {
log.Errorf("Cannot create cursor in %s collection for cache update: %s", plansCollectionName, err)
return err
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No plans is a tiny static collection.

Comment thread pkg/accounts/cache.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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