Casbin.NET icon indicating copy to clipboard operation
Casbin.NET copied to clipboard

Using LoadFilteredPolicy loads all policies unfiltered

Open thoraj opened this issue 3 years ago • 3 comments

We are using Casbin extensively in our system.

We're using Casbin.Net 1.12.2 and Casbin.NET.Adapter.EFCore 1.80

As the number of policies grows we are experiencing performance issues.

A while back we started filterering policies per tenant. So now we:

  • Determine the tenant
  • Create an EFCoreAdapter
  • LoadFilteredPolicies for tenant into adapter
  • Create enforcer (new Enforcer(model, adapter)

So the adapter has already loaded all policies when handed to the Enforcer constructor

However it seems all policies are still loaded when creating the enforcer. We have instrumented our code and it shows that:

  • The adapter (correctly) issues a query which filters the policies SELECT c.id, c.ptype, c.v0, c.v1, c.v2, c.v3, c.v4, c.v5 FROM casbin_rule AS c WHERE ((c.ptype = @__policyType_0) AND (c.v0 = @__field_1)) AND (c.v1 = @__field_2)

  • The enforcer issues another query which is NOT filtered. SELECT c.id, c.ptype, c.v0, c.v1, c.v2, c.v3, c.v4, c.v5 FROM casbin_rule AS c

The second non-filtered query is issued while the Enforcer is being constructed.

Is there some way we can prevent this from happening such that only policies for the tenant are loaded?

-- Thor A. Johansen

thoraj avatar Sep 14 '22 11:09 thoraj

@sagilio @sociometry @AsakusaRinne

casbin-bot avatar Sep 14 '22 11:09 casbin-bot

@AsakusaRinne

/cc @sagilio

hsluoyz avatar Sep 14 '22 12:09 hsluoyz

We plan to add the new option AutoLoadPolicy(#295) to control this behavior, the changes will also try implemented in the v1 version.

sagilio avatar Oct 01 '22 23:10 sagilio

Closed as resolved

hsluoyz avatar Apr 04 '24 14:04 hsluoyz