datasette.allowed_many() method and per-request permission check cache#2775
Merged
Conversation
Adds a per-request cache for permission check results, plus wiring that resolves action permissions in bulk before plugin hooks need them: - New _permission_check_cache contextvar, set to a fresh dict for each request by DatasetteRouter and reset when the request ends. Keys include the full serialized actor, so actors differing in any field (e.g. token restrictions) never share entries. SkipPermissions mode bypasses the cache entirely. - datasette.allowed_many() now consults the cache and stores its results there, so repeated datasette.allowed() checks within one request resolve without further SQL. - Table pages resolve all registered table-level actions against the current table and all database-level actions against its database (database pages likewise) in batched queries before invoking the table_actions/database_actions plugin hooks - allowed() calls made inside those hooks are then served from the cache with no plugin changes required. Actions with no permission rules from any plugin are resolved to False without touching the database. Benchmarks (benchmarks/) with a simulated 12-plugin ecosystem making 18 checks per table page show 34 -> 13 internal-DB queries per page; with 2ms-per-query internal DB latency (modelling Datasette Cloud) table page time drops from 77.9ms to 27.6ms - the caching layer accounts for ~91% of that improvement over allowed_many() alone. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2775 +/- ##
=====================================
Coverage 0.00% 0.00%
=====================================
Files 70 70
Lines 10744 10839 +95
=====================================
- Misses 10744 10839 +95 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
GPT-5.5 xhigh in Codex spotted this problem and fixed it with a CTE: https://gisthost.github.io/?46076499ee685acddc988ff6b47a74b0
Owner
Author
|
A code review from GPT-5.5 xhigh spotted an optimization: https://gisthost.github.io/?46076499ee685acddc988ff6b47a74b0 - the big SQL query would crash at over 500 actions because of SQLite compound SELECT limits, but using a CTE instead fixes that. |
simonw
commented
Jun 13, 2026
Comment on lines
+1461
to
+1466
| This hook may be called for many actions in rapid succession - for example | ||
| :ref:`datasette.allowed_many() <datasette_allowed_many>` gathers rules for every action in its batch | ||
| concurrently before table and database pages render their action menus. Hook implementations must not | ||
| assume that checks for different actions arrive one page-render apart, and expensive work (such as | ||
| network calls) should be cached independently of the ``action`` argument where possible. | ||
|
|
Owner
Author
There was a problem hiding this comment.
I don't think this documentation is useful.
simonw
added a commit
that referenced
this pull request
Jun 16, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Got this out of Fable before they shut it down.
📚 Documentation preview 📚: https://datasette--2775.org.readthedocs.build/en/2775/