Add ServerContextInterface for coroutine-safe request handling#171
Add ServerContextInterface for coroutine-safe request handling#171
Conversation
Introduce ServerContextInterface to abstract $_SERVER access, enabling thread-safe operation in concurrent environments like Swoole/RoadRunner. Changes: - Add ServerContextInterface with get() and has() methods - Add GlobalServerContext for traditional PHP-FPM (default) - Update ResourceStorage to use ServerContextInterface for X_VARY handling - Add RepositoryLoggerInterface::reset() for long-running environments Note: MobileEtagSetter is not modified (uses global state, rarely used)
|
@coderabbitai review |
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the
📝 WalkthroughWalkthroughThe PR introduces a thread-safe server context abstraction ( Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Tip 🧪 Unit Test Generation v2 is now available!We have significantly improved our unit test generation capabilities. To enable: Add this to your reviews:
finishing_touches:
unit_tests:
enabled: trueTry it out by using the Have feedback? Share your thoughts on our Discord thread! Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
✅ Actions performedReview triggered.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 1.x #171 +/- ##
===========================================
Coverage 100.00% 100.00%
- Complexity 241 246 +5
===========================================
Files 52 53 +1
Lines 729 746 +17
===========================================
+ Hits 729 746 +17 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Greptile OverviewGreptile SummaryAdds Key Changes
ArchitectureThe change follows a clean abstraction pattern - users running on Swoole/RoadRunner can provide a custom request-scoped implementation of Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Client
participant ResourceStorage
participant ServerContext as ServerContextInterface
participant Cache as TagAwareAdapter
Note over Client,Cache: Cache Key Generation with Vary Support
Client->>ResourceStorage: get(uri)
ResourceStorage->>ResourceStorage: getUriKey(uri, "ro-")
ResourceStorage->>ServerContext: has("X_VARY")
alt X_VARY exists
ServerContext-->>ResourceStorage: true
ResourceStorage->>ResourceStorage: getVary()
ResourceStorage->>ServerContext: get("X_VARY")
ServerContext-->>ResourceStorage: "val1,val2"
loop for each vary value
ResourceStorage->>ServerContext: get("X_VAL1"), get("X_VAL2")
ServerContext-->>ResourceStorage: value
end
ResourceStorage->>ResourceStorage: append vary string to key
else X_VARY not present
ServerContext-->>ResourceStorage: false
end
ResourceStorage->>Cache: getItem(key)
Cache-->>ResourceStorage: cached state
ResourceStorage-->>Client: ResourceState|null
Note over ResourceStorage,ServerContext: PHP-FPM: GlobalServerContext uses $_SERVER<br/>Swoole/RoadRunner: Custom implementation with request scope
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@tests/GlobalServerContextTest.php`:
- Around line 9-24: The tearDown currently only unsets
TEST_KEY/TEST_STRING/TEST_INT but misses restoring HTTP_USER_AGENT and X_VARY;
update GlobalServerContextTest to save original values of
$_SERVER['HTTP_USER_AGENT'] and $_SERVER['X_VARY'] in setUp (or capture the
whole $_SERVER snapshot) and then restore those specific keys in tearDown
(reassign original values if they existed, or unset if they were absent) so that
tests do not leak HTTP_USER_AGENT or X_VARY state between runs; modify the setUp
and tearDown methods in GlobalServerContextTest accordingly.
🧹 Nitpick comments (1)
src/ResourceStorage.php (1)
246-260: Trim X_VARY tokens to handle optional whitespace.Without trimming, values like
"Accept-Language, User-Agent"will look forX_ USER-AGENTand skip a vary dimension, risking cache collisions.✅ Suggested fix
- $varys = explode(',', $xvary); + $varys = explode(',', $xvary); $varyString = ''; foreach ($varys as $vary) { + $vary = trim($vary); + if ($vary === '') { + continue; + } $phpVaryKey = sprintf('X_%s', strtoupper($vary)); $value = $this->serverContext->get($phpVaryKey); if ($value !== null) { $varyString .= $value; } }
- Fix test tearDown to restore HTTP_USER_AGENT and X_VARY - Trim X_VARY tokens to handle optional whitespace
|
@coderabbitai Fixed: Test tearDown now restores HTTP_USER_AGENT and X_VARY. Added trim() to handle whitespace in X_VARY tokens. |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: Length of output: 2397 🏁 Script executed: Length of output: 9935
✅ Test tearDown restoration (lines 28-38 in
✅ trim() for X_VARY tokens (line 257 in $vary = trim($vary);
if ($vary === '') {
continue;
}
Both improvements enhance the robustness of the implementation for production use and testing. The changes look great! 🎉 |
Replace unreachable null-return with assert since getVary() is only
called when has('X_VARY') is true. Add test for empty X_VARY segment.
Summary
ServerContextInterfaceto abstract$_SERVERaccess for thread-safe operation in Swoole/RoadRunnerGlobalServerContextas default implementation for PHP-FPMResourceStorageto useServerContextInterfaceforX_VARYhandlingRepositoryLoggerInterface::reset()for long-running environmentsTest plan
GlobalServerContextTestcovers the interface implementation