Reconnect after MongoDB primary changes - #44
Conversation
📝 WalkthroughWalkthrough
ChangesNotWritablePrimary handling
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant MongoDBServer
participant SyncTransport
participant Client
MongoDBServer->>SyncTransport: Return response with error code 10107
SyncTransport->>Client: Deliver response frame
Client->>Client: Detect NotWritablePrimary
Client->>SyncTransport: Invalidate connection
Client-->>MongoDBServer: Replay MongoDB exception to caller
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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 |
Greptile SummaryThe PR now reconnects after MongoDB primary changes while retaining transaction session state.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains. Important Files Changed
Reviews (2): Last reviewed commit: "(fix): reconnect after MongoDB primary c..." | Re-trigger Greptile |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
tests/ClientTest.php (1)
493-514: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winCover the
writeErrorsbranch too.This test exercises only top-level
errmsghandling, while the implementation also adds 10107 invalidation forwriteErrors. Add a response containingwriteErrors[0]with code 10107 and assert transport/state cleanup; retain a permanent write-error case to verify existing behavior.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/ClientTest.php` around lines 493 - 514, Extend testNotWritablePrimaryClosesTransportBeforeTheErrorIsReplayed to cover a response whose writeErrors[0] has code 10107, asserting the same transport close and client state cleanup. Also retain or add a permanent write-error case with a non-10107 code to verify existing behavior remains unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/Client.php`:
- Around line 1805-1810: Update the invalidate handling in the write-error paths
around the transaction retry flow, including both referenced 10107 branches, so
invalidating a not-writable-primary connection replaces the physical transport
without removing the logical transaction session from $this->sessions. Ensure
withTransaction can reuse the existing session when it retries via
startTransaction($session), while preserving normal session cleanup for
non-transactional invalidation.
- Around line 89-90: Update the composer.json PHP version constraint from >=8.0
to >=8.3 to match the typed class constant syntax used by
Client::NOT_WRITABLE_PRIMARY_ERRORS.
---
Nitpick comments:
In `@tests/ClientTest.php`:
- Around line 493-514: Extend
testNotWritablePrimaryClosesTransportBeforeTheErrorIsReplayed to cover a
response whose writeErrors[0] has code 10107, asserting the same transport close
and client state cleanup. Also retain or add a permanent write-error case with a
non-10107 code to verify existing behavior remains unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: df6546ee-1d14-4b06-89e2-4335780414b5
📒 Files selected for processing (2)
src/Client.phptests/ClientTest.php
| private const array NOT_WRITABLE_PRIMARY_ERRORS = [10107]; | ||
|
|
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/usr/bin/env bash
set -euo pipefail
files="$(fd -a -t f | rg '(^|/)(composer\.json|composer\.lock|\.php-version|\.tool-versions|Dockerfile[^/]*|[^/]+\.ya?ml)$' || true)"
if [ -n "$files" ]; then
printf '%s\n' "$files" | xargs rg -n -i 'php|php-version|PHP_VERSION'
else
echo "No PHP toolchain declarations found"
fiRepository: utopia-php/mongo
Length of output: 26517
🌐 Web query:
PHP 8.3 typed class constant arrays declared type required const array syntax
💡 Result:
As of PHP 8.3, you can explicitly declare types for class constants, including arrays [1][2]. The syntax requires placing the type declaration between the visibility modifier (if present) and the constant name [3][2]. For an array type, the syntax is as follows: public const array MY_ARRAY = ['value1', 'value2']; Key details regarding typed class constants in PHP 8.3: - Strict Typing: Class constants are always evaluated strictly, regardless of the declare(strict_types=1) directive [3][4]. - Covariance: If a constant is overridden in a child class or interface implementation, the type cannot be widened [3][4]. - Array Content: When using the array type, the contents of the array must be scalar types [2][5]. - Backward Compatibility: Declaring a type for a class constant is a breaking change for existing code where child classes might redefine the constant with a different type [3]. All subclasses and implementations must adhere to the declared type, otherwise, a fatal error will occur [3]. - Supported Types: Aside from array, you can use scalar types such as bool, int, float, and string, as well as nullable, union, intersection, and DNF types [3][4][2]. void, callable, and never types are not supported [4].
Citations:
- 1: https://www.php.net/releases/8.3/en.php
- 2: https://www.php.net/manual/en/language.oop5.constants.php
- 3: https://php.watch/versions/8.3/typed-constants
- 4: https://wiki.php.net/rfc/typed_class_constants
- 5: https://www.php.net/language.oop5.constants.php
Raise the minimum supported PHP version.
private const array is a PHP 8.3 typed class constant, but composer.json still declares "php": ">=8.0", so Composer can install this on PHP 8.0–8.2 where it will fail with a parse error. Align the package version constraint with the PHP 8.3 syntax.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/Client.php` around lines 89 - 90, Update the composer.json PHP version
constraint from >=8.0 to >=8.3 to match the typed class constant syntax used by
Client::NOT_WRITABLE_PRIMARY_ERRORS.
d62f3e1 to
2ab78cf
Compare
Summary
Validation