Skip to content

Commit 31af9f9

Browse files
authored
Add "tcp_nodelay" option to StreamConnection (#31)
1 parent 3a7e0d0 commit 31af9f9

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ use Tarantool\Client\Packer\PurePacker;
2626

2727
$conn = new StreamConnection();
2828
// or
29-
// $conn = new StreamConnection('tcp://127.0.0.1:3301');
30-
// $conn = new StreamConnection('tcp://127.0.0.1:3301', ['socket_timeout' => 5.0, 'connect_timeout' => 5.0]);
29+
// $conn = new StreamConnection('tcp://127.0.0.1:3301', [
30+
// 'socket_timeout' => 5.0,
31+
// 'connect_timeout' => 5.0,
32+
// 'tcp_nodelay' => true,
33+
// ]);
34+
// or
3135
// $conn = new StreamConnection('unix:///tmp/tarantool_instance.sock');
3236

3337
$client = new Client($conn, new PurePacker());
3438
// or
3539
// $client = new Client($conn, new PeclPacker());
3640

37-
// If authentication credentials required
41+
// if authentication credentials are required
3842
// $client->authenticate('username', 'userpass');
3943

4044
$space = $client->getSpace('my_space');

src/Connection/StreamConnection.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public function open()
4040
$this->stream = $stream;
4141
stream_set_timeout($this->stream, $this->options['socket_timeout']);
4242

43+
// TODO
44+
// use stream_context_create() for php 7.1+
45+
// https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/standard/tests/streams/stream_context_tcp_nodelay.phpt
46+
47+
if (isset($this->options['tcp_nodelay']) && function_exists('socket_import_stream')) {
48+
$socket = socket_import_stream($this->stream);
49+
socket_set_option($socket, SOL_TCP, TCP_NODELAY, (int) $this->options['tcp_nodelay']);
50+
}
51+
4352
$greeting = $this->read(IProto::GREETING_SIZE, 'Unable to read greeting.');
4453

4554
return IProto::parseGreeting($greeting);

tests/Integration/ConnectionTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,50 @@ public function testReadLargeResponse()
210210
$this->assertSame($data, $result->getData()[0]);
211211
}
212212

213+
/**
214+
* @requires extension sockets
215+
*
216+
* @group tcp_only
217+
* @group pure_only
218+
*/
219+
public function testTcpNoDelayEnabled()
220+
{
221+
$clientBuilder = ClientBuilder::createFromEnv();
222+
$clientBuilder->setConnectionOptions(['tcp_nodelay' => true]);
223+
224+
$client = $clientBuilder->build();
225+
$client->ping();
226+
227+
$conn = $client->getConnection();
228+
$prop = (new \ReflectionObject($conn))->getProperty('stream');
229+
$prop->setAccessible(true);
230+
231+
$socket = socket_import_stream($prop->getValue($conn));
232+
$this->assertSame(1, socket_get_option($socket, SOL_TCP, TCP_NODELAY));
233+
}
234+
235+
/**
236+
* @requires extension sockets
237+
*
238+
* @group tcp_only
239+
* @group pure_only
240+
*/
241+
public function testTcpNoDelayDisabled()
242+
{
243+
$clientBuilder = ClientBuilder::createFromEnv();
244+
$clientBuilder->setConnectionOptions(['tcp_nodelay' => false]);
245+
246+
$client = $clientBuilder->build();
247+
$client->ping();
248+
249+
$conn = $client->getConnection();
250+
$prop = (new \ReflectionObject($conn))->getProperty('stream');
251+
$prop->setAccessible(true);
252+
253+
$socket = socket_import_stream($prop->getValue($conn));
254+
$this->assertSame(0, socket_get_option($socket, SOL_TCP, TCP_NODELAY));
255+
}
256+
213257
/**
214258
* @dataProvider \Tarantool\Client\Tests\GreetingDataProvider::provideGreetingsWithInvalidServerName
215259
*/

0 commit comments

Comments
 (0)