Skip to content

Commit

Permalink
Add "tcp_nodelay" option to StreamConnection (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
rybakit authored Aug 28, 2017
1 parent 3a7e0d0 commit 31af9f9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ use Tarantool\Client\Packer\PurePacker;

$conn = new StreamConnection();
// or
// $conn = new StreamConnection('tcp://127.0.0.1:3301');
// $conn = new StreamConnection('tcp://127.0.0.1:3301', ['socket_timeout' => 5.0, 'connect_timeout' => 5.0]);
// $conn = new StreamConnection('tcp://127.0.0.1:3301', [
// 'socket_timeout' => 5.0,
// 'connect_timeout' => 5.0,
// 'tcp_nodelay' => true,
// ]);
// or
// $conn = new StreamConnection('unix:///tmp/tarantool_instance.sock');

$client = new Client($conn, new PurePacker());
// or
// $client = new Client($conn, new PeclPacker());

// If authentication credentials required
// if authentication credentials are required
// $client->authenticate('username', 'userpass');

$space = $client->getSpace('my_space');
Expand Down
9 changes: 9 additions & 0 deletions src/Connection/StreamConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public function open()
$this->stream = $stream;
stream_set_timeout($this->stream, $this->options['socket_timeout']);

// TODO
// use stream_context_create() for php 7.1+
// https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/standard/tests/streams/stream_context_tcp_nodelay.phpt

if (isset($this->options['tcp_nodelay']) && function_exists('socket_import_stream')) {
$socket = socket_import_stream($this->stream);
socket_set_option($socket, SOL_TCP, TCP_NODELAY, (int) $this->options['tcp_nodelay']);
}

$greeting = $this->read(IProto::GREETING_SIZE, 'Unable to read greeting.');

return IProto::parseGreeting($greeting);
Expand Down
44 changes: 44 additions & 0 deletions tests/Integration/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,50 @@ public function testReadLargeResponse()
$this->assertSame($data, $result->getData()[0]);
}

/**
* @requires extension sockets
*
* @group tcp_only
* @group pure_only
*/
public function testTcpNoDelayEnabled()
{
$clientBuilder = ClientBuilder::createFromEnv();
$clientBuilder->setConnectionOptions(['tcp_nodelay' => true]);

$client = $clientBuilder->build();
$client->ping();

$conn = $client->getConnection();
$prop = (new \ReflectionObject($conn))->getProperty('stream');
$prop->setAccessible(true);

$socket = socket_import_stream($prop->getValue($conn));
$this->assertSame(1, socket_get_option($socket, SOL_TCP, TCP_NODELAY));
}

/**
* @requires extension sockets
*
* @group tcp_only
* @group pure_only
*/
public function testTcpNoDelayDisabled()
{
$clientBuilder = ClientBuilder::createFromEnv();
$clientBuilder->setConnectionOptions(['tcp_nodelay' => false]);

$client = $clientBuilder->build();
$client->ping();

$conn = $client->getConnection();
$prop = (new \ReflectionObject($conn))->getProperty('stream');
$prop->setAccessible(true);

$socket = socket_import_stream($prop->getValue($conn));
$this->assertSame(0, socket_get_option($socket, SOL_TCP, TCP_NODELAY));
}

/**
* @dataProvider \Tarantool\Client\Tests\GreetingDataProvider::provideGreetingsWithInvalidServerName
*/
Expand Down

0 comments on commit 31af9f9

Please sign in to comment.