casbin / Casbin.NET

An authorization library that supports access control models like ACL, RBAC, ABAC in .NET (C#)
https://casbin.org
Apache License 2.0
1.13k stars 110 forks source link

fix: Concurrent error caused by GFunctionCache. #297

Closed AsakusaRinne closed 1 year ago

AsakusaRinne commented 1 year ago

fix #296 : Concurrent error caused by GFunctionCache.

casbin-bot commented 1 year ago

@sagilio please review

sagilio commented 1 year ago

I think ReadWriteLock may be more suitable, here is a ref: https://github.com/casbin/Casbin.NET/blob/7f14625585b9d9adebbfb6576c15a55e6c1c2818/Casbin/Caching/EnforceCache.cs#L9. And the ExpressionCache maybe can also use in the same way.later

AsakusaRinne commented 1 year ago

I think ReadWriteLock may be more suitable, here is a ref:

https://github.com/casbin/Casbin.NET/blob/7f14625585b9d9adebbfb6576c15a55e6c1c2818/Casbin/Caching/EnforceCache.cs#L9

. And the ExpressionCache maybe can also use in the same way.later

I think the ConcurrentDictionary could be more efficient in this condition. I made some benchmarks here.

Processor: Intel i7-12700 OS: Windows 11 64bits Runtime: .NET 7.0

Method Mean Error StdDev Gen0 Allocated
AbacModel 156.2 ns 1.80 ns 1.19 ns - 32 B
BasicModel 204.3 ns 3.09 ns 1.84 ns - 32 B
KeyMatchModel 343.7 ns 3.02 ns 2.00 ns 0.0010 336 B
PriorityModel 246.9 ns 2.48 ns 1.30 ns - 120 B
RbacModel 475.3 ns 5.61 ns 3.71 ns 0.0010 296 B
RbacModelWithSmallScale 11,559.2 ns 95.83 ns 57.02 ns 0.0305 10016 B
RbacModelWithMediumScale 113,268.4 ns 1,778.27 ns 1,176.21 ns 0.3662 96424 B
RbacModelWithLargeScale 1,172,893.2 ns 17,688.30 ns 11,699.71 ns 1.9531 1039625 B
RbacModelWithResourceRoles 281.5 ns 2.75 ns 1.82 ns 0.0005 208 B
RbacModelWithDomains 300.7 ns 30.41 ns 18.10 ns 0.0005 144 B
RbacModelWithDeny 717.7 ns 10.85 ns 7.18 ns 0.0010 480 B
Method Mean Error StdDev Gen0 Allocated
AbacModel 155.8 ns 1.99 ns 1.31 ns - 32 B
BasicModel 204.0 ns 2.74 ns 1.81 ns - 32 B
KeyMatchModel 342.9 ns 2.93 ns 1.94 ns 0.0010 336 B
PriorityModel 247.9 ns 3.40 ns 2.25 ns - 120 B
RbacModel 470.4 ns 3.98 ns 2.63 ns 0.0010 296 B
RbacModelWithSmallScale 11,902.6 ns 122.37 ns 80.94 ns 0.0305 10016 B
RbacModelWithMediumScale 115,028.2 ns 897.78 ns 593.82 ns 0.3662 96424 B
RbacModelWithLargeScale 1,272,455.1 ns 10,652.52 ns 7,045.99 ns 3.9063 1039625 B
RbacModelWithResourceRoles 287.3 ns 5.29 ns 2.77 ns 0.0005 208 B
RbacModelWithDomains 291.0 ns 5.14 ns 3.40 ns 0.0005 144 B
RbacModelWithDeny 998.7 ns 124.33 ns 82.24 ns 0.0010 480 B
Method Mean Error StdDev Gen0 Allocated
AbacModel 155.3 ns 0.96 ns 0.63 ns - 32 B
BasicModel 286.7 ns 78.55 ns 51.96 ns - 32 B
KeyMatchModel 449.7 ns 61.19 ns 40.47 ns 0.0010 336 B
PriorityModel 282.9 ns 23.96 ns 12.53 ns - 120 B
RbacModel 618.2 ns 125.68 ns 83.13 ns 0.0010 296 B
RbacModelWithSmallScale 15,285.4 ns 2,712.99 ns 1,418.95 ns 0.0305 10016 B
RbacModelWithMediumScale 158,077.8 ns 36,768.17 ns 21,880.15 ns 0.2441 96424 B
RbacModelWithLargeScale 1,705,608.4 ns 353,096.91 ns 233,551.75 ns 1.9531 1039625 B
RbacModelWithResourceRoles 396.7 ns 81.66 ns 54.02 ns 0.0005 208 B
RbacModelWithDomains 330.8 ns 14.10 ns 7.37 ns 0.0005 144 B
RbacModelWithDeny 838.7 ns 10.37 ns 6.86 ns 0.0010 480 B

As we can see, ConcurrentDictionary is faster than ReadWriteLock except RbacModelWithDeny.

The code of adding ReadWriteLock for benchmark is at here.

github-actions[bot] commented 1 year ago

:tada: This PR is included in version 2.0.0-preview.5 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket:

github-actions[bot] commented 11 months ago

:tada: This PR is included in version 2.0.0 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket:

github-actions[bot] commented 11 months ago

:tada: This PR is included in version 2.0.0 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: