Skip to content

Commit

Permalink
Pass socket connector to connect()
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Dec 10, 2023
1 parent 5e321f2 commit c1ff625
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/SocketMysqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Amp\Cancellation;
use Amp\DeferredFuture;
use Amp\Socket\Socket;
use Amp\Socket\SocketConnector;
use Amp\Socket\SocketException;
use Amp\Sql\SqlException;
use Amp\Sql\TransactionIsolation;
use Amp\Sql\TransactionIsolationLevel;
use Revolt\EventLoop;
Expand All @@ -19,10 +21,19 @@ final class SocketMysqlConnection implements MysqlConnection
private readonly \Closure $release;

public static function connect(
Socket $socket,
SocketConnector $connector,
MysqlConfig $config,
?Cancellation $cancellation = null,
): self {
try {
$socket = $connector->connect($config->getConnectionString(), $config->getConnectContext(), $cancellation);
} catch (SocketException $exception) {
throw new SqlException(
'Connecting to the MySQL server failed: ' . $exception->getMessage(),
previous: $exception,
);
}

$processor = new Internal\ConnectionProcessor($socket, $config);
$processor->connect($cancellation);
return new self($processor);
Expand Down
5 changes: 2 additions & 3 deletions src/SocketMysqlConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ public function connect(SqlConfig $config, ?Cancellation $cancellation = null):
throw new \TypeError(\sprintf("Must provide an instance of %s to MySQL connectors", MysqlConfig::class));
}

$socket = ($this->connector ?? Socket\socketConnector())
->connect($config->getConnectionString(), $config->getConnectContext(), $cancellation);
$connector = $this->connector ?? Socket\socketConnector();

return SocketMysqlConnection::connect($socket, $config, $cancellation);
return SocketMysqlConnection::connect($connector, $config, $cancellation);
}
}

0 comments on commit c1ff625

Please sign in to comment.