Skip to content

Commit

Permalink
feat: fix concurrent map writes issue in getCachedResult() (#1348)
Browse files Browse the repository at this point in the history
Co-authored-by: king <yewei@myhayo.com>
  • Loading branch information
yesgs and king committed Dec 18, 2023
1 parent 6703d2f commit 953c551
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions enforcer_cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func (e *CachedEnforcer) RemovePolicies(rules [][]string) (bool, error) {
}

func (e *CachedEnforcer) getCachedResult(key string) (res bool, err error) {
e.locker.RLock()
defer e.locker.RUnlock()
e.locker.Lock()
defer e.locker.Unlock()
return e.cache.Get(key)
}

Expand Down

2 comments on commit 953c551

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 953c551 Previous: 6703d2f Ratio
BenchmarkCachedRaw - ns/op 17.51 ns/op 17.65 ns/op 0.99
BenchmarkCachedRaw - B/op 0 B/op 0 B/op NaN
BenchmarkCachedRaw - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkCachedBasicModel - ns/op 165.8 ns/op 165.6 ns/op 1.00
BenchmarkCachedBasicModel - B/op 104 B/op 104 B/op 1
BenchmarkCachedBasicModel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModel - ns/op 165.6 ns/op 161.6 ns/op 1.02
BenchmarkCachedRBACModel - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelSmall - ns/op 175.9 ns/op 176.6 ns/op 1.00
BenchmarkCachedRBACModelSmall - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModelSmall - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelMedium - ns/op 181.8 ns/op 188.1 ns/op 0.97
BenchmarkCachedRBACModelMedium - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModelMedium - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelLarge - ns/op 166.6 ns/op 156.4 ns/op 1.07
BenchmarkCachedRBACModelLarge - B/op 96 B/op 96 B/op 1
BenchmarkCachedRBACModelLarge - allocs/op 3 allocs/op 3 allocs/op 1
BenchmarkCachedRBACModelWithResourceRoles - ns/op 165.9 ns/op 163.9 ns/op 1.01
BenchmarkCachedRBACModelWithResourceRoles - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModelWithResourceRoles - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelWithDomains - ns/op 175 ns/op 178.4 ns/op 0.98
BenchmarkCachedRBACModelWithDomains - B/op 120 B/op 120 B/op 1
BenchmarkCachedRBACModelWithDomains - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedABACModel - ns/op 2905 ns/op 2979 ns/op 0.98
BenchmarkCachedABACModel - B/op 1541 B/op 1538 B/op 1.00
BenchmarkCachedABACModel - allocs/op 18 allocs/op 18 allocs/op 1
BenchmarkCachedKeyMatchModel - ns/op 182.5 ns/op 176.2 ns/op 1.04
BenchmarkCachedKeyMatchModel - B/op 152 B/op 152 B/op 1
BenchmarkCachedKeyMatchModel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelWithDeny - ns/op 166.7 ns/op 168.8 ns/op 0.99
BenchmarkCachedRBACModelWithDeny - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModelWithDeny - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedPriorityModel - ns/op 170 ns/op 162.5 ns/op 1.05
BenchmarkCachedPriorityModel - B/op 104 B/op 104 B/op 1
BenchmarkCachedPriorityModel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedWithEnforceContext - ns/op 307.7 ns/op 295.5 ns/op 1.04
BenchmarkCachedWithEnforceContext - B/op 240 B/op 240 B/op 1
BenchmarkCachedWithEnforceContext - allocs/op 5 allocs/op 5 allocs/op 1
BenchmarkCachedRBACModelMediumParallel - ns/op 167.4 ns/op 121.7 ns/op 1.38
BenchmarkCachedRBACModelMediumParallel - B/op 106 B/op 105 B/op 1.01
BenchmarkCachedRBACModelMediumParallel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkHasPolicySmall - ns/op 449.3 ns/op 444.1 ns/op 1.01
BenchmarkHasPolicySmall - B/op 150 B/op 150 B/op 1
BenchmarkHasPolicySmall - allocs/op 6 allocs/op 6 allocs/op 1
BenchmarkHasPolicyMedium - ns/op 480.6 ns/op 483.9 ns/op 0.99
BenchmarkHasPolicyMedium - B/op 157 B/op 157 B/op 1
BenchmarkHasPolicyMedium - allocs/op 6 allocs/op 6 allocs/op 1
BenchmarkHasPolicyLarge - ns/op 502.2 ns/op 503.8 ns/op 1.00
BenchmarkHasPolicyLarge - B/op 165 B/op 165 B/op 1
BenchmarkHasPolicyLarge - allocs/op 7 allocs/op 7 allocs/op 1
BenchmarkAddPolicySmall - ns/op 473.5 ns/op 488.2 ns/op 0.97
BenchmarkAddPolicySmall - B/op 152 B/op 152 B/op 1
BenchmarkAddPolicySmall - allocs/op 6 allocs/op 6 allocs/op 1
BenchmarkAddPolicyMedium - ns/op 549.4 ns/op 563.3 ns/op 0.98
BenchmarkAddPolicyMedium - B/op 172 B/op 173 B/op 0.99
BenchmarkAddPolicyMedium - allocs/op 7 allocs/op 7 allocs/op 1
BenchmarkAddPolicyLarge - ns/op 1116 ns/op 1218 ns/op 0.92
BenchmarkAddPolicyLarge - B/op 473 B/op 473 B/op 1
BenchmarkAddPolicyLarge - allocs/op 9 allocs/op 9 allocs/op 1
BenchmarkRemovePolicySmall - ns/op 503.9 ns/op 488.5 ns/op 1.03
BenchmarkRemovePolicySmall - B/op 166 B/op 166 B/op 1
BenchmarkRemovePolicySmall - allocs/op 7 allocs/op 7 allocs/op 1
BenchmarkRemovePolicyMedium - ns/op 556 ns/op 554.2 ns/op 1.00
BenchmarkRemovePolicyMedium - B/op 176 B/op 176 B/op 1
BenchmarkRemovePolicyMedium - allocs/op 7 allocs/op 7 allocs/op 1
BenchmarkRemovePolicyLarge - ns/op 1257 ns/op 1212 ns/op 1.04
BenchmarkRemovePolicyLarge - B/op 283 B/op 291 B/op 0.97
BenchmarkRemovePolicyLarge - allocs/op 12 allocs/op 13 allocs/op 0.92
BenchmarkRaw - ns/op 17.51 ns/op 17.68 ns/op 0.99
BenchmarkRaw - B/op 0 B/op 0 B/op NaN
BenchmarkRaw - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkBasicModel - ns/op 3636 ns/op 3719 ns/op 0.98
BenchmarkBasicModel - B/op 1506 B/op 1508 B/op 1.00
BenchmarkBasicModel - allocs/op 17 allocs/op 17 allocs/op 1
BenchmarkRBACModel - ns/op 5494 ns/op 5489 ns/op 1.00
BenchmarkRBACModel - B/op 2062 B/op 2065 B/op 1.00
BenchmarkRBACModel - allocs/op 35 allocs/op 35 allocs/op 1
BenchmarkRBACModelSizes/small - ns/op 47369 ns/op 49969 ns/op 0.95
BenchmarkRBACModelSizes/small - B/op 20170 B/op 20271 B/op 1.00
BenchmarkRBACModelSizes/small - allocs/op 480 allocs/op 480 allocs/op 1
BenchmarkRBACModelSizes/medium - ns/op 496395 ns/op 511984 ns/op 0.97
BenchmarkRBACModelSizes/medium - B/op 191741 B/op 191257 B/op 1.00
BenchmarkRBACModelSizes/medium - allocs/op 4829 allocs/op 4829 allocs/op 1
BenchmarkRBACModelSizes/large - ns/op 5279219 ns/op 5195604 ns/op 1.02
BenchmarkRBACModelSizes/large - B/op 1901542 B/op 1906391 B/op 1.00
BenchmarkRBACModelSizes/large - allocs/op 48213 allocs/op 48353 allocs/op 1.00
BenchmarkRBACModelSmall - ns/op 57717 ns/op 59230 ns/op 0.97
BenchmarkRBACModelSmall - B/op 20291 B/op 20352 B/op 1.00
BenchmarkRBACModelSmall - allocs/op 615 allocs/op 615 allocs/op 1
BenchmarkRBACModelMedium - ns/op 571714 ns/op 576178 ns/op 0.99
BenchmarkRBACModelMedium - B/op 194685 B/op 194638 B/op 1.00
BenchmarkRBACModelMedium - allocs/op 6021 allocs/op 6020 allocs/op 1.00
BenchmarkRBACModelLarge - ns/op 6045148 ns/op 6073531 ns/op 1.00
BenchmarkRBACModelLarge - B/op 1941339 B/op 1940496 B/op 1.00
BenchmarkRBACModelLarge - allocs/op 60608 allocs/op 60599 allocs/op 1.00
BenchmarkRBACModelWithResourceRoles - ns/op 4590 ns/op 4605 ns/op 1.00
BenchmarkRBACModelWithResourceRoles - B/op 1843 B/op 1846 B/op 1.00
BenchmarkRBACModelWithResourceRoles - allocs/op 27 allocs/op 27 allocs/op 1
BenchmarkRBACModelWithDomains - ns/op 5139 ns/op 5123 ns/op 1.00
BenchmarkRBACModelWithDomains - B/op 1822 B/op 1824 B/op 1.00
BenchmarkRBACModelWithDomains - allocs/op 25 allocs/op 25 allocs/op 1
BenchmarkABACModel - ns/op 2834 ns/op 2858 ns/op 0.99
BenchmarkABACModel - B/op 1531 B/op 1536 B/op 1.00
BenchmarkABACModel - allocs/op 17 allocs/op 17 allocs/op 1
BenchmarkABACRuleModel - ns/op 3890511 ns/op 3982954 ns/op 0.98
BenchmarkABACRuleModel - B/op 1320609 B/op 1330745 B/op 0.99
BenchmarkABACRuleModel - allocs/op 40091 allocs/op 40092 allocs/op 1.00
BenchmarkKeyMatchModel - ns/op 6130 ns/op 6212 ns/op 0.99
BenchmarkKeyMatchModel - B/op 3064 B/op 3064 B/op 1
BenchmarkKeyMatchModel - allocs/op 37 allocs/op 37 allocs/op 1
BenchmarkRBACModelWithDeny - ns/op 7000 ns/op 6982 ns/op 1.00
BenchmarkRBACModelWithDeny - B/op 2474 B/op 2483 B/op 1.00
BenchmarkRBACModelWithDeny - allocs/op 49 allocs/op 49 allocs/op 1
BenchmarkPriorityModel - ns/op 4200 ns/op 4241 ns/op 0.99
BenchmarkPriorityModel - B/op 1757 B/op 1759 B/op 1.00
BenchmarkPriorityModel - allocs/op 22 allocs/op 22 allocs/op 1
BenchmarkRBACModelWithDomainPatternLarge - ns/op 22991 ns/op 23818 ns/op 0.97
BenchmarkRBACModelWithDomainPatternLarge - B/op 16705 B/op 16762 B/op 1.00
BenchmarkRBACModelWithDomainPatternLarge - allocs/op 164 allocs/op 164 allocs/op 1
BenchmarkRoleManagerSmall - ns/op 69768 ns/op 69717 ns/op 1.00
BenchmarkRoleManagerSmall - B/op 11954 B/op 11955 B/op 1.00
BenchmarkRoleManagerSmall - allocs/op 797 allocs/op 797 allocs/op 1
BenchmarkRoleManagerMedium - ns/op 736714 ns/op 741601 ns/op 0.99
BenchmarkRoleManagerMedium - B/op 125915 B/op 125914 B/op 1.00
BenchmarkRoleManagerMedium - allocs/op 8741 allocs/op 8741 allocs/op 1
BenchmarkRoleManagerLarge - ns/op 8285580 ns/op 8214236 ns/op 1.01
BenchmarkRoleManagerLarge - B/op 1349926 B/op 1349923 B/op 1.00
BenchmarkRoleManagerLarge - allocs/op 89741 allocs/op 89741 allocs/op 1
BenchmarkBuildRoleLinksWithPatternLarge - ns/op 5937835409 ns/op 6040649458 ns/op 0.98
BenchmarkBuildRoleLinksWithPatternLarge - B/op 5335847336 B/op 5350679168 B/op 1.00
BenchmarkBuildRoleLinksWithPatternLarge - allocs/op 60947073 allocs/op 60950665 allocs/op 1.00
BenchmarkBuildRoleLinksWithDomainPatternLarge - ns/op 160150213 ns/op 163583356 ns/op 0.98
BenchmarkBuildRoleLinksWithDomainPatternLarge - B/op 141139862 B/op 141030638 B/op 1.00
BenchmarkBuildRoleLinksWithDomainPatternLarge - allocs/op 1676456 allocs/op 1676469 allocs/op 1.00
BenchmarkBuildRoleLinksWithPatternAndDomainPatternLarge - ns/op 6130166912 ns/op 6200676971 ns/op 0.99
BenchmarkBuildRoleLinksWithPatternAndDomainPatternLarge - B/op 5473784224 B/op 5472251672 B/op 1.00
BenchmarkBuildRoleLinksWithPatternAndDomainPatternLarge - allocs/op 62557797 allocs/op 62557899 allocs/op 1.00
BenchmarkHasLinkWithPatternLarge - ns/op 10376 ns/op 11049 ns/op 0.94
BenchmarkHasLinkWithPatternLarge - B/op 7599 B/op 7599 B/op 1
BenchmarkHasLinkWithPatternLarge - allocs/op 111 allocs/op 111 allocs/op 1
BenchmarkHasLinkWithDomainPatternLarge - ns/op 485.6 ns/op 498.3 ns/op 0.97
BenchmarkHasLinkWithDomainPatternLarge - B/op 80 B/op 80 B/op 1
BenchmarkHasLinkWithDomainPatternLarge - allocs/op 5 allocs/op 5 allocs/op 1
BenchmarkHasLinkWithPatternAndDomainPatternLarge - ns/op 10113 ns/op 10253 ns/op 0.99
BenchmarkHasLinkWithPatternAndDomainPatternLarge - B/op 7604 B/op 7602 B/op 1.00
BenchmarkHasLinkWithPatternAndDomainPatternLarge - allocs/op 111 allocs/op 111 allocs/op 1

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.10.

Benchmark suite Current: 953c551 Previous: 6703d2f Ratio
BenchmarkCachedRBACModelMediumParallel - ns/op 167.4 ns/op 121.7 ns/op 1.38

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.