-
Notifications
You must be signed in to change notification settings - Fork 398
feat: in-process embedded permissions and per-schema-version caching #3166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
josephschorr
wants to merge
4
commits into
authzed:main
Choose a base branch
from
josephschorr:embedded-permissions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e4ddf2d
feat(datalayer): cache schema-derived artifacts on the stored schema
josephschorr e1bf74b
feat(datastore): honor schema mode when writing bootstrap data
josephschorr 78c8485
feat(caveats): cache compiled caveats per schema version
josephschorr 7fc3209
feat(embedded): add in-process permissions library
josephschorr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package caveats | ||
|
|
||
| import ( | ||
| "sync" | ||
|
|
||
| "github.com/authzed/spicedb/pkg/caveats" | ||
| "github.com/authzed/spicedb/pkg/datalayer" | ||
| "github.com/authzed/spicedb/pkg/spiceerrors" | ||
| ) | ||
|
|
||
| // compiledCaveatCacheKey identifies the schema-derived cache of compiled (deserialized) | ||
| // caveats. The cache is tied to a single stored-schema version (via datalayer.CachedSchema) | ||
| // and is discarded when the schema changes. | ||
| var compiledCaveatCacheKey = datalayer.NewDerivedCacheKey("caveats.compiled") | ||
|
|
||
| func init() { | ||
| if err := datalayer.RegisterDerivedCache(compiledCaveatCacheKey, func() any { return &CompiledCaveatCache{} }); err != nil { | ||
| spiceerrors.MustPanicf("failed to register compiled caveat cache: %v", err) | ||
| } | ||
| } | ||
|
|
||
| // CompiledCaveatCache caches deserialized caveats (which embed a built CEL environment) by | ||
| // caveat name, for a single schema version. Deserializing a caveat rebuilds its CEL | ||
| // environment, which is expensive; caching it on the (shared) stored schema avoids paying | ||
| // that cost on every check. | ||
| type CompiledCaveatCache struct { | ||
| m sync.Map // map[string]*caveats.CompiledCaveat | ||
| } | ||
|
|
||
| // GetOrCompile returns the cached compiled caveat for name, or invokes compile and caches | ||
| // the result. compile is only called on a miss; concurrent misses may call compile more | ||
| // than once but only one result is retained. | ||
| func (c *CompiledCaveatCache) GetOrCompile(name string, compile func() (*caveats.CompiledCaveat, error)) (*caveats.CompiledCaveat, error) { | ||
| if v, ok := c.m.Load(name); ok { | ||
| return v.(*caveats.CompiledCaveat), nil | ||
| } | ||
| compiled, err := compile() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| actual, _ := c.m.LoadOrStore(name, compiled) | ||
| return actual.(*caveats.CompiledCaveat), nil | ||
| } | ||
|
|
||
| // CompiledCaveatCacheFor returns the compiled-caveat cache tied to the given cached schema, | ||
| // building it lazily on first access. | ||
| func CompiledCaveatCacheFor(s *datalayer.CachedSchema) *CompiledCaveatCache { | ||
| return datalayer.GetDerivedCache[*CompiledCaveatCache](s, compiledCaveatCacheKey) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package caveats | ||
|
|
||
| import ( | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/authzed/spicedb/pkg/caveats" | ||
| caveattypes "github.com/authzed/spicedb/pkg/caveats/types" | ||
| ) | ||
|
|
||
| func TestCompiledCaveatCacheGetOrCompile(t *testing.T) { | ||
| env := caveats.NewEnvironmentWithTypeSet(caveattypes.Default.TypeSet) | ||
| compiled, err := caveats.CompileCaveatWithName(env, "1 == 1", "test") | ||
| require.NoError(t, err) | ||
|
|
||
| c := &CompiledCaveatCache{} | ||
| calls := 0 | ||
| compile := func() (*caveats.CompiledCaveat, error) { | ||
| calls++ | ||
| return compiled, nil | ||
| } | ||
|
|
||
| // First access compiles; subsequent accesses return the cached instance without recompiling. | ||
| got1, err := c.GetOrCompile("a", compile) | ||
| require.NoError(t, err) | ||
| require.Same(t, compiled, got1) | ||
|
|
||
| got2, err := c.GetOrCompile("a", compile) | ||
| require.NoError(t, err) | ||
| require.Same(t, compiled, got2) | ||
| require.Equal(t, 1, calls, "compile should be invoked once per name") | ||
|
|
||
| // Distinct names compile independently. | ||
| _, err = c.GetOrCompile("b", compile) | ||
| require.NoError(t, err) | ||
| require.Equal(t, 2, calls) | ||
|
|
||
| // Errors propagate and are not cached. | ||
| boom := errors.New("boom") | ||
| _, err = c.GetOrCompile("c", func() (*caveats.CompiledCaveat, error) { | ||
| return nil, boom | ||
| }) | ||
| require.ErrorIs(t, err, boom) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we expecting to have many of these? Like do we need a registry, or can this be singleton?