-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Regular db pings to reconnect after timed out connections #41819
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 |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ | |
| use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection; | ||
| use Doctrine\DBAL\Driver; | ||
| use Doctrine\DBAL\Exception; | ||
| use Doctrine\DBAL\Exception\ConnectionLost; | ||
| use Doctrine\DBAL\Platforms\MySQLPlatform; | ||
| use Doctrine\DBAL\Platforms\OraclePlatform; | ||
| use Doctrine\DBAL\Platforms\SqlitePlatform; | ||
|
|
@@ -78,6 +79,7 @@ class Connection extends PrimaryReadReplicaConnection { | |
|
|
||
| /** @var DbDataCollector|null */ | ||
| protected $dbDataCollector = null; | ||
| private array $lastConnectionCheck = []; | ||
|
|
||
| protected ?float $transactionActiveSince = null; | ||
|
|
||
|
|
@@ -127,10 +129,13 @@ public function __construct( | |
| public function connect($connectionName = null) { | ||
| try { | ||
| if ($this->_conn) { | ||
| $this->reconnectIfNeeded(); | ||
| /** @psalm-suppress InternalMethod */ | ||
| return parent::connect(); | ||
| } | ||
|
|
||
| $this->lastConnectionCheck[$this->getConnectionName()] = time(); | ||
|
|
||
| // Only trigger the event logger for the initial connect call | ||
| $eventLogger = \OC::$server->get(IEventLogger::class); | ||
| $eventLogger->start('connect:db', 'db connection opened'); | ||
|
|
@@ -679,4 +684,26 @@ public function rollBack() { | |
| } | ||
| return $result; | ||
| } | ||
|
|
||
| private function reconnectIfNeeded(): void { | ||
| if ( | ||
| !isset($this->lastConnectionCheck[$this->getConnectionName()]) || | ||
| $this->lastConnectionCheck[$this->getConnectionName()] + 30 >= time() || | ||
| $this->isTransactionActive() | ||
|
Comment on lines
+690
to
+692
Contributor
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. Regarding this condition: This means that if we never saved the last connection check for a connection, we are skipping the reconnect logic? Practically, this seems like for single DB server setups, we are always skipping the reconnect logic, as |
||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $this->_conn->query($this->getDriver()->getDatabasePlatform()->getDummySelectSQL()); | ||
| $this->lastConnectionCheck[$this->getConnectionName()] = time(); | ||
| } catch (ConnectionLost|\Exception $e) { | ||
| $this->logger->warning('Exception during connectivity check, closing and reconnecting', ['exception' => $e]); | ||
| $this->close(); | ||
| } | ||
| } | ||
|
|
||
| private function getConnectionName(): string { | ||
| return $this->isConnectedToPrimary() ? 'primary' : 'replica'; | ||
| } | ||
| } | ||
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.
$this->getConnectionName()will returnreplicawhen the$this->_connisnull.So, on initialization, we will always save: