Skip to content

Commit e4b65f0

Browse files
authored
Add connect context arg to Connection::connect() (amphp#82)
Addresses amphp#81.
1 parent f0ecf78 commit e4b65f0

2 files changed

Lines changed: 19 additions & 20 deletions

File tree

src/Connection.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,23 @@ final class Connection implements Link
3333

3434
/**
3535
* @param ConnectionConfig $config
36-
* @param CancellationToken|null $config
36+
* @param CancellationToken|null $token
37+
* @param Socket\ClientConnectContext|null $context Note that TCP No Delay will be set to on automatically.
3738
*
3839
* @return Promise
3940
*/
40-
public static function connect(ConnectionConfig $config, CancellationToken $token = null): Promise
41-
{
42-
$token = $token ?? new NullCancellationToken();
43-
44-
return call(function () use ($config, $token) {
45-
static $connectContext;
46-
47-
$connectContext = $connectContext ?? (new Socket\ClientConnectContext())->withTcpNoDelay();
48-
$socket = yield Socket\connect($config->getConnectionString(), $connectContext, $token);
41+
public static function connect(
42+
ConnectionConfig $config,
43+
CancellationToken $token = null,
44+
Socket\ClientConnectContext $context = null
45+
): Promise {
46+
$token = $token ?? new NullCancellationToken;
47+
48+
$context = $context ?? new Socket\ClientConnectContext;
49+
50+
return call(function () use ($config, $token, $context) {
51+
$context = $context->withTcpNoDelay();
52+
$socket = yield Socket\connect($config->getConnectionString(), $context, $token);
4953

5054
$processor = new Internal\Processor($socket, $config);
5155
yield $processor->connect();

src/TimeoutConnector.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@
33
namespace Amp\Mysql;
44

55
use Amp\Promise;
6+
use Amp\Socket\ClientConnectContext;
67
use Amp\Sql\ConnectionConfig as SqlConnectionConfig;
78
use Amp\Sql\Connector;
8-
use Amp\TimeoutCancellationToken;
9-
use function Amp\call;
109

1110
final class TimeoutConnector implements Connector
1211
{
1312
const DEFAULT_TIMEOUT = 5000;
1413

15-
/** @var int */
16-
private $timeout;
14+
/** @var ClientConnectContext */
15+
private $context;
1716

1817
/**
1918
* @param int $timeout Milliseconds until connections attempts are cancelled.
2019
*/
2120
public function __construct(int $timeout = self::DEFAULT_TIMEOUT)
2221
{
23-
$this->timeout = $timeout;
22+
$this->context = (new ClientConnectContext())->withConnectTimeout($timeout);
2423
}
2524

2625
/**
@@ -34,10 +33,6 @@ public function connect(SqlConnectionConfig $config): Promise
3433
throw new \TypeError(\sprintf("Must provide an instance of %s to MySQL connectors", ConnectionConfig::class));
3534
}
3635

37-
return call(function () use ($config) {
38-
$token = new TimeoutCancellationToken($this->timeout);
39-
40-
return Connection::connect($config, $token);
41-
});
36+
return Connection::connect($config, null, $this->context);
4237
}
4338
}

0 commit comments

Comments
 (0)