Skip to content

Commit

Permalink
feat: add ContextAdapter interface to support context in adapters (#1305
Browse files Browse the repository at this point in the history
)

* feat: AdapterWithContext interface

* refactor: ContextAdapter interface

* Update adapter_context.go

* Update adapter_context.go

* Update adapter_context.go

---------

Co-authored-by: hsluoyz <hsluoyz@qq.com>
  • Loading branch information
PokIsemaine and hsluoyz committed Aug 28, 2023
1 parent 3b88403 commit 37b0889
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions persist/adapter_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2023 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package persist

import (
"context"

"github.com/casbin/casbin/v2/model"
)

// ContextAdapter provides a context-aware interface for Casbin adapters.
type ContextAdapter interface {
Adapter

// LoadPolicyCtx loads all policy rules from the storage with context.
LoadPolicyCtx(ctx context.Context, model model.Model) error
// SavePolicyCtx saves all policy rules to the storage with context.
SavePolicyCtx(ctx context.Context, model model.Model) error

// AddPolicyCtx adds a policy rule to the storage with context.
// This is part of the Auto-Save feature.
AddPolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error
// RemovePolicyCtx removes a policy rule from the storage with context.
// This is part of the Auto-Save feature.
RemovePolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error
// RemoveFilteredPolicyCtx removes policy rules that match the filter from the storage with context.
// This is part of the Auto-Save feature.
RemoveFilteredPolicyCtx(ctx context.Context, sec string, ptype string, fieldIndex int, fieldValues ...string) error
}

1 comment on commit 37b0889

@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: 37b0889 Previous: 3b88403 Ratio
BenchmarkCachedRaw - ns/op 24.61 ns/op 24.73 ns/op 1.00
BenchmarkCachedRaw - B/op 0 B/op 0 B/op NaN
BenchmarkCachedRaw - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkCachedBasicModel - ns/op 285.2 ns/op 300.9 ns/op 0.95
BenchmarkCachedBasicModel - B/op 104 B/op 104 B/op 1
BenchmarkCachedBasicModel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModel - ns/op 280.7 ns/op 294 ns/op 0.95
BenchmarkCachedRBACModel - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelSmall - ns/op 283.4 ns/op 302.5 ns/op 0.94
BenchmarkCachedRBACModelSmall - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModelSmall - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelMedium - ns/op 310 ns/op 317.2 ns/op 0.98
BenchmarkCachedRBACModelMedium - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModelMedium - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelLarge - ns/op 269 ns/op 264.4 ns/op 1.02
BenchmarkCachedRBACModelLarge - B/op 97 B/op 97 B/op 1
BenchmarkCachedRBACModelLarge - allocs/op 3 allocs/op 3 allocs/op 1
BenchmarkCachedRBACModelWithResourceRoles - ns/op 285.9 ns/op 289.6 ns/op 0.99
BenchmarkCachedRBACModelWithResourceRoles - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModelWithResourceRoles - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelWithDomains - ns/op 299.1 ns/op 329.9 ns/op 0.91
BenchmarkCachedRBACModelWithDomains - B/op 120 B/op 120 B/op 1
BenchmarkCachedRBACModelWithDomains - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedABACModel - ns/op 4422 ns/op 4669 ns/op 0.95
BenchmarkCachedABACModel - B/op 1520 B/op 1521 B/op 1.00
BenchmarkCachedABACModel - allocs/op 18 allocs/op 18 allocs/op 1
BenchmarkCachedKeyMatchModel - ns/op 315.6 ns/op 319.7 ns/op 0.99
BenchmarkCachedKeyMatchModel - B/op 152 B/op 152 B/op 1
BenchmarkCachedKeyMatchModel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelWithDeny - ns/op 289.6 ns/op 295.2 ns/op 0.98
BenchmarkCachedRBACModelWithDeny - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModelWithDeny - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedPriorityModel - ns/op 283 ns/op 300.6 ns/op 0.94
BenchmarkCachedPriorityModel - B/op 104 B/op 104 B/op 1
BenchmarkCachedPriorityModel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedWithEnforceContext - ns/op 542.8 ns/op 575 ns/op 0.94
BenchmarkCachedWithEnforceContext - B/op 240 B/op 240 B/op 1
BenchmarkCachedWithEnforceContext - allocs/op 5 allocs/op 5 allocs/op 1
BenchmarkCachedRBACModelMediumParallel - ns/op 277.4 ns/op 260.9 ns/op 1.06
BenchmarkCachedRBACModelMediumParallel - B/op 105 B/op 105 B/op 1
BenchmarkCachedRBACModelMediumParallel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkHasPolicySmall - ns/op 859.1 ns/op 864.2 ns/op 0.99
BenchmarkHasPolicySmall - B/op 150 B/op 150 B/op 1
BenchmarkHasPolicySmall - allocs/op 6 allocs/op 6 allocs/op 1
BenchmarkHasPolicyMedium - ns/op 894.2 ns/op 949.1 ns/op 0.94
BenchmarkHasPolicyMedium - B/op 157 B/op 157 B/op 1
BenchmarkHasPolicyMedium - allocs/op 6 allocs/op 6 allocs/op 1
BenchmarkHasPolicyLarge - ns/op 956.6 ns/op 1021 ns/op 0.94
BenchmarkHasPolicyLarge - B/op 165 B/op 165 B/op 1
BenchmarkHasPolicyLarge - allocs/op 7 allocs/op 7 allocs/op 1
BenchmarkAddPolicySmall - ns/op 877.3 ns/op 912.3 ns/op 0.96
BenchmarkAddPolicySmall - B/op 152 B/op 152 B/op 1
BenchmarkAddPolicySmall - allocs/op 6 allocs/op 6 allocs/op 1
BenchmarkAddPolicyMedium - ns/op 1130 ns/op 1335 ns/op 0.85
BenchmarkAddPolicyMedium - B/op 185 B/op 190 B/op 0.97
BenchmarkAddPolicyMedium - allocs/op 7 allocs/op 7 allocs/op 1
BenchmarkAddPolicyLarge - ns/op 1951 ns/op 2168 ns/op 0.90
BenchmarkAddPolicyLarge - B/op 417 B/op 430 B/op 0.97
BenchmarkAddPolicyLarge - allocs/op 9 allocs/op 9 allocs/op 1
BenchmarkRemovePolicySmall - ns/op 865.1 ns/op 925.3 ns/op 0.93
BenchmarkRemovePolicySmall - B/op 166 B/op 166 B/op 1
BenchmarkRemovePolicySmall - allocs/op 7 allocs/op 7 allocs/op 1
BenchmarkRemovePolicyMedium - ns/op 1024 ns/op 1062 ns/op 0.96
BenchmarkRemovePolicyMedium - B/op 179 B/op 179 B/op 1
BenchmarkRemovePolicyMedium - allocs/op 7 allocs/op 7 allocs/op 1
BenchmarkRemovePolicyLarge - ns/op 2382 ns/op 2332 ns/op 1.02
BenchmarkRemovePolicyLarge - B/op 305 B/op 297 B/op 1.03
BenchmarkRemovePolicyLarge - allocs/op 13 allocs/op 13 allocs/op 1
BenchmarkRaw - ns/op 24.3 ns/op 24.83 ns/op 0.98
BenchmarkRaw - B/op 0 B/op 0 B/op NaN
BenchmarkRaw - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkBasicModel - ns/op 5703 ns/op 6203 ns/op 0.92
BenchmarkBasicModel - B/op 1491 B/op 1489 B/op 1.00
BenchmarkBasicModel - allocs/op 17 allocs/op 17 allocs/op 1
BenchmarkRBACModel - ns/op 8300 ns/op 9743 ns/op 0.85
BenchmarkRBACModel - B/op 2038 B/op 2032 B/op 1.00
BenchmarkRBACModel - allocs/op 35 allocs/op 35 allocs/op 1
BenchmarkRBACModelSizes/small - ns/op 78977 ns/op 85933 ns/op 0.92
BenchmarkRBACModelSizes/small - B/op 20016 B/op 20001 B/op 1.00
BenchmarkRBACModelSizes/small - allocs/op 480 allocs/op 480 allocs/op 1
BenchmarkRBACModelSizes/medium - ns/op 958915 ns/op 1034963 ns/op 0.93
BenchmarkRBACModelSizes/medium - B/op 191303 B/op 191262 B/op 1.00
BenchmarkRBACModelSizes/medium - allocs/op 4826 allocs/op 4827 allocs/op 1.00
BenchmarkRBACModelSizes/large - ns/op 10072846 ns/op 12002893 ns/op 0.84
BenchmarkRBACModelSizes/large - B/op 1894800 B/op 1913428 B/op 0.99
BenchmarkRBACModelSizes/large - allocs/op 48040 allocs/op 48547 allocs/op 0.99
BenchmarkRBACModelSmall - ns/op 90194 ns/op 102949 ns/op 0.88
BenchmarkRBACModelSmall - B/op 20105 B/op 20044 B/op 1.00
BenchmarkRBACModelSmall - allocs/op 615 allocs/op 615 allocs/op 1
BenchmarkRBACModelMedium - ns/op 925728 ns/op 1028840 ns/op 0.90
BenchmarkRBACModelMedium - B/op 194240 B/op 194148 B/op 1.00
BenchmarkRBACModelMedium - allocs/op 6024 allocs/op 6024 allocs/op 1
BenchmarkRBACModelLarge - ns/op 10995712 ns/op 13069146 ns/op 0.84
BenchmarkRBACModelLarge - B/op 1952396 B/op 1954091 B/op 1.00
BenchmarkRBACModelLarge - allocs/op 61097 allocs/op 61164 allocs/op 1.00
BenchmarkRBACModelWithResourceRoles - ns/op 6879 ns/op 7598 ns/op 0.91
BenchmarkRBACModelWithResourceRoles - B/op 1823 B/op 1820 B/op 1.00
BenchmarkRBACModelWithResourceRoles - allocs/op 27 allocs/op 27 allocs/op 1
BenchmarkRBACModelWithDomains - ns/op 7779 ns/op 8634 ns/op 0.90
BenchmarkRBACModelWithDomains - B/op 1806 B/op 1803 B/op 1.00
BenchmarkRBACModelWithDomains - allocs/op 25 allocs/op 25 allocs/op 1
BenchmarkABACModel - ns/op 4274 ns/op 4809 ns/op 0.89
BenchmarkABACModel - B/op 1515 B/op 1512 B/op 1.00
BenchmarkABACModel - allocs/op 17 allocs/op 17 allocs/op 1
BenchmarkABACRuleModel - ns/op 5683517 ns/op 6256070 ns/op 0.91
BenchmarkABACRuleModel - B/op 1305638 B/op 1304174 B/op 1.00
BenchmarkABACRuleModel - allocs/op 40088 allocs/op 40088 allocs/op 1
BenchmarkKeyMatchModel - ns/op 9026 ns/op 10028 ns/op 0.90
BenchmarkKeyMatchModel - B/op 3027 B/op 3020 B/op 1.00
BenchmarkKeyMatchModel - allocs/op 37 allocs/op 37 allocs/op 1
BenchmarkRBACModelWithDeny - ns/op 10934 ns/op 11700 ns/op 0.93
BenchmarkRBACModelWithDeny - B/op 2448 B/op 2445 B/op 1.00
BenchmarkRBACModelWithDeny - allocs/op 49 allocs/op 49 allocs/op 1
BenchmarkPriorityModel - ns/op 6280 ns/op 7363 ns/op 0.85
BenchmarkPriorityModel - B/op 1741 B/op 1738 B/op 1.00
BenchmarkPriorityModel - allocs/op 22 allocs/op 22 allocs/op 1
BenchmarkRBACModelWithDomainPatternLarge - ns/op 34289 ns/op 41277 ns/op 0.83
BenchmarkRBACModelWithDomainPatternLarge - B/op 16602 B/op 16611 B/op 1.00
BenchmarkRBACModelWithDomainPatternLarge - allocs/op 164 allocs/op 164 allocs/op 1
BenchmarkRoleManagerSmall - ns/op 121817 ns/op 125101 ns/op 0.97
BenchmarkRoleManagerSmall - B/op 11953 B/op 11952 B/op 1.00
BenchmarkRoleManagerSmall - allocs/op 797 allocs/op 797 allocs/op 1
BenchmarkRoleManagerMedium - ns/op 1257872 ns/op 1310573 ns/op 0.96
BenchmarkRoleManagerMedium - B/op 125908 B/op 125907 B/op 1.00
BenchmarkRoleManagerMedium - allocs/op 8741 allocs/op 8741 allocs/op 1
BenchmarkRoleManagerLarge - ns/op 15997523 ns/op 18311677 ns/op 0.87
BenchmarkRoleManagerLarge - B/op 1349920 B/op 1349920 B/op 1
BenchmarkRoleManagerLarge - allocs/op 89741 allocs/op 89741 allocs/op 1
BenchmarkBuildRoleLinksWithPatternLarge - ns/op 9639008475 ns/op 11254167453 ns/op 0.86
BenchmarkBuildRoleLinksWithPatternLarge - B/op 5294561368 B/op 5289092176 B/op 1.00
BenchmarkBuildRoleLinksWithPatternLarge - allocs/op 60938206 allocs/op 60936349 allocs/op 1.00
BenchmarkBuildRoleLinksWithDomainPatternLarge - ns/op 257551844 ns/op 326736696 ns/op 0.79
BenchmarkBuildRoleLinksWithDomainPatternLarge - B/op 140019348 B/op 139839698 B/op 1.00
BenchmarkBuildRoleLinksWithDomainPatternLarge - allocs/op 1676221 allocs/op 1676165 allocs/op 1.00
BenchmarkBuildRoleLinksWithPatternAndDomainPatternLarge - ns/op 9926721989 ns/op 11517714364 ns/op 0.86
BenchmarkBuildRoleLinksWithPatternAndDomainPatternLarge - B/op 5432188224 B/op 5425607368 B/op 1.00
BenchmarkBuildRoleLinksWithPatternAndDomainPatternLarge - allocs/op 62548730 allocs/op 62546688 allocs/op 1.00
BenchmarkHasLinkWithPatternLarge - ns/op 16006 ns/op 17972 ns/op 0.89
BenchmarkHasLinkWithPatternLarge - B/op 7551 B/op 7547 B/op 1.00
BenchmarkHasLinkWithPatternLarge - allocs/op 111 allocs/op 111 allocs/op 1
BenchmarkHasLinkWithDomainPatternLarge - ns/op 852.1 ns/op 909.1 ns/op 0.94
BenchmarkHasLinkWithDomainPatternLarge - B/op 80 B/op 80 B/op 1
BenchmarkHasLinkWithDomainPatternLarge - allocs/op 5 allocs/op 5 allocs/op 1
BenchmarkHasLinkWithPatternAndDomainPatternLarge - ns/op 16425 ns/op 17606 ns/op 0.93
BenchmarkHasLinkWithPatternAndDomainPatternLarge - B/op 7550 B/op 7549 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.

Please sign in to comment.