Imp(limiter): fetch workspaces with cursor#554
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves the limiter worker’s workspace-fetch path to reduce MongoDB query cost and response size by switching from loading all workspaces into memory to streaming them via an aggregation cursor, while also adding a workspace field projection.
Changes:
- Refactored
DbHelper.getWorkspacesWithTariffPlans()to stream workspaces via an async generator and added a$projectstage. - Updated the limiter’s regular check to iterate workspaces with
for await ... ofinstead ofPromise.all(...map(...)). - Adjusted unit tests to consume the async generator.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| workers/limiter/src/dbHelper.ts | Reworks workspace+plan fetch into cursor-backed streaming with projection and a shared lookup pipeline. |
| workers/limiter/src/index.ts | Consumes streamed workspaces in the regular check loop; awaits DB bulk update. |
| workers/limiter/tests/dbHelper.test.ts | Updates tests to collect results from the async generator. |
Comments suppressed due to low confidence (1)
workers/limiter/src/index.ts:209
- The PR description mentions “store only 1 workspace at a time”, but
updatedWorkspacesstill accumulates an entry per workspace, so memory use remains O(n). Consider updating per-workspace (or storing only minimal update payloads like_id+ updated fields) instead of pushing full workspace objects for every iteration.
const workspaces = this.dbHelper.getWorkspacesWithTariffPlans();
const updatedWorkspaces: WorkspaceWithTariffPlan[] = [];
for await (const workspace of workspaces) {
/**
* If workspace is already blocked - do nothing
*/
if (workspace.isBlocked) {
continue;
}
const workspaceProjects = await this.dbHelper.getProjects(workspace._id.toString());
const { shouldBeBlockedByQuota, updatedWorkspace, projectsToUpdate } = await this.prepareWorkspaceUsageUpdate(workspace, workspaceProjects);
updatedWorkspaces.push(updatedWorkspace);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
2f68edc to
0053c2c
Compare
Kuchizu
reviewed
May 13, 2026
Kuchizu
previously approved these changes
May 13, 2026
Kuchizu
approved these changes
May 13, 2026
neSpecc
approved these changes
May 13, 2026
e11sy
added a commit
that referenced
this pull request
May 13, 2026
* feat(metrics): expose worker metrics over HTTP (#547) * perf(notifier): project only notifications field when loading rules (#551) * perf(notifier): project only notifications field when loading rules * test(notifier): update findOne assertion for projection arg * perf(paymaster): batch subscription check over workspaces (#550) * perf(paymaster): batch subscription check over workspaces * test(paymaster): cover multi-batch dispatch in subscription check * refactor(paymaster): pass batch into flush and clear it explicitly * Imp(limiter): fetch workspaces with cursor (#554) * imp(): fetch workspaces one by one * imp(): add workspace projection * imp(): fix function overload * fix(): eslint and types * fix(): missing await --------- Co-authored-by: Егор Коновалов <egorkonovalov@NB060201N01P.local> --------- Co-authored-by: Kuchizu <70284260+Kuchizu@users.noreply.github.com> Co-authored-by: e11sy <130844513+e11sy@users.noreply.github.com> Co-authored-by: Егор Коновалов <egorkonovalov@NB060201N01P.local>
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.
Problem:
hawk.workspaces query has issues with retrieving of all documents at the same time, also it does not have projection and returns whole workspace, that leads to top total execution time and enormous response len

Solution: