Skip to content

Add batch mode API for efficient mass policy operations - #522

Closed
mserico with Copilot wants to merge 5 commits into
masterfrom
copilot/fix-casbin-policy-loading
Closed

Add batch mode API for efficient mass policy operations#522
mserico with Copilot wants to merge 5 commits into
masterfrom
copilot/fix-casbin-policy-loading

Conversation

Copilot AI commented Dec 8, 2025

Copy link
Copy Markdown
Contributor

Mass policy operations (thousands of removePolicy()/removeGroupingPolicy() calls) cause severe performance degradation because each operation triggers database write, role link rebuild, and watcher notification. Users were also unnecessarily calling loadPolicy() after each modification, compounding the problem.

Changes

New Batch Mode API

  • startBatch(): Suspends autoSave, autoBuildRoleLinks, and autoNotifyWatcher
  • endBatch(): Restores settings, performs single save and role link rebuild
  • Handles adapters that don't implement savePolicy() gracefully

Documentation

  • Added batch mode usage section to README
  • Clarified that loadPolicy() is unnecessary after modifications when autoSave is enabled

Usage

// Instead of 1000 DB writes + 1000 role rebuilds
enforcer.startBatch();
for (const policy of policies) {
  await enforcer.removePolicy(...policy);
}
await enforcer.endBatch();  // 1 save + 1 rebuild

Performance improvement: O(N) → O(1) for N policy operations.

100% backward compatible - existing code unaffected.

Original prompt

This section details on the original issue you should resolve

<issue_title>Without enforcer.loadPolicy() Casbin behave unpredictably on mass updates.</issue_title>
<issue_description>In our ephemeral environments, we reconstruct permissions by frequently invoking methods like enforcer.removePolicy() and enforcer.removeGroupingPolicy() thousands of times. However, each change necessitates a call to enforcer.loadPolicy(). This is increasingly problematic as the service gets progressively burdened with the growing number of policies to reload with each update, ultimately leading to service downtime.

enforcer is setup as follows:

export const casbinProviders = [
  {
    provide: PROVIDER_CASBIN_ENFORCER,
    useFactory: async (dataSource: DataSource) => {
      const adapter = await TypeORMAdapter.newAdapter({ connection: dataSource });
      const enforcer = await newEnforcer(newModelFromString(PERMISSIONS_MODEL), adapter);
      await enforcer.addFunction('isMatch', casbinIsMatch);
      await enforcer.addFunction('includes', casbinIncludes);
      await enforcer.loadPolicy();
      await enforcer.enableAutoSave(true);
      return enforcer;
    },
    inject: [getDataSourceToken()]
  }
];

In our production environment, the substantial number of policies we manage also poses a significant challenge. The frequent use of methods like enforcer.removePolicy() and enforcer.removeGroupingPolicy(), followed by enforcer.loadPolicy() after each modification, is proving to be problematic. As the number of policies grows, these operations increasingly strain the server during normal usage, leading to severe performance issues and potential service disruptions.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI and others added 4 commits December 8, 2025 12:30
Co-authored-by: mserico <140243407+mserico@users.noreply.github.com>
Co-authored-by: mserico <140243407+mserico@users.noreply.github.com>
Co-authored-by: mserico <140243407+mserico@users.noreply.github.com>
Co-authored-by: mserico <140243407+mserico@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix unpredictable behavior in Casbin on mass updates Add batch mode API for efficient mass policy operations Dec 8, 2025
Copilot AI requested a review from mserico December 8, 2025 12:38
@mserico
mserico marked this pull request as ready for review December 8, 2025 14:05
@mserico mserico closed this Dec 8, 2025
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.

Without enforcer.loadPolicy() Casbin behave unpredictably on mass updates.

3 participants