-
Notifications
You must be signed in to change notification settings - Fork 3
Fix pool exhaustion: retry on creation failure, add diagnostics #29
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -251,6 +251,7 @@ public function pop(): Connection | |
| { | ||
| $attempts = 0; | ||
| $totalSleepTime = 0; | ||
| $lastException = null; | ||
|
|
||
| try { | ||
| do { | ||
|
|
@@ -279,15 +280,20 @@ public function pop(): Connection | |
| $this->pool->synchronized(function () { | ||
| $this->connectionsCreated--; | ||
| }); | ||
| throw $e; | ||
| // Don't throw immediately - fall through to try getting | ||
| // an existing connection from the pool | ||
| $lastException = $e; | ||
| } | ||
| } | ||
|
|
||
| $connection = $this->pool->pop($this->getSynchronizationTimeout()); | ||
|
|
||
| if ($connection === false || $connection === null) { | ||
| if ($attempts >= $this->getRetryAttempts()) { | ||
| throw new Exception("Pool '{$this->name}' is empty (size {$this->size})"); | ||
| $activeCount = count($this->active); | ||
| $idleCount = $this->pool->count(); | ||
| $message = "Pool '{$this->name}' is empty (size {$this->size}, active {$activeCount}, idle {$idleCount})"; | ||
| throw new Exception($message, 0, $lastException); | ||
|
Comment on lines
+293
to
+296
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't report repeated creation failures as pool exhaustion. If Possible fix if ($connection === false || $connection === null) {
if ($attempts >= $this->getRetryAttempts()) {
$activeCount = count($this->active);
$idleCount = $this->pool->count();
- $message = "Pool '{$this->name}' is empty (size {$this->size}, active {$activeCount}, idle {$idleCount})";
+ $message = ($lastException !== null && $activeCount === 0 && $idleCount === 0)
+ ? "Pool '{$this->name}' failed to create a connection (size {$this->size}, active {$activeCount}, idle {$idleCount})"
+ : "Pool '{$this->name}' is empty (size {$this->size}, active {$activeCount}, idle {$idleCount})";
throw new Exception($message, 0, $lastException);
}🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| $totalSleepTime += $this->getRetrySleep(); | ||
|
|
@@ -302,7 +308,9 @@ public function pop(): Connection | |
| } | ||
| } while ($attempts < $this->getRetryAttempts()); | ||
|
|
||
| throw new Exception('Failed to get a connection from the pool'); | ||
| $activeCount = count($this->active); | ||
| $idleCount = $this->pool->count(); | ||
| throw new Exception("Pool '{$this->name}' failed to provide a connection (size {$this->size}, active {$activeCount}, idle {$idleCount})", 0, $lastException); | ||
| } finally { | ||
| $this->recordPoolTelemetry(); | ||
| $this->telemetryWaitDuration->record($totalSleepTime, $this->telemetryAttributes); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The chained exception still drops the original cause.
$lastExceptionhere is only the wrapper returned bycreateConnection(). That method currently throws a fresh\Exceptionwithout$easprevious, so the final chain still loses the original exception type and stack.Preserve the root cause from
createConnection()} catch (\Exception $e) { if ($attempts >= $this->getReconnectAttempts()) { - throw new \Exception('Failed to create connection: ' . $e->getMessage()); + throw new \Exception('Failed to create connection: ' . $e->getMessage(), 0, $e); } sleep($this->getReconnectSleep()); }Also applies to: 293-296, 311-313
🤖 Prompt for AI Agents