Skip to content
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

fix: position of white noise middleware #25857

Merged
merged 8 commits into from
Oct 30, 2024
Merged

fix: position of white noise middleware #25857

merged 8 commits into from
Oct 30, 2024

Conversation

pauldambra
Copy link
Member

@pauldambra pauldambra commented Oct 28, 2024

see see https://posthoghelp.zendesk.com/agent/tickets/19227 where user is reporting slow asset load times through the managed reverse proxy

(i believe that) for some routes assets are loaded through a CDN. For others - including the managed reverse proxy - they are loaded by hitting the django web instances. in these cases we see consistently slow time to first byte (TTFB).


The whitenoise middleware docs (https://whitenoise.readthedocs.io/en/stable/django.html#enable-whitenoise) say its middleware should be high up in the list of middleware

we had a lot of middleware ahead of it, so we're running those middleware on requests for static assets

locally this change makes the request 60-100ms TTFB instead of 120-169ms TTFB


switching on whitenoise's build time compression makes that 30-60ms per request since we're not gzipping the assets on the fly but it is super slow to run the offline compression, so i'm not doing that here

@pauldambra pauldambra requested a review from a team October 28, 2024 18:17
@@ -96,6 +96,7 @@
"django_structlog.middlewares.RequestMiddleware",
"django_structlog.middlewares.CeleryMiddleware",
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would definitely put this behind ShortCircuitMiddleware, as otherwise you're loading this for every capture/decide endpoint hit.

Copy link
Member Author

Choose a reason for hiding this comment

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

dropping it below the AllowIPMiddleware didn't seem to have an impact on response time (in my super scientific refreshing locally testing) so went below those two

@pauldambra pauldambra requested a review from timgl October 28, 2024 18:51
Copy link
Contributor

@benjackwhite benjackwhite left a comment

Choose a reason for hiding this comment

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

No more context than anyone else to know if this is a good call or not but in principle - sure?

@timgl timgl merged commit bc07ba4 into master Oct 30, 2024
89 checks passed
@timgl timgl deleted the fix/white-noise branch October 30, 2024 18:16
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.

3 participants