Skip to content

Conversation

@bmtcril
Copy link
Contributor

@bmtcril bmtcril commented Nov 25, 2025

The is_admin_or_superuser_check is being called once per policy when checking enforcement, creating a potential performance issue with numerous calls to the database. This adds a RequestCache to use an in-thread memory cache for these repeated calls.

Also fixes an ongoing issue with requirements upgrades.

Merge checklist:
Check off if complete or not applicable:

  • Version bumped
  • Changelog record added
  • Documentation updated (not only docstrings)
  • Fixup commits are squashed away
  • Unit tests added/updated
  • Manual testing instructions provided
  • Noted any: Concerns, dependencies, migration issues, deadlines, tickets

@BryanttV
Copy link
Contributor

Hi @bmtcril, thanks for starting this conversation! Regarding the test failures, I was reviewing them and found that the global scope was not being used correctly. I'm actually not sure why they weren’t failing before, but this PR solves it: #147

@bmtcril bmtcril force-pushed the bmtcril/cache_superuser_check branch from 934807f to d8a03a4 Compare November 25, 2025 18:40
if auto_load_policy_interval > 0:
cls.configure_enforcer_auto_loading(auto_load_policy_interval)
else:
logger.warning("CASBIN_AUTO_LOAD_POLICY_INTERVAL is not set or zero; auto-load is disabled.")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This warning was firing several times in a request, I don't think we need it anymore?


compile-requirements: ## compile the requirements/*.txt files with the latest packages satisfying requirements/*.in
pip install -qr requirements/pip-tools.txt
pip install -qr requirements/pip.txt
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the fix we've been using elsewhere to deal with the current pip-tools compatibility issues with pip.

-c https://raw.githubusercontent.com/edx/edx-lint/master/edx_lint/files/common_constraints.txt

# Different packages want different versions of click, we force the most compatible one here
click==8.3.0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is another pip-tools issue that was blocking make upgrade . pip-tools wanted 8.3.1, everything else wanted 8.3.0 so I've pinned it here for now.

@bmtcril bmtcril force-pushed the bmtcril/cache_superuser_check branch from dcf6472 to 3611811 Compare November 25, 2025 19:24
@bmtcril bmtcril marked this pull request as ready for review November 25, 2025 19:26
Copy link
Member

@mariajgrimaldi mariajgrimaldi left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you so much for working on this :)

@bmtcril bmtcril changed the title DO NOT MERGE: refactor: cache the results of is_admin_or_superuser_check Cache the results of is_admin_or_superuser_check Nov 25, 2025
Tycho Hob added 4 commits November 25, 2025 14:36
is_admin_or_superuser_check is being called once per policy when
checking enforcement, creating a potential performance issue with
numerous calls to the database. This adds a brief cache to offload some
of the burden, but we will need a better fix long term.
By using the RequestCache instead of the Django cache we are able to
have a thread-local memory copy of the user's superuser / staff state
that exists only for the length of the request. This will save a large
number of round trips to the cache backend.
@bmtcril bmtcril force-pushed the bmtcril/cache_superuser_check branch from 3611811 to a8350d6 Compare November 25, 2025 19:39
Copy link
Contributor

@BryanttV BryanttV left a comment

Choose a reason for hiding this comment

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

@bmtcril, thanks! I tested locally, and it works great!

@bmtcril
Copy link
Contributor Author

bmtcril commented Nov 26, 2025

This has been tested on the dev sandbox and provides a substantial performance improvement, no errors were noted. Mean response time under load for the validate/me call went from ~470ms to ~109ms. Auth User selects went from 171 to 2.

There is room for future improvement by reducing the columns selected to just is_staff / is_superuser and ensuring we don't join auth_userprofile but it's likely to have negligible impact compared to this.

@mariajgrimaldi mariajgrimaldi merged commit e8d27f6 into main Nov 27, 2025
14 checks passed
@mariajgrimaldi mariajgrimaldi deleted the bmtcril/cache_superuser_check branch November 27, 2025 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants