Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 36 additions & 34 deletions src/Driver/Mysqli/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
class Mysqli extends Driver implements CRUDAbleInterface, CRUDQueryableInterface
{
public const string ID = "mysqli";

/** @var int[] */
protected const array CONNECTION_ERROR_CODES = [2006, 2013];

protected string $id = self::ID;

/**
Expand Down Expand Up @@ -77,8 +81,6 @@ class Mysqli extends Driver implements CRUDAbleInterface, CRUDQueryableInterface
*/
protected ?\mysqli $connection = null;

protected int $connectionRetries = 1;

/**
* Mysqli constructor.
*
Expand Down Expand Up @@ -123,6 +125,17 @@ protected function reconnect(): void
$this->connect();
}

/**
* @return RetryCollection
*/
protected function getRetryCollection(): RetryCollection
{
return new RetryCollection([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure it's a good idea to set the retries to 1 and 3 by default?
The default behavior of PHP / Mysqli would be 1 try (-> 0 retries).
I'm concerned that people might overlook that this is implemented differently in this library.

new RetryGroup(static::CONNECTION_ERROR_CODES, 1), // connection
new RetryGroup([1213], 3) // deadlock
]);
}

/**
* Execute a mysql query
*
Expand All @@ -134,24 +147,19 @@ protected function reconnect(): void
protected function rawQuery(string $query): mysqli_result|true
{
$this->connect();
$retries = $this->connectionRetries;
$retries = $this->getRetryCollection();
while (true) {
try {
return $this->connection->query($query);
} catch (mysqli_sql_exception $e) {
// no more retries left
if ($retries <= 0) {
throw MysqliException::fromException($e);
}

// connection error, try to reconnect and retry
if ($e->getCode() === 2006 || $e->getCode() === 2013) {
$this->reconnect();
$retries--;
if ($retries->canRetry($e->getCode())) {
if (in_array($e->getCode(), static::CONNECTION_ERROR_CODES, true)) {
$this->reconnect();
}
$this->handleRetriedException($e);
continue;
}

// other error, throw exception
throw MysqliException::fromException($e);
}
}
Expand All @@ -166,29 +174,33 @@ protected function rawQuery(string $query): mysqli_result|true
protected function escape(string $data): string
{
$this->connect();
$retries = $this->connectionRetries;
$retries = $this->getRetryCollection();
while (true) {
try {
return $this->connection->real_escape_string($data);
} catch (mysqli_sql_exception $e) {
// no more retries left
if ($retries <= 0) {
throw MysqliException::fromException($e);
}

// connection error, try to reconnect and retry
if ($e->getCode() === 2006 || $e->getCode() === 2013) {
$this->reconnect();
$retries--;
if ($retries->canRetry($e->getCode())) {
if (in_array($e->getCode(), static::CONNECTION_ERROR_CODES, true)) {
$this->reconnect();
}
$this->handleRetriedException($e);
continue;
}

// other error, throw exception
throw MysqliException::fromException($e);
}
}
}

/**
* @param mysqli_sql_exception $exception
* @return void
*/
protected function handleRetriedException(mysqli_sql_exception $exception): void
{
// can be used to log retried exceptions
}

/**
* Save the model
*
Expand Down Expand Up @@ -383,14 +395,4 @@ public function setDatabase(string $database): Mysqli
$this->database = $database;
return $this;
}

/**
* @param int $connectionRetries
* @return $this
*/
public function setConnectionRetries(int $connectionRetries): static
{
$this->connectionRetries = $connectionRetries;
return $this;
}
}
50 changes: 50 additions & 0 deletions src/Driver/Mysqli/RetryCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Aternos\Model\Driver\Mysqli;

class RetryCollection
{
/**
* @param RetryGroup[] $groups
*/
public function __construct(protected array $groups = [])
{
}

/**
* @param RetryGroup $group
* @return $this
*/
public function addGroup(RetryGroup $group): static
{
$this->groups[] = $group;
return $this;
}

/**
* @param int[] $statusCodes
* @return $this
*/
public function removeGroup(array $statusCodes): static
{
$this->groups = array_values(array_filter($this->groups, function (RetryGroup $group) use ($statusCodes) {
return !$group->matchesStatusCodes($statusCodes);
}));
return $this;
}

/**
* @param int $statusCode
* @return bool
*/
public function canRetry(int $statusCode): bool
{
foreach ($this->groups as $group) {
if ($group->matchesStatusCode($statusCode) && $group->hasRetriesLeft()) {
$group->addRetry();
return true;
}
}
return false;
}
}
74 changes: 74 additions & 0 deletions src/Driver/Mysqli/RetryGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Aternos\Model\Driver\Mysqli;

class RetryGroup
{
protected int $retries = 0;

/**
* @param int[] $statusCodes
* @param int $maxRetries
*/
public function __construct(
protected array $statusCodes = [],
protected int $maxRetries = 1,
)
{
}

/**
* @return int
*/
public function getMaxRetries(): int
{
return $this->maxRetries;
}

/**
* @return void
*/
public function addRetry(): void
{
$this->retries++;
}

/**
* @return bool
*/
public function hasRetriesLeft(): bool
{
return $this->retries < $this->maxRetries;
}

/**
* @return int[]
*/
public function getStatusCodes(): array
{
return $this->statusCodes;
}

/**
* @param int $statusCode
* @return bool
*/
public function matchesStatusCode(int $statusCode): bool
{
return in_array($statusCode, $this->statusCodes);
}

/**
* @param int[] $statusCodes
* @return bool
*/
public function matchesStatusCodes(array $statusCodes): bool
{
foreach ($statusCodes as $statusCode) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also use

return array_any($statusCodes, fn($statusCode) => $this->matchesStatusCode($statusCode));

here

if ($this->matchesStatusCode($statusCode)) {
return true;
}
}
return false;
}
}
Loading