Skip to content

Imp(limiter): fetch workspaces with cursor#554

Merged
e11sy merged 5 commits into
masterfrom
imp/fetch-workspaces-with-cursor
May 13, 2026
Merged

Imp(limiter): fetch workspaces with cursor#554
e11sy merged 5 commits into
masterfrom
imp/fetch-workspaces-with-cursor

Conversation

@e11sy
Copy link
Copy Markdown
Member

@e11sy e11sy commented May 11, 2026

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
image

Solution:

  • use projection
  • use cursor based retrieving, yield each workspace and store only 1 workspace at a time

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 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 $project stage.
  • Updated the limiter’s regular check to iterate workspaces with for await ... of instead of Promise.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 updatedWorkspaces still 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.

Comment thread workers/limiter/src/dbHelper.ts Outdated
Comment thread workers/limiter/src/index.ts Outdated
Comment thread workers/limiter/src/dbHelper.ts Outdated
@e11sy e11sy force-pushed the imp/fetch-workspaces-with-cursor branch from 2f68edc to 0053c2c Compare May 12, 2026 23:21
Comment thread workers/limiter/src/dbHelper.ts Outdated
Kuchizu
Kuchizu previously approved these changes May 13, 2026
@e11sy e11sy merged commit 8f68626 into master May 13, 2026
5 checks passed
@e11sy e11sy deleted the imp/fetch-workspaces-with-cursor branch May 13, 2026 16:16
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>
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.

4 participants