Write cached responses atomically#491
Merged
Merged
Conversation
Every request that renders a cacheable page writes the response body to the same cache file, so under concurrent requests for one page the losers fail with "resource busy (file is locked)" and are served a 500 - the behaviour reported in #482, where a homepage flood turned a third of responses into errors. A half-written file was also briefly visible to nginx, which serves the cache directory directly. The response is now written to a temporary file (suffixed with the writing thread's id, so concurrent writers cannot collide on that name either) and renamed into place, which is atomic. Under the same 400-connection homepage flood, every response is now a 200.
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.
Every request that renders a cacheable page writes the response body to the same cache file, so concurrent requests for one page race and the losers fail with
resource busy (file is locked)and are served a 500.@flip111 tracked this down in #482 — hitting the homepage with 400 connections caused about a third of responses to produce 500s, each paired with the
withBinaryFile: resource busyerror. The response is now written to a temporary file and renamed into place, which is atomic.Verified using the same approach as @flip111; all ~3600 responses are 200s without errors, the cache file looks correct, and no temp files are left behind.