Compute ETag from (mtime, size) to skip whole-file load per request#212
Merged
Conversation
nginx (ngx_http_set_etag) and Apache HTTPD's default FileETag MTime Size both derive a static-file ETag from the same metadata. Doing the same here lets FileHTTPHandler and DirectoryHTTPHandler skip the per-request Data(contentsOf:) + SHA-256 load. Closes TVT-290 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #212 +/- ##
==========================================
- Coverage 92.75% 92.74% -0.02%
==========================================
Files 70 70
Lines 3645 3653 +8
==========================================
+ Hits 3381 3388 +7
- Misses 264 265 +1 ☔ View full report in Codecov by Sentry. |
swhitty
approved these changes
Apr 28, 2026
swhitty
approved these changes
Apr 28, 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.
Note that this PR exchanges performance for accuracy. It changes ETag to be based on modification time & size rather than a proper SHA. This technique is also used by Apache and nginx.
This PR would also simplify updating the directory handler to adopt streaming as the file handler already does.
Summary
"<hex-mtime>-<hex-size>", matching nginx (ngx_http_set_etaginsrc/http/ngx_http_core_module.c) and Apache HTTPD's defaultFileETag MTime Size.FileHTTPHandlerandDirectoryHTTPHandlerno longer callData(contentsOf:)solely to compute an ETag.W/prefix) soIf-Rangekeeps working forFileHTTPHandler's Range path.Closes TVT-290.
Test plan
HTTPCacheControlTests(6 tests): nil for missing file; quoted strong format with hex-hex inner; stable for unchanged file; differs on size change; differs on mtime change; collides when mtime+size match but contents differ (the property that distinguishes a metadata ETag from a content-hash ETag).swift test— 435 tests pass.swift build— clean, no warnings.🤖 Generated with Claude Code