change reconect wrapper to only check connection after inactivity

instead of always checking every minute, only check the connection after a minute of inactivity.
this should better reflect how idle timeouts of sql servers work and remove most of the cases where this check was previously done.

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2021-02-09 16:30:59 +01:00
parent b3ac30c4da
commit b51f32ec08
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
1 changed files with 5 additions and 4 deletions

View File

@ -31,21 +31,22 @@ use Doctrine\DBAL\Driver;
class ReconnectWrapper extends \Doctrine\DBAL\Connection { class ReconnectWrapper extends \Doctrine\DBAL\Connection {
public const CHECK_CONNECTION_INTERVAL = 60; public const CHECK_CONNECTION_INTERVAL = 60;
private $lastConnectionCheck = null; private $lastQuery = null;
public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null) { public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null) {
parent::__construct($params, $driver, $config, $eventManager); parent::__construct($params, $driver, $config, $eventManager);
$this->lastConnectionCheck = time(); $this->lastQuery = time();
} }
public function connect() { public function connect() {
$now = time(); $now = time();
$checkTime = $now - self::CHECK_CONNECTION_INTERVAL; $checkTime = $now - self::CHECK_CONNECTION_INTERVAL;
if ($this->lastConnectionCheck > $checkTime || $this->isTransactionActive()) { if ($this->lastQuery > $checkTime || $this->isTransactionActive()) {
$this->lastQuery = $now;
return parent::connect(); return parent::connect();
} else { } else {
$this->lastConnectionCheck = $now; $this->lastQuery = $now;
if (!$this->ping()) { if (!$this->ping()) {
$this->close(); $this->close();
} }